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

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

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

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

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

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

//ReturnsaTypeobjectwhichrepresentthisobjectinstance.//[System.Security.SecuritySafeCritical]//auto-generated[Pure][ResourceExposure(ResourceScope.None)][MethodImplAttribute(MethodImplOptions.InternalCall)]publicexternTypeGetType();

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

publicPropertyInfoGetProperty(Stringname,BindingFlagsbindingAttr,Binderbinder,TypereturnType,Type[]types,ParameterModifier[]modifiers){if(name==null)thrownewArgumentNullException("name");if(types==null)thrownewArgumentNullException("types");Contract.EndContractBlock();returnGetPropertyImpl(name,bindingAttr,binder,returnType,types,modifiers);}publicPropertyInfoGetProperty(Stringname,TypereturnType,Type[]types,ParameterModifier[]modifiers){if(name==null)thrownewArgumentNullException("name");if(types==null)thrownewArgumentNullException("types");Contract.EndContractBlock();returnGetPropertyImpl(name,Type.DefaultLookup,null,returnType,types,modifiers);}publicPropertyInfoGetProperty(Stringname,BindingFlagsbindingAttr){if(name==null)thrownewArgumentNullException("name");Contract.EndContractBlock();returnGetPropertyImpl(name,bindingAttr,null,null,null,null);}publicPropertyInfoGetProperty(Stringname,TypereturnType,Type[]types){if(name==null)thrownewArgumentNullException("name");if(types==null)thrownewArgumentNullException("types");Contract.EndContractBlock();returnGetPropertyImpl(name,Type.DefaultLookup,null,returnType,types,null);}publicPropertyInfoGetProperty(Stringname,Type[]types){if(name==null)thrownewArgumentNullException("name");if(types==null)thrownewArgumentNullException("types");Contract.EndContractBlock();returnGetPropertyImpl(name,Type.DefaultLookup,null,null,types,null);}publicPropertyInfoGetProperty(Stringname,TypereturnType){if(name==null)thrownewArgumentNullException("name");if(returnType==null)thrownewArgumentNullException("returnType");Contract.EndContractBlock();returnGetPropertyImpl(name,Type.DefaultLookup,null,returnType,null,null);}internalPropertyInfoGetProperty(Stringname,BindingFlagsbindingAttr,TypereturnType){if(name==null)thrownewArgumentNullException("name");if(returnType==null)thrownewArgumentNullException("returnType");Contract.EndContractBlock();returnGetPropertyImpl(name,bindingAttr,null,returnType,null,null);}publicPropertyInfoGetProperty(Stringname){if(name==null)thrownewArgumentNullException("name");Contract.EndContractBlock();returnGetPropertyImpl(name,Type.DefaultLookup,null,null,null,null);}

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

[DebuggerStepThroughAttribute][Diagnostics.DebuggerHidden]publicObjectGetValue(Objectobj){returnGetValue(obj,null);}[DebuggerStepThroughAttribute][Diagnostics.DebuggerHidden]publicvirtualObjectGetValue(Objectobj,Object[]index){returnGetValue(obj,BindingFlags.Default,null,index,null);}publicabstractObjectGetValue(Objectobj,BindingFlagsinvokeAttr,Binderbinder,Object[]index,CultureInfoculture);

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

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

///<summary>///获取对象的所有公共属性。///</summary>///<paramname="obj">定义该方法的数据类型。</param>///<returns>返回包含该对象的属性信息的数组。</returns>publicstaticIEnumerable<PropertyInfo>GetProperties(thisobjectobj){returnobj.GetType().GetProperties();}

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

///<summary>///获取一个对象的属性。///</summary>///<paramname="obj">定义该方法的数据类型。gb</param>///<paramname="flags">提供要确定要检索的属性的标志。</param>///<returns>返回包含该对象的属性信息的数组。</returns>publicstaticIEnumerable<PropertyInfo>GetProperties(thisobjectobj,BindingFlagsflags){returnobj.GetType().GetProperties(flags);}

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

///<summary>///用指定名称获取具有指定名称的属性的当前对象的属性值。///</summary>///<paramname="obj">要检索的属性值的对象。</param>///<paramname="propertyName">要检索的属性的名称。</param>///<returns>返回属性的值。</returns>publicstaticobjectGetPropertyValue(thisobjectobj,stringpropertyName){varitem=obj.GetType().GetProperty(propertyName);if(item==null)returnnull;varvalue=obj.GetType().GetProperty(propertyName).GetValue(obj);if(item.PropertyType.IsGenericType){value=item.PropertyType.GetProperty(propertyName);}returnvalue;}

(4).获取一个枚举字符串值。

///<summary>///获取一个枚举字符串值。///</summary>///<paramname="obj">该枚举返回的字符串值。</param>///<returns>返回一个枚举字符串值。</returns>publicstaticstringGetStringValue(thisSystem.Enumobj){varfieldInfo=obj.GetType().GetField(obj.ToString());varattributes=fieldInfo.GetCustomAttributes(typeof(StringValueAttribute),false)asStringValueAttribute[];varoutput=(StringValueAttribute)attributes.GetValue(0);returnoutput.Text;}

(5).获取方法调用。

///<summary>///获取方法调用///</summary>///<typeparamname="T"></typeparam>///<paramname="action"></param>///<returns></returns>publicstaticMethodCallExpressionGetMethodCall<T>(Expression<T>action){varcall=action.BodyasMethodCallExpression;returncall;}

(6).获取类型名称.

///<summary>///获取类型名称///</summary>///<typeparamname="T"></typeparam>///<returns></returns>publicstaticstringGetTypeName<T>(){returntypeof(T).Name;}

(7).获取参数值

///<summary>///获取参数值///</summary>///<paramname="methodCall"></param>///<returns></returns>publicstaticIEnumerable<Tuple<ParameterInfo,object>>GetArgumentValues(MethodCallExpressionmethodCall){varparameters=methodCall.Method.GetParameters();if(!parameters.Any())yieldbreak;for(vari=0;i<parameters.Length;i++){vararg=methodCall.Arguments[i];varceValue=argasConstantExpression;if(ceValue!=null)yieldreturnnewTuple<ParameterInfo,object>(parameters[i],ceValue.Value);elseyieldreturnnewTuple<ParameterInfo,object>(parameters[i],GetExpressionValue(arg));}}

(8).获取表达式值

///<summary>///获取表达式值///</summary>///<paramname="expression"></param>///<returns></returns>privatestaticobjectGetExpressionValue(Expressionexpression){varlambda=Expression.Lambda<Func<object>>(Expression.Convert(expression,typeof(object)));varfunc=lambda.Compile();returnfunc();}

反射类的继承层次如下:

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