我可以在运行时创建实体名称吗?尝试实例化新的“按层次结构表”(TBH)实体

时间:2014-03-12 02:12:40

标签: c# linq entity-framework linq-to-entities

我已经使用TPH来创建表的特殊实例,即。

section <- introduction-section

有许多不同的部分类型即结论

section <- conclusion-section

我需要实例化这些类的新对象,如果没有TBH,我会:

var myNewSection = new section();

但是我使用TBH所以我需要实例化子实体,以便正确填充鉴别器:

myNewSection = new I_Section();   db.Section.AddObject(myNewSection);

然而,这会产生一个问题,我必须明确引用TBH实体,即I_Section,并在循环中有一个switch语句。

有没有办法在运行时定义这个类,即I_Section,比如说我可以从循环中的其他值构造一个字符串?

String strSectionCode = "I"
string strFullName = strSectionCode + "_" + "Section";

var myNewSection = new strFullname ??? () // I know this is not possible, but it shows where I am trying to get to.

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用反射,您应该能够在运行时实例化对象。看看Activator.CreateInstance,它提供了几个重载来创建对象的实例