开发者

Silverlight - Extend WCF Class in the Client

开发者 https://www.devze.com 2023-02-10 09:56 出处:网络
I\'ve seen this question asked in various incarnations around the web, but never a definitive 开发者_开发知识库answer.And since this is the forum for definitive answers....

I've seen this question asked in various incarnations around the web, but never a definitive 开发者_开发知识库answer. And since this is the forum for definitive answers....

I am using WCF to provide the Entity objects for my Silverlight client, based on an SQL database.

I'd like to extend the class for this object on the client side only. I am interested in creating properties that make sense for binding only, and don't make sense to store in the main object. Is it possible to extend this class? If not, is there a "best practice" for doing this sort of thing?

Thanks, wTs


Generated classes actually have the modifier 'partial', so it is possible to extend them.

For example, this DataContract:

[DataContract]
public class TestObject
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Title { get; set; }
}

Can be extended on the client's side in this way:

namespace SilverlightTest.ServiceReference1
{
    public partial class TestObject
    {
        public string ExtendedTitle
        {
            get { return this.Id+" "+this.Title; }
        }
    }
}

Take notice of the name of namespace, it must be the same as the namespace of a service reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消