Create an instance of a class from a string

后端 未结 8 1453
别跟我提以往
别跟我提以往 2020-11-22 02:53

Is there a way to create an instance of a class based on the fact I know the name of the class at runtime. Basically I would have the name of the class in a string.

8条回答
  •  执笔经年
    2020-11-22 03:20

    Its pretty simple. Assume that your classname is Car and the namespace is Vehicles, then pass the parameter as Vehicles.Car which returns object of type Car. Like this you can create any instance of any class dynamically.

    public object GetInstance(string strFullyQualifiedName)
    {         
         Type t = Type.GetType(strFullyQualifiedName); 
         return  Activator.CreateInstance(t);         
    }
    

    If your Fully Qualified Name(ie, Vehicles.Car in this case) is in another assembly, the Type.GetType will be null. In such cases, you have loop through all assemblies and find the Type. For that you can use the below code

    public object GetInstance(string strFullyQualifiedName)
    {
         Type type = Type.GetType(strFullyQualifiedName);
         if (type != null)
             return Activator.CreateInstance(type);
         foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
         {
             type = asm.GetType(strFullyQualifiedName);
             if (type != null)
                 return Activator.CreateInstance(type);
         }
         return null;
     }
    

    Now if you want to call a parameterized constructor do the following

    Activator.CreateInstance(t,17); // Incase you are calling a constructor of int type
    

    instead of

    Activator.CreateInstance(t);
    

提交回复
热议问题