Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何将XMLSerializer与包含IList的Castle ActiveRecord一起使用<;T>;成员_.net_Xml Serialization_Castle Activerecord_Generic List - Fatal编程技术网

.net 如何将XMLSerializer与包含IList的Castle ActiveRecord一起使用<;T>;成员

.net 如何将XMLSerializer与包含IList的Castle ActiveRecord一起使用<;T>;成员,.net,xml-serialization,castle-activerecord,generic-list,.net,Xml Serialization,Castle Activerecord,Generic List,我正在尝试将XMLSerializer与castle活动记录类一起使用,该类如下所示: [ActiveRecord("Model")] public class DataModel : ActiveRecordBase { private IList<Document> documents; [XmlArray("Documents")] public virtual IList<Document> Documents {

我正在尝试将XMLSerializer与castle活动记录类一起使用,该类如下所示:

[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase
{
    private IList<Document> documents;

    [XmlArray("Documents")]
    public virtual IList<Document> Documents
    {
        get { return documents; }
        set
        {
            documents = value;    
        }
    }
}
[ActiveRecord(“模型”)]
公共类数据模型:ActiveRecordBase
{
私人IList文件;
[XmlArray(“文档”)]
公共虚拟IList文档
{
获取{返回文档;}
设置
{
文件=价值;
}
}
}
但是,XMLSerializer由于IList接口而遇到问题。 (引发异常:无法序列化System.Collections.Generic.IList`1..类型的成员'DataModel.Documents'

我在别处读到,这是XMLSerializer中的一个限制,建议的解决方法是将其声明为
列表
接口

因此,我尝试将
IList
更改为
List
。 这会导致ActiveRecord引发异常: 属性DataModel的类型。文档必须是接口(IList、ISet、IDictionary或其通用计数器部件)。不能将ArrayList或List用作属性类型


所以,问题是:如何将XMLSerializer与包含IList成员的Castle ActiveRecord一起使用?

有趣。。。我能建议的最好方法是在
文档上使用
[XmlIgnore]
——ActiveRecord是否有类似的忽略成员的方法?你可以这样做:

[XmlIgnore]
public virtual IList<Document> Documents
{
    get { return documents; }
    set
    {
        documents = value;    
    }
}

[Tell ActiveRecord to ignore this one...]
[XmlArray("Documents"), XmlArrayItem("Document")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Document[] DocumentsSerialization {
    get {
         if(Documents==null) return null;
         return Documents.ToArray(); // LINQ; or do the long way
    }
    set {
         if(value == null) { Documents = null;}
         else { Documents = new List<Document>(value); }
    }
}
[XmlIgnore]
公共虚拟IList文档
{
获取{返回文档;}
设置
{
文件=价值;
}
}
[告诉ActiveRecord忽略此项…]
[XmlArray(“文件”)、XmlArrayItem(“文件”)]
[可浏览(false)、可编辑可浏览(editorbrowseblestate.Never)]
公共文档[]文档序列化{
得到{
如果(Documents==null)返回null;
返回Documents.ToArray();//LINQ;或执行长程
}
设置{
如果(value==null){Documents=null;}
else{Documents=新列表(值);}
}
}
,因此您必须解决它。一种方法是使用非通用的
IList

[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase<DataModel> {
    [XmlArray("Documents")]
    [HasMany(typeof(Document)]
    public virtual IList Documents {get;set;}
}
[ActiveRecord(“模型”)]
公共类数据模型:ActiveRecordBase{
[XmlArray(“文档”)]
[HasMany(文件的类型)]
公共虚拟IList文档{get;set;}
}

这是有关此错误的更多信息。

谢谢,它可以工作。由于额外的属性,界面变得有点污染,但我真的看不到有多少选择,除非Microsoft对IList序列化问题采取措施。我了解到这不是一个错误,而是一个功能,解决它的方法是使用数据传输对象(或DTOs)。感谢您将我引向Microsoft bug报告。他们没有解决这一问题真是太糟糕了。不过,我不想失去使用泛型列表的类型安全优势,因此我必须选择上面Marc建议的解决方案,或者将所有内容转移到DataContractSerializer。