一:
要用反射需要知道 类全名,程序集 类全名(名称空间加类名)
程序集就是右键项目属性可以看到程序集名称
默认和项目名称,命名空间一样
例如:Jd.xxx.bbbbI.Three_groupthree_f,Jd
通过字符串创建对象方法: IisWin iswin = Activator.CreateInstance(Type.GetType(“类全名,程序集”)) as IisWin; //得到相应类型的对象
/// <summary>
/// 复制对象的属性
/// </summary>
public static void CopyProperties(this object _m, object _o)
{
foreach (PropertyInfo item in _o.GetType().GetProperties())
{
try
{
object val = _m.GetType().GetProperty(item.Name).GetValue(_m);
item.SetValue(_o, val);
}
catch { }
}
}
二:更具类型创建对象
public T CreateTByType<T>()
{
Type type = typeof(T);
T model = (T)Activator.CreateInstance(type);
return model;
}
获得类型
Type ts = typeof(UniteAttribute);
/// <summary>
/// 把从数据库读取到的值赋值到传的泛型里
/// </summary>
/// <param name="piArr"></param>
/// <param name="model"></param>
/// <param name="reader"></param>
protected static void GetReaderValueToModel(PropertyInfo[] piArr, T model, SqlDataReader reader)
{
foreach (PropertyInfo pi in piArr)
{
var proName = pi.Name;
try
{
pi.SetValue(model, reader[proName], null);//数据库取出来的值反射赋值给model可以不进行类型转化,因为他默认拆箱成字段相应类型
}
catch (Exception)
{
}
}
}
获取属性类型
item.PropertyType
三: 反射取特性
//反射取特性
TableAttribute dbResult = Attribute.GetCustomAttribute(typeof(T), typeof(TableAttribute), true) as TableAttribute;
foreach (PropertyInfo p in mx.GetType().GetProperties())
{
FieldAttribute tables = (FieldAttribute)p.GetCustomAttributes(false)[0];
MessageBox.Show(tables.Name + "类型" + tables.DbType);
}
获得指定类型的特性
ColumnAttribute cols = pi.GetCustomAttribute<ColumnAttribute>(true);//取特性
public object[] GetThatAttribute<T>(string methodname, Type t)
{
return t.GetMethod(methodname).GetCustomAttributes(typeof(T), true);
}
四:反射调用方法
反射调用参数为request的方法
Type tm = Type.GetType(classname);
object obj = Activator.CreateInstance(tm);
Activator.CreateInstance(tm).GetType().GetMethod(methodname).Invoke(obj, new Object[] { filterContext.HttpContext.Request });
方法定义:
public void Test(HttpRequestBase _request)
{
string aa = _request["aa"];
string bb = _request["bb"];
}
访问与调用私有方法
//访问私有方法
foreach (MethodInfo m in t.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic))
{
Console.WriteLine("方法名{0}", m.Name);
}
//调用私有方法
t.GetMethod("Say",BindingFlags.Instance|BindingFlags.NonPublic).Invoke(p,new object[]{"hello"});
private void Say(string _name)
{
Console.WriteLine("我被调用了:" + _name);
}
五:反射访问私有字段给私有字段赋值
给私有字段赋值
public static void SetPrivateProperty(this object instance, string propertyname, object value)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo field = type.GetProperty(propertyname, flag);
field.SetValue(instance, value, null);
}
访问私有字段
foreach (PropertyInfo mi in t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic))//访问私有成员
{
Console.WriteLine("{0}\t{1}\t{2}", mi.PropertyType, mi.Name, mi.GetValue(p));
}
六:反射判断是否为某个可空类型
lottery_type ad = new lottery_type();
Type type = ad.GetType();//获取某个类的类型
foreach (PropertyInfo item in type.GetProperties())
{
string pname = item.Name;//拿到属性名
string ptype = item.PropertyType.Name;//拿到属性类型的名称
//验证是否为可空类型 int?
if (item.PropertyType == typeof(int?))
{
}
//验证是否为可空类型 DateTime?
if (item.PropertyType == typeof(DateTime?))
{
}
}
反射简单运用:
http://blog.csdn.net/aojiancc2/article/details/50999104