动态创建类的实例

时间:2019-01-16 14:35:38

标签: c# generics reflection

我正在尝试动态创建和使用类的实例,但始终遇到错误“ a是变量但用作类型”。

  //First we need to find the project that holds all of our entity models in the assembly
        var assembly = AppDomain.CurrentDomain.GetAssemblies()
                      .Where(x => x.FullName.Contains("MyProject.Models"))
                      .FirstOrDefault();

 //Now we need to search through the assembly to match the Entity to the supplied TableName
        var type = assembly.GetTypes()
                  .FirstOrDefault(t => t.Name == localTableName);

        var a = Activator.CreateInstance(type);

为什么我不能这样使用

List<a> dynamicList = new List<a>;

或者我需要做的

var query = context.Database.SqlQuery<a>(...);

在链接的重复项中,它只是翻译一个列表。我可能不需要列表,我可能需要在SQLQuery中使用的类

0 个答案:

没有答案
相关问题