C# Activator.CreateInstance()方法使用
2017-05-26 16:19
776 查看
C#在类工厂中动态创建类的实例,所使用的方法为:
两种方法区别仅为:创建无参数的构造方法和创建有参数的构造函数。
//Activator.CreateInstance(Type)
object result = null;
Type typeofControl =null;
typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl);
//Activator.CreateInstance(Type,Object[])
object result = null;
Type typeofControl =null;
typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);
但是在动态创建时,可能会动态使用到外部应用的DLL中类的实例,则此时需要进行反编译操作,使用Reflection命名控件下的Assembly类。
//先使用Assembly类载入DLL,再根据类的全路径获取类
object result = null;
Type typeofControl = null;
Assembly tempAssembly;
tempAssembly = Assembly.LoadFrom(vDllName);
typeofControl = tempAssembly.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);
1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Object[]) |
//Activator.CreateInstance(Type)
object result = null;
Type typeofControl =null;
typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl);
//Activator.CreateInstance(Type,Object[])
object result = null;
Type typeofControl =null;
typeofControl = Type.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);
但是在动态创建时,可能会动态使用到外部应用的DLL中类的实例,则此时需要进行反编译操作,使用Reflection命名控件下的Assembly类。
//先使用Assembly类载入DLL,再根据类的全路径获取类
object result = null;
Type typeofControl = null;
Assembly tempAssembly;
tempAssembly = Assembly.LoadFrom(vDllName);
typeofControl = tempAssembly.GetType(vFullClassName);
result = Activator.CreateInstance(typeofControl, objParam);
相关文章推荐
- C# Activator.CreateInstance()方法使用
- (转) C# Activator.CreateInstance()方法使用
- C# Activator.CreateInstance()方法使用
- C# Activator.CreateInstance()方法使用
- C# Activator.CreateInstance()方法使用
- C#的Activator.CreateInstance方法
- C# Activator.CreateInstance()方法
- C#学习常用方法(4000)---Activator.CreateInstance方法
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
- C#反射之一(Assembly.Load及Assembly.Load.CreateInstance方法介绍)
- 关于反射中Assembly.CreateInstance()与Activator.CreateInstance()方法的区别
- C#反射中Assembly.Load及Assembly.Load.CreateInstance方法介绍
- C#反射中Assembly.Load及Assembly.Load.CreateInstance 方法
- 用Activator .CreateInstance代替new实现类的实例化Assembly.CreateInstance()与Activator.CreateInstance()方法的区别
- C#反射中Assembly.Load及Assembly.Load.CreateInstance方法介绍
- 关于Assembly.CreateInstance()与Activator.CreateInstance()方法的区别
- C#反射之(Assembly_Load及Assembly_Load_CreateInstance方法介绍)
- Activator.CreateInstance 方法 (Type)和Class1 obj=new Class1()方法实例化
- 再谈Activator.CreateInstance(Type type)方法创建对象和Expression Tree创建对象性能的比较(更新版)
- Activator.CreateInstance(T) 方法