http://blog.csdn.net/RonoTian/article/details/2900714
语法:
1. Activator.CreateInstance (Type)2. Activator.CreateInstance (Type, Object[])
两种方法区别仅为:创建无参数的构造方法和创建有参数的构造函数。
三、取得Assembly的方法:
Assembly.Load
Assembly.LoadFile
Assembly.LoadFrom
Type对象的Assembly方法
1、Assembly.LoadFile只载入相应的dll文件,比如Assembly.LoadFile("a.dll"),则载入a.dll,假如a.dll中引用了b.dll的话,b.dll并不会被载入。
Assembly.LoadFrom则不一样,它会载入dll文件及其引用的其他dll,比如上面的例子,b.dll也会被载入。
2、用Assembly.LoadFrom载入一个Assembly时,会先检查前面是否已经载入过相同名字的Assembly,比如a.dll有两个版本(版本1在目录1下,版本2放在目录2下),程序一开始时载入了版本1,当使用Assembly.LoadFrom("2\\a.dll")载入版本2时,不能载入,而是返回版本1。
Assembly.LoadFile的话则不会做这样的检查,比如上面的例子换成Assembly.LoadFile的话,则能正确载入
MemberInfo-成员
ConstructorInfo-结构
FieldInfo-字段
MethodInfo-方法
PropertyInfo-属性
EventInfo-事件
例子一 实现简单工厂模式
传统的简单工厂模式的缺点:由于工厂类集中了所有实例的创建逻辑,
工厂类就需要判断何时创建何种种类的产品, 导致系统丧失灵活性和可维护性。
public interface IQSDB
{
void Execute();
}
public class IMPORT_JOB : IQSDB
{
public IMPORT_JOB()
{
Console.WriteLine("IMPORT_JOB");
}
public void Execute() { Console.WriteLine("Execute!"); }
}
public class DCCHECK : IQSDB
{
public DCCHECK()
{
Console.WriteLine("DCCHECK");
}
public void Execute() { }
}
public class OCEAN : IQSDB
{
public OCEAN()
{
Console.WriteLine("OCEAN");
}
public void Execute() { }
}
/// <summary>
/// //不用反射的工厂类
/// </summary>
public class QSDBFactory
{
private static QSDBFactory instance = new QSDBFactory();
public static QSDBFactory Instance
{
get { return instance; }
}
public IQSDB Create(string name)
{
switch (name)
{
case "IMPORT_JOB":
return new IMPORT_JOB();
case "DCCHECK":
return new DCCHECK();
case "OCEAN":
return new OCEAN();
default:
throw new Exception("Wrong name.");
}
}
}
/// <summary>
/// 反射工厂类
/// </summary>
public class QSDBFactoryReflection
{
private static QSDBFactoryReflection _instance = new
QSDBFactoryReflection();
public static QSDBFactoryReflection Instance
{
get { return _instance; }
}
public IQSDB Create(string Name)
{
IQSDB qsdb = null;
try
{
Type type = Type.GetType(Name, true);
qsdb = (IQSDB)Activator.CreateInstance(type);
}
catch (TypeLoadException e)
{
Console.WriteLine("exception caught - {0}", e.Message);
}
return qsdb;
}
}
winform例子
CustomerAttribute的例子
static void Main(string[] args)
{
PropertyInfo[] infos = mp.GetType().GetProperties();
string Str_TestAtrrubute = "";
object[] objDataFieldAttribute = null;
foreach (PropertyInfo info in infos)
{
objDataFieldAttribute = info.GetCustomAttributes(typeof(DataFieldAttribute), false);
if (objDataFieldAttribute != null)
{
Console.WriteLine("FieldName: "+((DataFieldAttribute)objDataFieldAttribute[0]).FieldName + " ; FieldType:" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldType);
}
}
Console.WriteLine(Str_TestAtrrubute);
Console.ReadKey();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class M_People
{
string _Pl_ID;
[DataFieldAttribute("Pl_ID", "Int")]
public string Pl_ID
{
get { return _Pl_ID; }
set { _Pl_ID = value; }
}
int _PL_Age;
[DataFieldAttribute("PL_Age", "Int")]
public int PL_Age
{
get { return _PL_Age; }
set { _PL_Age = value; }
}
string _Pl_Sex;
[DataFieldAttribute("Pl_Sex", "nvarchar")]
public string Pl_Sex
{
get { return _Pl_Sex; }
set { _Pl_Sex = value; }
}
string _Pl_LoginName;
[DataFieldAttribute("Pl_LoginName", "nvarchar")]
public string Pl_LoginName
{
get { return _Pl_LoginName; }
set { _Pl_LoginName = value; }
}
string _Pl_TrueName;
[DataFieldAttribute("Pl_TrueName", "nvarchar")]
public string Pl_TrueName
{
get { return _Pl_TrueName; }
set { _Pl_TrueName = value; }
}
string _PL_Pwd;
[DataFieldAttribute("PL_Pwd", "nvarchar")]
public string PL_Pwd
{
get { return _PL_Pwd; }
set { _PL_Pwd = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class DataFieldAttribute:System.Attribute
{
private string _FieldName;
private string _FieldType;
public DataFieldAttribute(string fieldname, string fieldtype)
{
this._FieldName = fieldname;
this._FieldType = fieldtype;
}
public string FieldName
{
get { return this._FieldName; }
set { this._FieldName = value; }
}
public string FieldType
{
get { return this._FieldType; }
set { this._FieldType = value; }
}
}
}
反射的定义:审查元数据并收集关于它的类型信息的能力。
通过反射,可以在运行时获得程序或程序集中每一个类型(包括类、结构、委托、接
口和枚举等)的成员和成员的信息。有了反射,即可对每一个类型了如指掌。另外我
还可以直接创建对象,即使这个对象的类型在编译时还不知道。
智能感应 也是一种反射
Type类的属性:
Name 数据类型名
FullName 数据类型的完全限定名(包括命名空间名)
Namespace 定义数据类型的命名空间名
IsAbstract 指示该类型是否是抽象类型
IsArray 指示该类型是否是数组
IsClass 指示该类型是否是类
IsEnum 指示该类型是否是枚举
IsInterface 指示该类型是否是接口
IsPublic 指示该类型是否是公有的
IsSealed 指示该类型是否是密封类
IsValueType 指示该类型是否是值类型
Type类的方法:
GetConstructor(), GetConstructors():返回ConstructorInfo类型,用于
取得该类的构造函数的信息
GetEvent(), GetEvents():返回EventInfo类型,用于取得该类的事件的信
息
GetField(), GetFields():返回FieldInfo类型,用于取得该类的字段(成
员变量)的信息
GetInterface(), GetInterfaces():返回InterfaceInfo类型,用于取得该
类实现的接口的信息
GetMember(), GetMembers():返回MemberInfo类型,用于取得该类的所有
成员的信息
GetMethod(), GetMethods():返回MethodInfo类型,用于取得该类的方法
的信息
GetProperty(), GetProperties():返回PropertyInfo类型,用于取得该类
的属性的信息
可以调用这些成员,其方式是调用Type的InvokeMember()方法,或者调用
MethodInfo, PropertyInfo和其他类的Invoke()方法。
Example 1
Activator.CreateInstance 方法 (Type)
使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。
1. Activator.CreateInstance (Type)
2. Activator.CreateInstance (Type, Object[])
创建无参数的构造方法和创建有参数的构造函数。
例子一 实现简单工厂模式
传统的简单工厂模式的缺点:由于工厂类集中了所有实例的创建逻辑,
工厂类就需要判断何时创建何种种类的产品, 导致系统丧失灵活性和可维护性。
Example 2
CustomerPRopery
Example 3
ORM
Example 4