[英]Property overriden with “new” gets dupped in WCF Webservice
I have a problem with a Webservice I'm creating, the thing is, I have a class, name it ClassA with PropertyA, he na ClassB inheriting ClassA that overrides PropertyA using "new" like this: 我正在创建的Web服务有问题,问题是,我有一个类,用PropertyA命名为ClassA,他没有ClassB继承ClassA,而ClassA使用“新”覆盖了PropertyA,如下所示:
[DataContract]
public class ClassA
{
[DataMember]
public string PropertyA { get; set; }
[DataMember]
public string PropertyB { get; set; }
}
[DataContract]
public class ClassB : ClassA
{
[DataMember]
public new int PropertyA { get; set; }
}
This should make ClassB "replace" the string PropertyA from ClassA for a int PropertyA in ClassB. 这应该使ClassB从ClassA中“替换”字符串PropertyA,以替换ClassB中的int PropertyA。
I expose ClassB in a service method like this: 我在这样的服务方法中公开ClassB:
public bool TellMeSomething(ClassB param) { .... whatever .... }
When I add the service reference in the client application, the classes imported show something like this: 当我在客户端应用程序中添加服务引用时,导入的类显示如下内容:
public class ClassA
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
}
public class ClassB : ClassA
{
public int PropertyA1 { get; set; }
}
As you can notice, ClassB has no "new int PropertyA" anymore, but a "int PropertyA1". 如您所见,ClassB不再有“新int PropertyA”,而是“ int PropertyA1”。 I guess the serializer finds the ancestor has a PropertyA already and doesn't know how to handle the "new" keyword to hide the "string PropertyA", so it creates a new name for the PropertyA in the inherited class...
我猜想序列化程序发现祖先已经有一个PropertyA,并且不知道如何处理“ new”关键字来隐藏“ string PropertyA”,因此它在继承的类中为PropertyA创建了一个新名称。
Is there any way to avoid this behavior? 有什么办法可以避免这种行为? I would need that property to be called like it should...
我需要像应该那样调用该属性...
You should use generics instead of identifier re-using: 您应该使用泛型而不是重复使用标识符:
[DataContract]
public class ClassA<T>
{
[DataMember]
public T PropertyA { get; set; }
[DataMember]
public int PropertyB { get; set; }
}
// No need to override PropertyA since T is int and ClassB
// inherits both PropertyA and PropertyB...
[DataContract]
public class ClassB : ClassA<int>
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.