开发者

WCF - any way to mark an attribute as ignored?

Basically if I have the following:

[DataContract]
public class Foo
{
    [MyCustomAttribute(...)]
    [DataMember(IsRequired = true)]
    public int bar { get; set; }
}

How can I get it so that the MyCustomAttribute is ignored when the user generates the class using "Add Service Reference..."

Basically, I don't want that attribute to be set on the properties of the client generated code. Keep in mind I still 开发者_C百科want the property itself to show up, but basically the client should look like this...

[DataContract]
public class Foo
{
    [DataMember(IsRequired = true)]
    public int bar { get; set; }
}


There is no way to have your attributes included in the code that gets generated by clients

Reference: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/ce241118-bc79-4788-a739-c445a52fcd1d


The [DataMember] is part of the serialization engine and thus needed. The reason you see it in the proxy is that the proxies uses the same DataContractSerializer as the service. When IsRequired is set to true the proxy can determine if it is feasible to sent a client (if the required value is set) or not.

From the WSDL it is possible to determine if a property is required or not, so the DataMemberAttribute is set based on the WSDL file; not the source code of the service. The proxies are by default generated entirely from service descriptions available on the net. And as the other answers mention you will surely not get your own custom attributes copied to the proxy.


One possibility would be to create an interface and place the Attribute onto the interface methods.

You could query the interface instead of the concrete class, but the WCF would only see the concrete implementation of the class.

You could say:

 [DataContract] 
 public class Foo : IFoo
 {
     [DataMember(IsRequired = true)]
     public int bar { get; set; } 
 }

 public interface IFoo 
 {
     [MyCustomAttribute(...)]
     int bar { get; set; } 
 }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜