Reflection in C # (1)

zhaozj2021-02-16  90

Many times you need to create an instance according to the information of the class, you need to create an example with the constructor with parameters, as follows: string assemblyfile; string implclass; string [] impCTorsig; // Constructor parameter type array example, "system.string" other protected object CreateInstance (object [] ctorArgs) {// Load Assembly Assembly ass = Assembly.LoadFrom (assemblyFile); // Get class, the name must be a class name space with type t = ass.GetType (implClass); // Parameter Type Type [] ctorsigr = type.gettypeArray (IMPLCTORSIG); // Get constructor information constructorinfo ctorinfo = t.getConstructor (CTORSIGARR); // Configure instance Return Ctorinfo.invoke (CTORARGS) by parameter list. Common ways not to create object instances by constructive functions: Object O = Activator.createInstance (T);

转载请注明原文地址:https://www.9cbs.com/read-16097.html

New Post(0)