欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

淺析.NET的反射特性

    在.net框架體系內(nèi),反射特性較多的應用到。反射的相關(guān)定義分為兩種。

成都創(chuàng)新互聯(lián)公司是專業(yè)的太谷網(wǎng)站建設公司,太谷接單;提供成都網(wǎng)站建設、成都做網(wǎng)站,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行太谷網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

     自然解釋:射是一種自然現(xiàn)象,表現(xiàn)為受刺激物對刺激物的逆反應;這是反射的字面解釋,我們看一下計算機編程中的反射;

     編程解釋:通過 System.Reflection 命名空間中的類以及 System.Type,您可以獲取有關(guān)已加載的程序集和在其中定義的類型(如類、接口和值類型)的信息。 您也可以使用反射在運行時創(chuàng)建類型實例,以及調(diào)用和訪問這些實。

     反射(Reflection)有下列用途:它允許在運行時查看屬性(attribute)信息;它允許審查集合中的各種類型,以及實例化這些類型;它允許延遲綁定的方法和屬性(property);它允許在運行時創(chuàng)建新類型,然后使用這些類型執(zhí)行一些任務。

     下面介紹一下有關(guān)反射的程序集的相關(guān)屬性和方法的源碼:

        (1).Object的GetType()方法:

淺析.NET的反射特性

    // 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();

淺析.NET的反射特性

      (2).PropertyInfo的GetProperty()方法:

淺析.NET的反射特性

 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);
        }

淺析.NET的反射特性

   (3).Object的GetValue()方法:

淺析.NET的反射特性

[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);

淺析.NET的反射特性

  以上介紹了一下有關(guān)反射的相關(guān)方法的底層方法源碼,現(xiàn)在介紹一下較為通用的方法:

    (1).獲取對象的所有公共屬性。

淺析.NET的反射特性

        /// <summary>
        /// 獲取對象的所有公共屬性。        /// </summary>
        /// <param name="obj">定義該方法的數(shù)據(jù)類型。</param>
        /// <returns>返回包含該對象的屬性信息的數(shù)組。</returns>
        public static IEnumerable<PropertyInfo> GetProperties(this object obj)
        {            return obj.GetType().GetProperties();
        }

淺析.NET的反射特性

    (2).獲取一個對象的屬性。

淺析.NET的反射特性

        /// <summary>
        ///獲取一個對象的屬性。        /// </summary>
        /// <param name="obj">定義該方法的數(shù)據(jù)類型。gb</param>
        /// <param name="flags">提供要確定要檢索的屬性的標志。</param>
        /// <returns>返回包含該對象的屬性信息的數(shù)組。</returns>
        public static IEnumerable<PropertyInfo> GetProperties(this object obj, BindingFlags flags)
        {            return obj.GetType().GetProperties(flags);
        }

淺析.NET的反射特性

    (3).用指定名稱獲取具有指定名稱的屬性的當前對象的屬性值。新航道培訓

淺析.NET的反射特性

        /// <summary>
        ///用指定名稱獲取具有指定名稱的屬性的當前對象的屬性值。        /// </summary>
        /// <param name="obj">要檢索的屬性值的對象。</param>
        /// <param name="propertyName">要檢索的屬性的名稱。</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;
        }

淺析.NET的反射特性

     (4).獲取一個枚舉字符串值。

淺析.NET的反射特性

        /// <summary>
        ///獲取一個枚舉字符串值。        /// </summary>
        /// <param name="obj">該枚舉返回的字符串值。</param>
        /// <returns>返回一個枚舉字符串值。</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;
        }

淺析.NET的反射特性

     (5).獲取方法調(diào)用。

淺析.NET的反射特性

        /// <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;
        }

淺析.NET的反射特性

    (6).獲取類型名稱.

淺析.NET的反射特性

        /// <summary>
        /// 獲取類型名稱        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetTypeName<T>()
        {            return typeof (T).Name;
        }

淺析.NET的反射特性

    (7).獲取參數(shù)值

淺析.NET的反射特性

        /// <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));
            }
        }

淺析.NET的反射特性

    (8).獲取表達式值

淺析.NET的反射特性

        /// <summary>
        /// 獲取表達式值        /// </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();
        }

淺析.NET的反射特性

    反射類的繼承層次如下:

      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   

 

標題名稱:淺析.NET的反射特性
新聞來源:http://chinadenli.net/article6/ihspog.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站維護、網(wǎng)站內(nèi)鏈、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管