反射的帮助类
在做項(xiàng)目時(shí),常常需要配置一些屬性的,這就需要用到反射機(jī)制,下面是項(xiàng)目中常用到一些基本的放射方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Collections;namespace SKY.DEFINITION {/// <summary>/// 該類是用來(lái)實(shí)現(xiàn)反射的/// 范小軍/// </summary>public class ReflectionAboutClass{/// <summary>/// 構(gòu)造函數(shù)/// 傳遞一個(gè)對(duì)象進(jìn)來(lái)/// </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>/// 用來(lái)保存對(duì)象中所有的屬性/// </summary>public List<string> Ltype { get; set; }/// <summary>/// 用來(lái)保存對(duì)象中所有的屬性2/// </summary>public List<string> Ltype2 { get; set; }/// <summary>/// 用來(lái)保存對(duì)象中所有屬性的值/// </summary>public List<string> LValue { get; set; }/// <summary>/// 用來(lái)保存某個(gè)命名空間下所有的類名稱/// </summary>public List<string> LClass { get; set; }/// <summary>/// 用來(lái)保存屬性的描述信息/// </summary>public List<string> LAttribute { get; set; }/// <summary>/// 用來(lái)保存屬性的描述信息2/// </summary>public List<string> LAttribute2 { get; set; }/// <summary>/// 用來(lái)保存一個(gè)類中所有的方法名/// </summary>public List<string> LMethodName { get; set; }/// <summary>/// 用來(lái)保存默認(rèn)值的/// </summary>public List<string> LDefaultValue { get; set; }#endregion#region 方法/// <summary>/// 用來(lái)獲取一個(gè)對(duì)象的所有屬性,并保存在一個(gè)List 中/// 并取得所有的屬性,對(duì)于于obj 對(duì)象中的值/// </summary>/// <param name="NameSpacestr"></param>/// 命名空間的字符串參數(shù)/// <param name="obj"></param>/// 對(duì)象參數(shù)/// <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>/// 獲取一個(gè)屬性的類型/// </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>/// 根據(jù)屬性的名稱去取得該某個(gè)對(duì)象的該屬性的值/// </summary>/// 該對(duì)象所在的命名空間/// <param name="NameSpacestr"></param>/// 取值的對(duì)象/// <param name="obj"></param>/// 需要取值的屬性名稱/// <param name="PropertyName"></param> /// <returns></returns>public string getPropertyValue(string NameSpacestr, Object obj, string PropertyName){//SKY.MODEL.UserModelType 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>/// 判斷是否存在某個(gè)命名空間/// </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>/// 獲取某個(gè)對(duì)象的命名空間/// </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>/// 用來(lái)獲取一個(gè)類中的所有的方法/// </summary>/// 參數(shù)為包括命名空間的類名/// <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>/// 用來(lái)獲取一個(gè)命名空間下所有類的名字/// </summary>/// 命名空間字符串作為參數(shù)/// <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>/// 用來(lái)獲取屬性的描述信息/// </summary>/// 類名字做為參數(shù)/// <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>/// 用來(lái)獲取屬性的描述信息/// </summary>/// 類名字做為參數(shù)2返回List string/// <param name="NameSpacestr"></param>// List<string> LAttributepublic 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>/// 用來(lái)獲取屬性的描述信息/// </summary>/// 類名字做為參數(shù)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>/// 通過(guò)方法名字,執(zhí)行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} }?
轉(zhuǎn)載于:https://www.cnblogs.com/fanxiaojun/archive/2012/03/08/2385688.html
總結(jié)
- 上一篇: ubuntu apt 相关命令
- 下一篇: [转] Oracle中Blob转Clob