在做项目时,常常需要配置一些属性的,这就需要用到反射机制,下面是项目中常用到一些基本的放射方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
namespace SKY.DEFINITION
{
/// <summary>
/// 该类是用来实现反射的
/// 范小军
/// </summary>
public class ReflectionAboutClass
{
/// <summary>
/// 构造函数
/// 传递一个对象进来
/// </summary>
/// <param name="obj"></param>
public ReflectionAboutClass()
{
this.Ltype = new List<string>();
this.Ltype2 = new List<string>();
this.LValue = new List<string>();
this.LClass = new List<string>();
this.LAttribute = new List<string>();
this.LAttribute2 = new List<string>();
this.LMethodName = new List<string>();
this.LDefaultValue = new List<string>();
}
#region 变量
/// <summary>
/// 用来保存对象中所有的属性
/// </summary>
public List<string> Ltype { get; set; }
/// <summary>
/// 用来保存对象中所有的属性2
/// </summary>
public List<string> Ltype2 { get; set; }
/// <summary>
/// 用来保存对象中所有属性的值
/// </summary>
public List<string> LValue { get; set; }
/// <summary>
/// 用来保存某个命名空间下所有的类名称
/// </summary>
public List<string> LClass { get; set; }
/// <summary>
/// 用来保存属性的描述信息
/// </summary>
public List<string> LAttribute { get; set; }
/// <summary>
/// 用来保存属性的描述信息2
/// </summary>
public List<string> LAttribute2 { get; set; }
/// <summary>
/// 用来保存一个类中所有的方法名
/// </summary>
public List<string> LMethodName { get; set; }
/// <summary>
/// 用来保存默认值的
/// </summary>
public List<string> LDefaultValue { get; set; }
#endregion
#region 方法
/// <summary>
/// 用来获取一个对象的所有属性,并保存在一个List 中
/// 并取得所有的属性,对于于obj 对象中的值
/// </summary>
/// <param name="NameSpacestr"></param>
/// 命名空间的字符串参数
/// <param name="obj"></param>
/// 对象参数
/// <returns></returns>
public void getProperty(string NameSpacestr)
{
Object obj = new object();
Type t = Type.GetType(NameSpacestr);
if (t == null) {
return;
}
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Ltype.Add(pi.Name);
//LValue.Add( pi.GetValue(obj, null).ToString());
}
}
/// <summary>
/// 获取一个属性的类型
/// </summary>
/// <param name="NameSpacestr"></param>
/// <param name="obj"></param>
/// <param name="PropertyName"></param>
/// <returns></returns>
public string getPropertyType(string NameSpacestr, string PropertyName)
{
Type t = Type.GetType(NameSpacestr);
string tempvalue = "";
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
List<string> lpropername = new List<string>();
List<string> ldefault = new List<string>();
string temp = "";
object[] attrs = pi.GetCustomAttributes(typeof(PropertyDescriptionAttribute), true);
if (attrs.Length == 1)
{
PropertyDescriptionAttribute attr = (PropertyDescriptionAttribute)attrs[0];
lpropername.Add(attr.ProperVlaue);
ldefault.Add(attr.DefaultValue.ToString());
}
for (int i = 0; i < lpropername.Count; i++) {
if (ldefault[i].Equals(PropertyName))
{
temp = lpropername[i];
}
}
if (pi.Name.Equals(temp))
{
tempvalue = pi.PropertyType.Name.ToString();
break;
}
}
return tempvalue;
}
/// <summary>
/// 根据属性的名称去取得该某个对象的该属性的值
/// </summary>
/// 该对象所在的命名空间
/// <param name="NameSpacestr"></param>
/// 取值的对象
/// <param name="obj"></param>
/// 需要取值的属性名称
/// <param name="PropertyName"></param>
/// <returns></returns>
public string getPropertyValue(string NameSpacestr, Object obj, string PropertyName)
{
//SKY.MODEL.UserModel
Type t = Type.GetType(NameSpacestr);
if (t == null) {
return "";
}
string tempvalue = "";
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (pi.Name.Equals(PropertyName))
{
tempvalue = pi.GetValue(obj, null).ToString();
break;
}
}
return tempvalue;
}
/// <summary>
/// 判断是否存在某个命名空间
/// </summary>
/// <param name="NameSpacestr"></param>
/// <returns></returns>
public bool IsExitNameSpace(string NameSpacestr)
{
Type t = Type.GetType(NameSpacestr);
if (t == null) {
return false;
}else{
return true;
}
}
/// <summary>
/// 获取某个对象的命名空间
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public string getSpaceName(Object obj)
{
Type t = obj.GetType();
if (t == null) {
return "";
}
return t.FullName.ToString();
}
/// <summary>
/// 用来获取一个类中的所有的方法
/// </summary>
/// 参数为包括命名空间的类名
/// <param name="ClassName"></param>
public void getMethodName(string ClassName)
{
Type t = Type.GetType(ClassName);
if (t == null) {
return;
}
MethodInfo[] methods = t.GetMethods();
foreach (MethodInfo method in methods)
{
LMethodName.Add(method.Name);
}
}
/// <summary>
/// 用来获取一个命名空间下所有类的名字
/// </summary>
/// 命名空间字符串作为参数
/// <param name="NameSpacestr"></param>
public void getClassName(string NameSpacestr)
{
Assembly _Assembyle = Assembly.GetAssembly(this.GetType());
Type[] _TypeList = _Assembyle.GetTypes();
for (int i = 0; i != _TypeList.Length; i++)
{
if (_TypeList[i].Namespace == NameSpacestr)
{
LClass.Add(NameSpacestr + "." + _TypeList[i].Name);
}
}
}
/// <summary>
/// 用来获取属性的描述信息
/// </summary>
/// 类名字做为参数
/// <param name="NameSpacestr"></param>
public void getAttribute(string NameSpacestr)
{
Object obj = new object();
Type t = Type.GetType(NameSpacestr);
if (t == null) {
return;
}
foreach (PropertyInfo proInfo in t.GetProperties())
{
object[] attrs = proInfo.GetCustomAttributes(typeof(PropertyDescriptionAttribute), true);
if (attrs.Length == 1)
{
PropertyDescriptionAttribute attr = (PropertyDescriptionAttribute)attrs[0];
LAttribute.Add(attr.FieldName);
if (attr.DefaultValue != null) {
LDefaultValue.Add(attr.DefaultValue.ToString());
}
}
}
}
/// <summary>
/// 用来获取属性的描述信息
/// </summary>
/// 类名字做为参数2返回List string
/// <param name="NameSpacestr"></param>
// List<string> LAttribute
public void getAttribute_Return(string NameSpacestr)
{
//List<string> LAttribute2 = new List<string>();
//List<string> LAttribute2Name = new List<string>();
Object obj = new object();
Type t = Type.GetType(NameSpacestr);
if (t == null) {
return;
}
foreach (PropertyInfo proInfo in t.GetProperties())
{
object[] attrs = proInfo.GetCustomAttributes(typeof(PropertyDescriptionAttribute), true);
if (attrs.Length == 1)
{
PropertyDescriptionAttribute attr = (PropertyDescriptionAttribute)attrs[0];
LAttribute2.Add(attr.FieldName);
Ltype2.Add(proInfo.Name);
}
}
//a.Add(LAttribute2);
//a.Add(LAttribute2Name);
}
/// <summary>
/// 用来获取属性的描述信息
/// </summary>
/// 类名字做为参数2返回List string
/// <param name="NameSpacestr"></param>
public string getAttribute_Return2(string NameSpacestr ,string Properties)
{
// Object obj = new object();
Type t = Type.GetType(NameSpacestr);
if (t == null) {
return "";
}
string FieldName = "";
foreach (PropertyInfo proInfo in t.GetProperties())
{
object[] attrs = proInfo.GetCustomAttributes(typeof(PropertyDescriptionAttribute), true);
if (attrs.Length == 1)
{
if (Properties == proInfo.Name)
{
PropertyDescriptionAttribute attr = (PropertyDescriptionAttribute)attrs[0];
FieldName = attr.FieldName;
}
}
}
return FieldName;
}
/// <summary>
/// 通过方法名字,执行BLL层中ReportDataSourse类中的方法
/// </summary>
/// <param name="methodname"></param>
public void exectueMethod(string methodname)
{
// Assembly assem = Assembly.GetExecutingAssembly();
Assembly assem = Assembly.Load("TLaura.TxBLL");
AssemblyName assemName = assem.GetName();
Object o = assem.CreateInstance("ReportDataSourse", false, BindingFlags.ExactBinding, null, new Object[] { }, null, null); try
{
MethodInfo m = assem.GetType("TLaura.TxBLL.ReportDataSourse").GetMethod(methodname);
Object ret = m.Invoke(o, new Object[] { 42, 4 });
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
#endregion
}
}