Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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
C# SQLite网络扩展中多个(相关)类类型的数组_C#_Xamarin_Sqlite Net Extensions - Fatal编程技术网

C# SQLite网络扩展中多个(相关)类类型的数组

C# SQLite网络扩展中多个(相关)类类型的数组,c#,xamarin,sqlite-net-extensions,C#,Xamarin,Sqlite Net Extensions,在SQLite.Net扩展中,有没有一种方法可以让不同的类类型集合继承自同一父类?理想情况下,我希望有一个符合接口或抽象类的对象数组,但是我甚至不能让标准类的子类型工作 public class ActivityCategory { [PrimaryKey, AutoIncrement] public int Id { get; set; } public string Title { get; set; } public string Icon { get; s

在SQLite.Net扩展中,有没有一种方法可以让不同的类类型集合继承自同一父类?理想情况下,我希望有一个符合接口或抽象类的对象数组,但是我甚至不能让标准类的子类型工作

public class ActivityCategory
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Icon { get; set; }
    public bool Recommended { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public SomeAbstractClass[] multipleTypesAllowed { get; set; }
}

public abstract class SomeAbstractClass
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string SomeValue { get; set; }

    [ForeignKey(typeof(ActivityCategory))]
    public int ActivityCategoryId { get; set; }

    [ManyToOne(CascadeOperations = CascadeOperation.All)]
    public ActivityCategory Category { get; set; }
}
除了multipleTypesAllowed的空数组之外,我无法让上面返回任何内容。如果我使父类不抽象并实例化它,它就可以工作,但即使它的子类也不能工作。有没有办法让继承与SQLite.Net一起工作


干杯

当您使用GetWithChildren获取ActivityCategory对象时,sqlite net内部使用一个反射来调用
new ActivateCategory()
,然后为每个子对象调用
new SomeAbstractClass()
。这就是为什么sqlite net库中的所有泛型都有约束
where T:new()
。 这就是当类是抽象的时得到空数组的原因。它只是不能被库实例化。 如果希望在同一数组中存储不同类型的实例,就不能让sqlite net猜测每次应该使用哪种类型。 当然,我不知道应用程序的逻辑,但我认为如果不使用sqlite net扩展,就无法避免手动编写查询。 例如,如果基类是Vehicle,而Car和Truck是派生类,则可以使用:

    db.CreateTable<Vehicle>();
    Car c = new Car();
    Truck t = new Truck();
    db.Insert(c, typeof(Vehicle));
    db.Insert(t, typeof(Vehicle));
    ...
    List<Car> cars = db.Query<Car>("select * from Vehicle where VehicleType = ?", VehicleTypes.Car);
    List<Truck> trucks = db.Query<Truck>("select * from Vehicle where VehicleType = ?", VehicleTypes.Truck);
db.CreateTable();
c车=新车();
卡车t=新卡车();
db.插入(c,车辆类型);
db.插入(t,车辆类型);
...
List cars=db.Query(“从车辆中选择*,其中车辆类型=?”,车辆类型.Car);
List trucks=db.Query(“从车辆中选择*,其中车辆类型=?”,车辆类型.Truck);
然后,您可以将两个列表合并为一个车辆列表,并将此列表添加到父实例(示例中为ActivityCategory)