日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

反射的帮助类

發(fā)布時間:2024/9/5 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 反射的帮助类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在做項目時,常常需要配置一些屬性的,這就需要用到反射機制,下面是項目中常用到一些基本的放射方法:

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.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>/// 判斷是否存在某個命名空間/// </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> 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>/// 用來獲取屬性的描述信息/// </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>/// 通過方法名字,執(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} }

?

轉載于:https://www.cnblogs.com/fanxiaojun/archive/2012/03/08/2385688.html

總結

以上是生活随笔為你收集整理的反射的帮助类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。