2017-02-16 61 views
0

我的WCF接口看起来是这样的:与DataContract名称重命名WCF泛型类属性

[ServiceContract] 
public interface IService 
{ 
    [OperationContract] 
    Foo<String>[] Test(); 
} 

Foo<T>类看起来是这样的:

[DataContract(Name = "FooOf{0}")] 
public class Foo<T> 
{ 
    public T Value { get; set; } 
} 

我发布的WCF服务,并添加服务引用到我的客户端代码,然后我使用这种方法是这样的:

var client = new ServiceClient(new BasicHttpBinding(), new EndpointAddress(@"http://myServer:port/Service.svc")); 
FooOfString[] result = client.Test(); 

但是,我不能acc ESS生成的类的属性,例如,我不能做到这一点:

var value = result[0].Value; // cannot access property, does not compile 

我能够通过删除我Foo<T>类的[DataContract(Name = "FooOf{0}")]部分做同样的事情,我可以访问属性,问题是生成的类的名称,它更改为FooOfStringCHtiIp13,看起来很丑,我试图将它重命名为更易读。此操作,但是,现在工作:

FooOfStringCHtiIp13[] result = client.Test(); 
var value = result[0].Value; // can access, compiles 

感觉就像一些没有被序列化,我无法正常使用此。任何想法如何正确实现这一点?

+0

'FooOf {0}'格式生成的类名'FooOfT',其中'T'在我的情况'酒吧' – Peroxy

+1

尝试将DataMember属性添加到Value属性? – Joe

+0

我刚刚试过,因为你写你的评论..这是问题 – Peroxy

回答

0

解决我的问题,这个问题是在我Foo<T>类的Value财产,我不得不添加DataMemberAttribute到我的财产:

[DataContract(Name = "FooOf{0}")] 
public class Foo<T> 
{ 
    [DataMemberAttribute(Name = "Value")] 
    public T Value { get; set; } 
}