mercredi 16 janvier 2019

Creating instance of a class dynamically [duplicate]

This question already has an answer here:

I am trying to create and use an instance of a class dynamically but keep running into the error " a is a variable but used as a type".

  //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);

Why can't I use this like so

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





Aucun commentaire:

Enregistrer un commentaire