使用泛型类型

本文关键字:泛型类型 | 更新日期: 2023-09-27 18:22:04

我想使用自己的函数来处理不同的数据库表。当我使用SQLite时,表是使用自定义类填充的。我试图递归地填充它们,所以我创建了这个结构和数组:

public class cCodesPair
{
    public string cCodeKey { get; set; }
    public Type CommonCodeClass { get; set; }
    public cCodesPair(string key, Type o)
    {
        this.cCodeKey = key;
        this.CommonCodeClass = o;
    }
}
    private cCodesPair[] codesPairs = new cCodesPair[] 
    {
        new cCodesPair("DESC_UNITS", typeof(SQLEventUnits)),
        new cCodesPair("DESC_DISCIPLINE", typeof(SQLDisciplines)),
        new cCodesPair("DESC_COUNTRY", typeof(SQLCountries)),
        new cCodesPair("DESC_VIDEOS", typeof(SQLVideos)),
        new cCodesPair("DESC_ATHLETES", typeof(SQLAthletes))
    };

创建它的想法是在数组中循环以创建查询表,并为每个表填充一个Dictionary。

尝试做到这一点的功能是:

    public void Load<T>(T t, SQLiteConnection conn) where T : Type
    {
        try
        {
            var query = conn.Table<T>;
            //MORE CODE...
        } catch (Exception ex)
        {
            Debug.WriteLine("Exception")
        }
    }

在数组中循环并调用Load函数的函数如下:

    private async Task fillDictionaries()
    {
        for (int i = 0; i < codesPairs.Length; i++)
        {
            MasterDescriptions m = new MasterDescriptions();
            Type t = codesPairs[i].CommonCodeClass;
            m.Load(t, conn);
        }
    }

但我知道查询的表是一个名为Type的表。

我想按常规获取表SQLEventUnitsSQLDisciplines等。有人知道怎么做吗?提前感谢!

使用泛型类型

您知道Microsoft企业库吗?https://msdn.microsoft.com/en-us/library/ff648951.aspx

做你想要实现的事情。另一个参考:http://www.codeproject.com/Tips/657233/Enterprise-Library-6-Sqlite-Logging-and-Exceptions