网站建设资讯

NEWS

网站建设资讯

浅析.NET的反射特性-创新互联

   在.net框架体系内,反射特性较多的应用到。反射的相关定义分为两种。

创新互联专注于企业全网营销推广、网站重做改版、五龙口网站定制设计、自适应品牌网站建设、H5建站商城建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为五龙口等各大城市提供网站开发制作服务。

   自然解释:射是一种自然现象,表现为受刺激物对刺激物的逆反应;这是反射的字面解释,我们看一下计算机编程中的反射;

   编程解释:通过 System.Reflection 命名空间中的类以及 System.Type,您可以获取有关已加载的程序集和在其中定义的类型(如类、接口和值类型)的信息。 您也可以使用反射在运行时创建类型实例,以及调用和访问这些实。

     反射(Reflection)有下列用途:它允许在运行时查看属性(attribute)信息;它允许审查集合中的各种类型,以及实例化这些类型;它允许延迟绑定的方法和属性(property);它允许在运行时创建新类型,然后使用这些类型执行一些任务。

   下面介绍一下有关反射的程序集的相关属性和方法的源码:

      (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的反射特性

以上介绍了一下有关反射的相关方法的底层方法源码,现在介绍一下较为通用的方法:

  (1).获取对象的所有公共属性。

浅析.NET的反射特性

        /// 
        /// 获取对象的所有公共属性。        /// 
        /// 定义该方法的数据类型。
        /// 返回包含该对象的属性信息的数组。
        public static IEnumerable GetProperties(this object obj)
        {            return obj.GetType().GetProperties();
        }

浅析.NET的反射特性

  (2).获取一个对象的属性。

浅析.NET的反射特性

        /// 
        ///获取一个对象的属性。        /// 
        /// 定义该方法的数据类型。gb
        /// 提供要确定要检索的属性的标志。
        /// 返回包含该对象的属性信息的数组。
        public static IEnumerable GetProperties(this object obj, BindingFlags flags)
        {            return obj.GetType().GetProperties(flags);
        }

浅析.NET的反射特性

  (3).用指定名称获取具有指定名称的属性的当前对象的属性值。新航道培训

浅析.NET的反射特性

        /// 
        ///用指定名称获取具有指定名称的属性的当前对象的属性值。        /// 
        /// 要检索的属性值的对象。
        /// 要检索的属性的名称。
        /// 返回属性的值。
        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的反射特性

        /// 
        ///获取一个枚举字符串值。        /// 
        /// 该枚举返回的字符串值。
        /// 返回一个枚举字符串值。
        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).获取方法调用。

浅析.NET的反射特性

        /// 
        /// 获取方法调用        /// 
        /// 
        /// 
        /// 
        public static MethodCallExpression GetMethodCall(Expression  action )
        {            var call = action.Body as MethodCallExpression;            return call;
        }

浅析.NET的反射特性

  (6).获取类型名称.

浅析.NET的反射特性

        /// 
        /// 获取类型名称        /// 
        /// 
        /// 
        public static string GetTypeName()
        {            return typeof (T).Name;
        }

浅析.NET的反射特性

  (7).获取参数值

浅析.NET的反射特性

        /// 
        /// 获取参数值        /// 
        /// 
        /// 
        public static IEnumerable> 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(parameters[i], ceValue.Value);                else
                    yield return new Tuple(parameters[i], GetExpressionValue(arg));
            }
        }

浅析.NET的反射特性

  (8).获取表达式值

浅析.NET的反射特性

        /// 
        /// 获取表达式值        /// 
        /// 
        /// 
        private static object GetExpressionValue(Expression expression)
        {            var lambda = Expression.Lambda>(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   

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网页名称:浅析.NET的反射特性-创新互联
链接分享:http://cdweb.net/article/docsci.html