【发布时间】:2011-12-15 20:20:18
【问题描述】:
有人知道是否可以仅使用属性来使 DataContract 序列化使用隐式转换为类中属性类型的字符串运算符?
例如:
[DataContract]
public class Root
{
[DataMember]
public Element Member { get; set; }
}
public class Element
{
private string value;
private Element(string value)
{
this.value = value;
}
public static implicit operator string(Element element)
{
return element.value != null ? element.value : "";
}
public static implicit operator Element(string value)
{
if (Something()) return new Element(value);
throw new InvalidCastException()
}
}
(这只是手忙脚乱写的,不管编译等问题)
拉斯-埃里克
【问题讨论】:
-
你想达到什么目的?
-
您希望系统如何将字符串反序列化回元素?
-
我希望它使用定义的两个运算符来隐式地来回转换它。它可以像拥有一个名为 type 的 DataMember 属性一样简单。 IE。 [DataMember(Type=typeof(string))]
标签: c# .net wcf datacontractserializer