在.net框架體系內(nèi),反射特性較多的應(yīng)用到。反射的相關(guān)定義分為兩種。
成都創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的太谷網(wǎng)站建設(shè)公司,太谷接單;提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行太谷網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
自然解釋?zhuān)荷涫且环N自然現(xiàn)象,表現(xiàn)為受刺激物對(duì)刺激物的逆反應(yīng);這是反射的字面解釋?zhuān)覀兛匆幌掠?jì)算機(jī)編程中的反射;
編程解釋?zhuān)和ㄟ^(guò) System.Reflection 命名空間中的類(lèi)以及 System.Type,您可以獲取有關(guān)已加載的程序集和在其中定義的類(lèi)型(如類(lèi)、接口和值類(lèi)型)的信息。 您也可以使用反射在運(yùn)行時(shí)創(chuàng)建類(lèi)型實(shí)例,以及調(diào)用和訪問(wèn)這些實(shí)。
反射(Reflection)有下列用途:它允許在運(yùn)行時(shí)查看屬性(attribute)信息;它允許審查集合中的各種類(lèi)型,以及實(shí)例化這些類(lèi)型;它允許延遲綁定的方法和屬性(property);它允許在運(yùn)行時(shí)創(chuàng)建新類(lèi)型,然后使用這些類(lèi)型執(zhí)行一些任務(wù)。
下面介紹一下有關(guān)反射的程序集的相關(guān)屬性和方法的源碼:
(1).Object的GetType()方法:
// Returns a Type object which represent this object instance. // [System.Security.SecuritySafeCritical] // auto-generated [Pure] [ResourceExposure(ResourceScope.None)] [MethodImplAttribute(MethodImplOptions.InternalCall)] public extern Type GetType();
(2).PropertyInfo的GetProperty()方法:
public PropertyInfo GetProperty(String name,BindingFlags bindingAttr,Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) { if (name == null) throw new ArgumentNullException("name"); if (types == null) throw new ArgumentNullException("types"); Contract.EndContractBlock(); return GetPropertyImpl(name,bindingAttr,binder,returnType,types,modifiers); } public PropertyInfo GetProperty(String name, Type returnType, Type[] types,ParameterModifier[] modifiers) { if (name == null) throw new ArgumentNullException("name"); if (types == null) throw new ArgumentNullException("types"); Contract.EndContractBlock(); return GetPropertyImpl(name,Type.DefaultLookup,null,returnType,types,modifiers); } public PropertyInfo GetProperty(String name, BindingFlags bindingAttr) { if (name == null) throw new ArgumentNullException("name"); Contract.EndContractBlock(); return GetPropertyImpl(name,bindingAttr,null,null,null,null); } public PropertyInfo GetProperty(String name, Type returnType, Type[] types) { if (name == null) throw new ArgumentNullException("name"); if (types == null) throw new ArgumentNullException("types"); Contract.EndContractBlock(); return GetPropertyImpl(name,Type.DefaultLookup,null,returnType,types,null); } public PropertyInfo GetProperty(String name, Type[] types) { if (name == null) throw new ArgumentNullException("name"); if (types == null) throw new ArgumentNullException("types"); Contract.EndContractBlock(); return GetPropertyImpl(name,Type.DefaultLookup,null,null,types,null); } public PropertyInfo GetProperty(String name, Type returnType) { if (name == null) throw new ArgumentNullException("name"); if (returnType == null) throw new ArgumentNullException("returnType"); Contract.EndContractBlock(); return GetPropertyImpl(name,Type.DefaultLookup,null,returnType,null,null); } internal PropertyInfo GetProperty(String name, BindingFlags bindingAttr, Type returnType) { if (name == null) throw new ArgumentNullException("name"); if (returnType == null) throw new ArgumentNullException("returnType"); Contract.EndContractBlock(); return GetPropertyImpl(name, bindingAttr, null, returnType, null, null); } public PropertyInfo GetProperty(String name) { if (name == null) throw new ArgumentNullException("name"); Contract.EndContractBlock(); return GetPropertyImpl(name,Type.DefaultLookup,null,null,null,null); }
(3).Object的GetValue()方法:
[DebuggerStepThroughAttribute] [Diagnostics.DebuggerHidden] public Object GetValue(Object obj) { return GetValue(obj, null); } [DebuggerStepThroughAttribute] [Diagnostics.DebuggerHidden] public virtual Object GetValue(Object obj,Object[] index) { return GetValue(obj, BindingFlags.Default, null, index, null); } public abstract Object GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture);
以上介紹了一下有關(guān)反射的相關(guān)方法的底層方法源碼,現(xiàn)在介紹一下較為通用的方法:
(1).獲取對(duì)象的所有公共屬性。
/// <summary> /// 獲取對(duì)象的所有公共屬性。 /// </summary> /// <param name="obj">定義該方法的數(shù)據(jù)類(lèi)型。</param> /// <returns>返回包含該對(duì)象的屬性信息的數(shù)組。</returns> public static IEnumerable<PropertyInfo> GetProperties(this object obj) { return obj.GetType().GetProperties(); }
(2).獲取一個(gè)對(duì)象的屬性。
/// <summary> ///獲取一個(gè)對(duì)象的屬性。 /// </summary> /// <param name="obj">定義該方法的數(shù)據(jù)類(lèi)型。gb</param> /// <param name="flags">提供要確定要檢索的屬性的標(biāo)志。</param> /// <returns>返回包含該對(duì)象的屬性信息的數(shù)組。</returns> public static IEnumerable<PropertyInfo> GetProperties(this object obj, BindingFlags flags) { return obj.GetType().GetProperties(flags); }
(3).用指定名稱(chēng)獲取具有指定名稱(chēng)的屬性的當(dāng)前對(duì)象的屬性值。新航道培訓(xùn)
/// <summary> ///用指定名稱(chēng)獲取具有指定名稱(chēng)的屬性的當(dāng)前對(duì)象的屬性值。 /// </summary> /// <param name="obj">要檢索的屬性值的對(duì)象。</param> /// <param name="propertyName">要檢索的屬性的名稱(chēng)。</param> /// <returns>返回屬性的值。</returns> public static object GetPropertyValue(this object obj, string propertyName) { var item = obj.GetType().GetProperty(propertyName); if (item == null) return null; var value = obj.GetType().GetProperty(propertyName).GetValue(obj); if (item.PropertyType.IsGenericType) { value = item.PropertyType.GetProperty(propertyName); } return value; }
(4).獲取一個(gè)枚舉字符串值。
/// <summary> ///獲取一個(gè)枚舉字符串值。 /// </summary> /// <param name="obj">該枚舉返回的字符串值。</param> /// <returns>返回一個(gè)枚舉字符串值。</returns> public static string GetStringValue(this System.Enum obj) { var fieldInfo = obj.GetType().GetField(obj.ToString()); var attributes = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[]; var output = (StringValueAttribute)attributes.GetValue(0); return output.Text; }
(5).獲取方法調(diào)用。
/// <summary> /// 獲取方法調(diào)用 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="action"></param> /// <returns></returns> public static MethodCallExpression GetMethodCall<T>(Expression<T> action ) { var call = action.Body as MethodCallExpression; return call; }
(6).獲取類(lèi)型名稱(chēng).
/// <summary> /// 獲取類(lèi)型名稱(chēng) /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static string GetTypeName<T>() { return typeof (T).Name; }
(7).獲取參數(shù)值
/// <summary> /// 獲取參數(shù)值 /// </summary> /// <param name="methodCall"></param> /// <returns></returns> public static IEnumerable<Tuple<ParameterInfo, object>> GetArgumentValues(MethodCallExpression methodCall) { var parameters = methodCall.Method.GetParameters(); if (!parameters.Any()) yield break; for(var i = 0; i < parameters.Length; i++) { var arg = methodCall.Arguments[i]; var ceValue = arg as ConstantExpression; if (ceValue != null) yield return new Tuple<ParameterInfo, object>(parameters[i], ceValue.Value); else yield return new Tuple<ParameterInfo, object>(parameters[i], GetExpressionValue(arg)); } }
(8).獲取表達(dá)式值
/// <summary> /// 獲取表達(dá)式值 /// </summary> /// <param name="expression"></param> /// <returns></returns> private static object GetExpressionValue(Expression expression) { var lambda = Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof (object))); var func = lambda.Compile(); return func(); }
反射類(lèi)的繼承層次如下:
System.reflection
System.Reflection.Assembly
System.Reflection.MemberInfo
System.Reflection.EventInfo
System.Reflection.FieldInfo
System.Reflection.MethodBase
System.Reflection.ConstructorInfo
System.Reflection.MethodInfo
System.Reflection.PropertyInfo
System.Type
標(biāo)題名稱(chēng):淺析.NET的反射特性
新聞來(lái)源:http://muchs.cn/article6/ihspog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站維護(hù)、網(wǎng)站內(nèi)鏈、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)