【发布时间】:2015-11-16 19:00:45
【问题描述】:
我想使用自己的函数来处理不同的数据库表。当我使用 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))
};
创建这个的想法是遍历数组以创建查询表并为每个填充一个字典。
尝试这样做的函数是:
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 的表。
我想动态地获得表 SQLEventUnits、SQLDisciplines 等。
有谁知道怎么做?
提前致谢!
【问题讨论】:
-
您可以通过反射来完成此操作,但请记住,它可能会减慢您的应用程序速度。
-
@Greg 你能举个例子吗?我已经使用
Reflection来调用cCodesPair类型本身的构造函数,但我不知道在这种情况下如何使用Reflection,因为我不想从存储在类中的类型调用任何方法. -
要使用数据表吗?还是建立一个模型?
-
@Greg 我想用表格的内容填充
Dictionaries。为此,我有一个泛型类,它被称为 Load 方法。 -
@Greg 我每个
cCodesPair Type都有一个字典
标签: c# windows-10 win-universal-app sqlite-net windows-10-universal