【发布时间】:2011-10-25 18:18:44
【问题描述】:
我正在从数据库中获取 SomeThingType,然后想要调用 SomeThing<T>(T value)。我在运行时从数据库中获取类型。
我卡住了,怎么办?
ProductType productType = GetProductType(userId);
SomeThingType someThingType = GetSomeThingTypeFromDb(userId);
switch(productType)
{
case ProductType.ONE:
// stuck here
IThing<int> a = new SomeThing<int>(..);
IThing<string> a = new SomeThing<string>(..);
IThing<DateTime> a = new SomeThing<DateTime>(..);
break;
}
我不知道如何根据我在运行时从数据库中检索到的枚举值来使用正确的泛型类型。
db 值映射到我想用来确定我应该使用哪种泛型类型的枚举,字符串、int 或 DateTime。
这是我可以在 c# 中解决的问题吗?或者这只能在动态语言中实现?
【问题讨论】: