C# 无法强制转换类型为';System.Collections.ArrayList';输入';System.Collections.Generic.IEnumerable';

C# 无法强制转换类型为';System.Collections.ArrayList';输入';System.Collections.Generic.IEnumerable';,c#,nhibernate,C#,Nhibernate,我最近将Windows SmartClient解决方案从nHibernate 2.2升级到4.0,在写入db时遇到异常 在以下代码中引发异常: this.session.Save(this.Location); // NHibernate.ISession tx.Commit(); // exception thrown here 例外情况是: NHibernate.dll中的“System.InvalidCastException” System.InvalidCastException:

我最近将Windows SmartClient解决方案从nHibernate 2.2升级到4.0,在写入db时遇到异常

在以下代码中引发异常:

this.session.Save(this.Location); // NHibernate.ISession
tx.Commit(); // exception thrown here
例外情况是:

NHibernate.dll中的“System.InvalidCastException” System.InvalidCastException: 无法强制转换“System.Collections.ArrayList”类型的对象 输入'System.Collections.Generic.IEnumerable'1[System.Object]'

保存的对象中有多个列表,以下是几个具有代表性的列表:

protected System.Collections.IList locationList;
public virtual System.Collections.IList AssociatedLocationList
{
    get
    {
        if (this.locationList == null)
        {
            this.locationList = new System.Collections.ArrayList();
        }
        return this.locationList;
    }
    set { this.locationList = value; }
}
protected System.Collections.Generic.IList<Inspection> inspectionList;
public virtual System.Collections.Generic.IList<Inspection> InspectionList
{
    get
    {
        if (this.inspectionList == null)
        {
            this.inspectionList = new System.Collections.Generic.List<Inspection>();
        }

        return this.inspectionList;
    }
    set { this.inspectionList = value; }
}
protected System.Collections.IList locationList;
公共虚拟系统.Collections.IList AssociatedLocationList
{
得到
{
if(this.locationList==null)
{
this.locationList=新的System.Collections.ArrayList();
}
返回此.locationList;
}
设置{this.locationList=value;}
}
受保护的System.Collections.Generic.IList检查列表;
公共虚拟系统.Collections.Generic.IList检查列表
{
得到
{
if(this.inspectionList==null)
{
this.inspectionList=new System.Collections.Generic.List();
}
返回此.inspectionList;
}
设置{this.inspectionList=value;}
}
请注意,有些指定了类型,有些则没有

一个建议是将属性设置为
IList
,但我已经有了它


可以做什么?

我希望我正确地理解了您的问题,如果是这样,我不确定您是否需要进行空检查。相反,父类应该有一个位置列表,这些位置完全保存在另一个表中

这是您的位置列表所在的类。

public class Parent
{
    public virtual Guid Id { get; set; }
    public virtual IList<Location> Locations { get; set; }

    //This is the mapping for this class.
    public class ParentMapping : ClassMap<Parent>
    {
        Id(x => x.Id).GeneratedBy.Guid();

        //This is what relates your location list to this parent.
        //Notice that in the Location object below, 
        //there's a Owner property which will point back to here.
        HasMany(x => x.Locations).Cascade.All();
    }
}
public class Location
{
    public virtual Guid Id { get; set; }
    public virtual Parent Owner { get; set; }
    public virtual string SomeProperty { get; set; }

    //This is the mapping for this class.
    public class LocationMapping : ClassMap<Location>
    {
        Id(x => x.Id).GeneratedBy.Guid();

        Map(x => x.SomeProperty);

        //This will relate our property back to the parent.
        References(x => x.Owner);
    }
}
公共类父类
{
公共虚拟Guid Id{get;set;}
公共虚拟IList位置{get;set;}
//这是该类的映射。
公共类父映射:类映射
{
Id(x=>x.Id).GeneratedBy.Guid();
//这是将您的位置列表与此父级关联的内容。
//请注意,在下面的Location对象中,
//这里有一个业主财产,它将指向这里。
HasMany(x=>x.Locations).Cascade.All();
}
}
这是定义位置的类。

public class Parent
{
    public virtual Guid Id { get; set; }
    public virtual IList<Location> Locations { get; set; }

    //This is the mapping for this class.
    public class ParentMapping : ClassMap<Parent>
    {
        Id(x => x.Id).GeneratedBy.Guid();

        //This is what relates your location list to this parent.
        //Notice that in the Location object below, 
        //there's a Owner property which will point back to here.
        HasMany(x => x.Locations).Cascade.All();
    }
}
public class Location
{
    public virtual Guid Id { get; set; }
    public virtual Parent Owner { get; set; }
    public virtual string SomeProperty { get; set; }

    //This is the mapping for this class.
    public class LocationMapping : ClassMap<Location>
    {
        Id(x => x.Id).GeneratedBy.Guid();

        Map(x => x.SomeProperty);

        //This will relate our property back to the parent.
        References(x => x.Owner);
    }
}
公共类位置
{
公共虚拟Guid Id{get;set;}
公共虚拟父所有者{get;set;}
公共虚拟字符串SomeProperty{get;set;}
//这是该类的映射。
公共类位置映射:类映射
{
Id(x=>x.Id).GeneratedBy.Guid();
Map(x=>x.SomeProperty);
//这将使我们的财产与父母相关。
参考文献(x=>x.Owner);
}
}

这里的问题可能是NHibernate的最新版本。4.0

您有以下选项

1) 大多数情况下,新版本支持向后兼容。如果是这种情况,请查找iSession.Save方法的重载版本。你也可能会得到非通用的

2) 可能新的save方法只支持泛型类型。您的是非通用的,即ArrayList。如果您可以将其更改为Ilist,那应该会有所帮助

3) 若您并没有对ILS的控制权,那个么您可以在这两者之间编写转换器,它可以将Arraylist转换为Ilist,并且可以使用Save方法工作


希望这能有所帮助。

在NHibernate 4.0中删除了对持久性非通用集合的支持。改为转换为泛型集合


请参阅中的突破性更改列表。

您是否考虑过在这方面使用泛型?在光秃秃的物体周围乱扔似乎很。。。2003年。我在原来的帖子中添加了另一个具有代表性的属性。你是说没有这个会导致问题吗?演员阵容将不再以通用版本存在。根据你和@Robert Harvey所说的,我正在制作#2。这可能需要一段时间。此代码已有10年历史。