如何使用[DataMember]将IEnumerable <string>用于WCF中的属性</string>

时间:2013-04-10 11:52:39

标签: wcf

我正在尝试将[DataContract]和[DataMember]用于下面的类。

[DataContract]
public abstract class TestClass
{
    #region Public Contructors        
    protected TestClass()
    {
    }
    #endregion

    #region Public Properties   

    [DataMember]
    public IEnumerable<string> Subjects { get; set; }

    #endregion      


}

我收到以下错误

  

无法序列化成员TestClass.Subjects   的类型System.Collections.Generic.IEnumerable`1 [[System.String,   mscorlib,版本= 4.0.0.0,文化=中性,   PublicKeyToken = b77a5c561934e089]]因为它是一个接口

我应该怎样解决这个问题?

1 个答案:

答案 0 :(得分:2)

如错误消息所示,您无法序列化IEnumerable之类的界面。 尝试使用像List<string>甚至string[]这样的具体集合。

请记住,您通过WCF发送的serialized类可能更适合作为代理类或DTO,因此这样您就不会弄乱您的商业模式。

希望这有帮助