无法强制转换类型为';NHibernate.Collection.Generic.PersistentGenericBag到类型';System.Collections.Generic.List

无法强制转换类型为';NHibernate.Collection.Generic.PersistentGenericBag到类型';System.Collections.Generic.List,nhibernate,fluent-nhibernate,fluent,ilist,Nhibernate,Fluent Nhibernate,Fluent,Ilist,我试图通过反序列化xml文件来加载域类。因此,我在域类中使用了System.Collections.Generic.List。但当我尝试使用Session object保存对象时,它失败了,出现异常“无法将类型为'NHibernate.Collection.Generic.PersistentGenericBag1[MyFirstMapTest.Class5]'的对象强制转换为类型为'System.Collections.Generic.List1[MyFirstMapTest.Class5]”

我试图通过反序列化xml文件来加载域类。因此,我在域类中使用了System.Collections.Generic.List。但当我尝试使用Session object保存对象时,它失败了,出现异常“无法将类型为'NHibernate.Collection.Generic.PersistentGenericBag
1[MyFirstMapTest.Class5]'的对象强制转换为类型为'System.Collections.Generic.List
1[MyFirstMapTest.Class5]”这个问题在之前的一些讨论中已经发布,答案是使用IList而不是List()

但是,如果使用IList,则无法在域类中反序列化xml

XmlTextReader xtr = new XmlTextReader(@"C:\Temp\SampleInput.xml");
XmlSerializer serializer = new XmlSerializer(objClass5.GetType());
objClass5 = (MyFirstMapTest.Class5)serializer.Deserialize(xtr);
session.Save(objClass5);
它抛出了下面的错误 无法序列化System.Collections.Generic.IList`1[[xxxxxxxxxx,示例,版本=1.0.0.0,区域性=中性,PublicKeyToken=null]]类型的成员xxxxx,因为它是一个接口

我尝试使用PersistentGenericBag而不是List,但PersistentGenericBag不可序列化。因此,反序列化不起作用


我如何解决这个问题?感谢您关注此问题。

您可以尝试使用NHibernte绑定的支持字段和序列化的属性,其中属性的类型为List,而支持字段为IList

编辑
流畅映射可能如下所示:

public class HierarchyLevelMap : IAutoMappingOverride<HierarchyLevel>
{
    public void Override(AutoMapping<HierarchyLevel> mapping)
    {
        mapping.HasMany(x => x.StructuralUnits)
            .Access.ReadOnlyPropertyThroughCamelCaseField();
    }
}
public类HierarchyLevelMap:IAutoMappingOverride
{
公共无效替代(自动映射)
{
mapping.HasMany(x=>x.units)
.Access.ReadOnlyPropertyThroughCamelCaseField();
}
}
实体:

public class HierarchyLevel : IEntity
{
    private readonly IList<StructuralUnit> structuralUnits = new List<StructuralUnit>();

    public virtual List<StructuralUnit> StructuralUnits
    {
        get { return structuralUnits; }
        set { structuralUnits = value; }
    }
}
公共类层次结构级别:独立性
{
私有只读IList structuralUnits=新列表();
公共虚拟列表单元
{
获取{return structuralUnits;}
设置{structuralUnits=value;}
}
}

您可以在类中创建以下两个属性:

public class Sample
{
    private IList<Sample> _list;

    [XmlIgnoreAttribute]
    public virtual IList<Sample> List
    {
        get
        {
            return _list;
        }
        set
        {
            _list = value;
        }
    }

    public virtual List<Sample> List
    {
        get
        {
            return (List<Sample>)_list;
        }
        set
        {
            _list = value;
        }
    }
}
公共类示例
{
私有IList_列表;
[XmlIgnoreAttribute]
公共虚拟IList列表
{
得到
{
返回列表;
}
设置
{
_列表=值;
}
}
公共虚拟列表
{
得到
{
返回(列表)\u列表;
}
设置
{
_列表=值;
}
}
}

并且您只映射IList属性。

如何使用备份字段?那是私人财产吗?您有示例吗?我添加了一些示例,您能检查一下吗?这是给我类型转换错误“无法将类型”System.Collections.Generic.IList“隐式转换为”System.Collections.Generic.List“。存在显式转换(是否缺少转换?)“这是给我类型转换错误”无法将类型“System.Collections.Generic.IList”隐式转换为“System.Collections.Generic.List”。存在显式转换(是否缺少转换?)