C#的Activator.CreateInstance方法
2015-12-08 00:05
441 查看
转载自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html
Activator.CreateInstance 方法 (Type)
使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
C#
public static Object
CreateInstance (Type type)
Activator.CreateInstance 泛型方法 ()
注意:此方法在 .NET Framework 2.0 版中是新增的。
创建类型的一个实例,该类型由指定的泛型类型参数指定。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
C#
public static T
CreateInstance<T> ()
Activator.CreateInstance
(Type)实例
输入一个类的名称为参数,返回一个相应的类的实例。
这在工厂模式中是非常有用的,这样,可以使程序有更高的扩展性
/*
* Created by SharpDevelop.
* User: 19004991
* Date: 2009-8-6
* Time: 9:10
*/
using System;
namespace ActivatorCreateInstance
{
public interface IObject
{
void printName();
}
public class ClassExam
: IObject
{
private string name="default
name";
public ClassExam()
{
}
public ClassExam(string name)
{
this.name = name;
}
public void printName()
{
Console .WriteLine (this.name
);
}
}
public class Program
{
public Program()
{
}
public static void Main(string[]
args)
{
// TODO:
Implement Functionality Here
IObject obj1=(IObject)Activator.CreateInstance(System.Type.GetType
("ActivatorCreateInstance.ClassExam"));
obj1.printName();
IObject obj2=(IObject)Activator.CreateInstance(System.Type.GetType("ActivatorCreateInstance.ClassExam"),new string[]{"seted
new name"});
obj2.printName();
Console.Write("Press
any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Activator.CreateInstance 方法 (Type)
使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
C#
public static Object
CreateInstance (Type type)
Activator.CreateInstance 泛型方法 ()
注意:此方法在 .NET Framework 2.0 版中是新增的。
创建类型的一个实例,该类型由指定的泛型类型参数指定。
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
C#
public static T
CreateInstance<T> ()
Activator.CreateInstance
(Type)实例
输入一个类的名称为参数,返回一个相应的类的实例。
这在工厂模式中是非常有用的,这样,可以使程序有更高的扩展性
/*
* Created by SharpDevelop.
* User: 19004991
* Date: 2009-8-6
* Time: 9:10
*/
using System;
namespace ActivatorCreateInstance
{
public interface IObject
{
void printName();
}
public class ClassExam
: IObject
{
private string name="default
name";
public ClassExam()
{
}
public ClassExam(string name)
{
this.name = name;
}
public void printName()
{
Console .WriteLine (this.name
);
}
}
public class Program
{
public Program()
{
}
public static void Main(string[]
args)
{
// TODO:
Implement Functionality Here
IObject obj1=(IObject)Activator.CreateInstance(System.Type.GetType
("ActivatorCreateInstance.ClassExam"));
obj1.printName();
IObject obj2=(IObject)Activator.CreateInstance(System.Type.GetType("ActivatorCreateInstance.ClassExam"),new string[]{"seted
new name"});
obj2.printName();
Console.Write("Press
any key to continue . . . ");
Console.ReadKey(true);
}
}
}
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程