浅谈.Net反射系列基本来到了尾声,本文主要从.Net Framework的源码角度去分析:
System.Reflection命名空间下定义的类型
以及Type类型
首先各个反射类型都继承于MemberInfo类,在看MemberInfo类之前,先看一下MemberTypes枚举类:
public enum MemberTypes
{
/// <summary>Specifies that the member is a constructor, representing a <see cref="T:System.Reflection.ConstructorInfo" /> member. Hexadecimal value of 0x01.</summary>
Constructor = 1,
/// <summary>Specifies that the member is an event, representing an <see cref="T:System.Reflection.EventInfo" /> member. Hexadecimal value of 0x02.</summary>
Event = 2,
/// <summary>Specifies that the member is a field, representing a <see cref="T:System.Reflection.FieldInfo" /> member. Hexadecimal value of 0x04.</summary>
Field = 4,
/// <summary>Specifies that the member is a method, representing a <see cref="T:System.Reflection.MethodInfo" /> member. Hexadecimal value of 0x08.</summary>
Method = 8,
/// <summary>Specifies that the member is a property, representing a <see cref="T:System.Reflection.PropertyInfo" /> member. Hexadecimal value of 0x10.</summary>
Property = 16,
/// <summary>Specifies that the member is a type, representing a <see cref="F:System.Reflection.MemberTypes.TypeInfo" /> member. Hexadecimal value of 0x20.</summary>
TypeInfo = 32,
/// <summary>Specifies that the member is a custom member type. Hexadecimal value of 0x40.</summary>
Custom = 64,
/// <summary>Specifies that the member is a nested type, extending <see cref="T:System.Reflection.MemberInfo" />.</summary>
NestedType = 128,
/// <summary>Specifies all member types.</summary>
All = 191
}
这里枚举了抽象类MemberInfo的各个具体的子类型。我们看一下它们OO类图:
首先我们先分析MemberInfo类。
MemberInfo类的属性:
public virtual IEnumerable<CustomAttributeData> CustomAttributes
public abstract Type DeclaringType
public abstract MemberTypes MemberType
public abstract string Name
public abstract Type ReflectedType
MemberInfo类的方法:
public override bool Equals(object obj)
public abstract object[] GetCustomAttributes(bool inherit);
public abstract object[] GetCustomAttributes(Type attributeType, bool inherit);
public virtual IList<CustomAttributeData> GetCustomAttributesData()
public abstract bool IsDefined(Type attributeType, bool inherit);
通过这些定义,我们大致知道,MemberInfo对象所代表的Member自定义特性有哪些,定义在哪个类里面,从哪个类反射的,是什么MemberType等等。
分析Type类。
属性有:
public abstract Assembly Assembly
public TypeAttributes Attributes
public abstract Type BaseType
public virtual bool ContainsGenericParameters
public virtual MethodBase DeclaringMethod
public override Type DeclaringType
public abstract string FullName
public virtual Type[] GenericTypeArguments
public bool IsAbstract
public bool IsArray
public bool IsByRef
public bool IsClass
public bool IsInterface
public virtual bool IsEnum
public virtual bool IsGenericParameter
public virtual bool IsGenericType
public bool IsMarshalByRef
public bool IsNested
public bool IsNotPublic
public bool IsPointer
public bool IsPrimitive
public bool IsPublic
public bool IsSealed
public virtual bool IsSerializable
public bool IsSpecialName
public bool IsValueType
public bool IsVisible
public override MemberTypes MemberType
public abstract string Namespace
public override Type ReflectedType
方法有:
public override bool Equals(object o)
public virtual Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
public virtual MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
public ConstructorInfo GetConstructor(Type[] types)
public ConstructorInfo[] GetConstructors()
public virtual MemberInfo[] GetDefaultMembers()
public virtual string GetEnumName(object value)
public EventInfo GetEvent(string name)
public virtual EventInfo[] GetEvents()
public FieldInfo GetField(string name)
public FieldInfo[] GetFields()
public virtual Type[] GetGenericArguments()
public virtual Type[] GetGenericParameterConstraints()
public Type GetInterface(string name)
public abstract Type[] GetInterfaces();
public MemberInfo[] GetMember(string name)
public MemberInfo[] GetMembers()
public MethodInfo GetMethod(string name)
public MethodInfo GetMethod(string name, Type[] types)
public MethodInfo[] GetMethods()
public abstract MethodInfo[] GetMethods(BindingFlags bindingAttr);
public Type GetNestedType(string name)
public Type[] GetNestedTypes()
public PropertyInfo[] GetProperties()
public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr);
public PropertyInfo GetProperty(string name)
public PropertyInfo GetProperty(string name, Type returnType)
public static Type GetType(string typeName)
public new Type GetType()
public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
public virtual bool IsEnumDefined(object value)
public virtual bool IsEquivalentTo(Type other)
public virtual bool IsInstanceOfType(object o)
public virtual bool IsSubclassOf(Type c)
public override string ToString()
通过上面的定义,从Type对象代表的是一个类,以类为第一视角去定义了很多属性和方法。
分析FieldInfo类。
属性:
public override MemberTypes MemberType
public abstract FieldAttributes Attributes
public abstract Type FieldType
public bool IsNotSerialized
public bool IsPrivate
public bool IsPublic
public bool IsSpecialName
public bool IsStatic
方法:
public virtual object GetRawConstantValue()
public abstract object GetValue(object obj)
public void SetValue(object obj, object value)
通过上面的定义,从FieldInfo对象代表的是一个字段,以字段为第一视角去定义了很多属性和方法。
分析PropertyInfo类。
属性:
public abstract PropertyAttributes Attributes
public abstract bool CanRead
public abstract bool CanWrite
public virtual MethodInfo GetMethod
public virtual MethodInfo SetMethod
public bool IsSpecialName
public override MemberTypes MemberType
public abstract Type PropertyType
方法:
public MethodInfo[] GetAccessors()
public virtual object GetConstantValue()
public virtual object GetRawConstantValue()
public abstract MethodInfo GetGetMethod(bool nonPublic);
public MethodInfo GetGetMethod()
public abstract MethodInfo GetSetMethod(bool nonPublic);
public MethodInfo GetSetMethod()
public object GetValue(object obj)
public virtual object GetValue(object obj, object[] index)
public void SetValue(object obj, object value)
分析MethodBase类。
属性:
public abstract MethodAttributes Attributes
public virtual bool ContainsGenericParameters
public bool IsAbstract
public bool IsConstructor
public bool IsFinal
public virtual bool IsGenericMethod
public bool IsPrivate
public bool IsPublic
public bool IsSpecialName
public bool IsStatic
public bool IsVirtual
方法:
public static MethodBase GetCurrentMethod()
public virtual Type[] GetGenericArguments()
public virtual MethodBody GetMethodBody()
public abstract ParameterInfo[] GetParameters();
public object Invoke(object obj, object[] parameters)
分析ConstructorInfo类。
public readonly static string ConstructorName;
public readonly static string TypeConstructorName;
public override MemberTypes MemberType
public object Invoke(object[] parameters)
分析MethodInfo类。
public override MemberTypes MemberType
public virtual ParameterInfo ReturnParameter
public virtual Type ReturnType
public abstract MethodInfo GetBaseDefinition();
public override Type[] GetGenericArguments()
public virtual MethodInfo GetGenericMethodDefinition()
public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments)
分析ParameterInfo类。
public virtual ParameterAttributes Attributes
public virtual IEnumerable<CustomAttributeData> CustomAttributes
public virtual bool HasDefaultValue
public virtual object DefaultValue
public bool IsIn
public bool IsOptional
public bool IsOut
public virtual MemberInfo Member
public virtual string Name
public virtual Type ParameterType
public virtual int Position
public virtual object RawDefaultValue
public virtual object[] GetCustomAttributes(bool inherit)
public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
public virtual IList<CustomAttributeData> GetCustomAttributesData()
public virtual bool IsDefined(Type attributeType, bool inherit)
分析EventInfo类。
public virtual MethodInfo AddMethod
public abstract EventAttributes Attributes
public bool IsSpecialName
public override MemberTypes MemberType
public virtual MethodInfo RemoveMethod
public abstract MethodInfo GetAddMethod(bool nonPublic);
public abstract MethodInfo GetRemoveMethod(bool nonPublic);
浅谈.Net反射系列全部结束