【问题标题】:Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag to type 'System.Collections.Generic.List无法将类型为“NHibernate.Collection.Generic.PersistentGenericBag”的对象转换为类型“System.Collections.Generic.List”
【发布时间】:2013-05-20 04:10:36
【问题描述】:

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

但是,如果我使用 IList,那么我无法将 xml 反序列化为 Domain 类。

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, Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 类型的成员 xxxxx,因为它是一个接口。”

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

我该如何解决这个问题?感谢您查看此问题。

【问题讨论】:

    标签: nhibernate fluent-nhibernate fluent ilist


    【解决方案1】:

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

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

    public class HierarchyLevelMap : IAutoMappingOverride<HierarchyLevel>
    {
        public void Override(AutoMapping<HierarchyLevel> mapping)
        {
            mapping.HasMany(x => x.StructuralUnits)
                .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; }
        }
    }
    

    【讨论】:

    • 如何使用支持字段?那是私有财产吗?你有样品吗?
    • 我添加了一些例子,你能检查一下吗?
    • 这给了我类型转换错误“无法将类型 'System.Collections.Generic.IList' 隐式转换为 'System.Collections.Generic.List'。存在显式转换(您是否缺少演员表?)"
    【解决方案2】:

    你可以在你的类中创建两个这样的属性:

    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 属性。

    【讨论】:

    • 这给了我类型转换错误“无法将类型 'System.Collections.Generic.IList' 隐式转换为 'System.Collections.Generic.List'。存在显式转换(您是否缺少演员表?)"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2016-02-10
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2015-09-14
    相关资源
    最近更新 更多