在创建从List<T>
或ICollection<T>
继承自定义属性的自定义集合时,我发现了这个问题:
public class MyCollection: List<int>
{
public string MyCustomProperty { get; set; }
}
据我所知,这样的集合将传递抛出WCF作为ArrayOfInt,WCF不会序列化我的自定义属性。解决方案是创建将在内部管理集合的包装类,并具有自定义属性。
我想为我的需求做一个更好的解决方法...... IEnumerable<T>
会遇到同样的问题吗?
public class MyCollection: IEnumerable<int>
{
/**************/
/* Code that implements IEnumerable<int> and manages the internal List<T> */
/* I know I will not able to cast it to List<T>, but I don't need it. */
/* If I will need it, I will implement cast operators later */
/**************/
public string MyCustomProperty { get; set; }
}
上面的类是否会抛出WCF包含MyCustomProperty值?
由于
答案 0 :(得分:1)
我尝试了它并没有序列化自定义属性。 我刚从service方法返回了整个类对象。结果仍然是ArrayOfInt(我使用List作为容器)
public class MyExtension: IEnumerable<int>
{
public string CustomString { get; set; }
private List<int> lst = new List<int>();
public void Add(int i)
{
lst.Add(i);
}
public IEnumerator<int> GetEnumerator()
{
return lst.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return lst.GetEnumerator();
}
}
我必须将其标记为DataContract,并将每个成员标记为DataMember以将所有属性序列化。
<MyExtension xmlns="http://schemas.datacontract.org/2004/07/GetRequestTest" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CustomString>sunny</CustomString>
<lst xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>1</a:int>
</lst>
</MyExtension>