专栏首页明丰随笔浅谈.Net反射 11

浅谈.Net反射 11

浅谈.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反射系列全部结束

本文分享自微信公众号 - 明丰随笔(liumingfengwx2),作者:刘明丰

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-07-27

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • IOC编程

    2. 开放/封闭原则: 添加任何新行为,应该是扩展到新类中,而不应该直接修改原来运行良好的代码。

    小蜜蜂
  • 浅谈.Net反射 4

    System.Reflection命名空间下的Assembly类型,代表了一个程序集,并包含了关于程序集的信息。

    小蜜蜂
  • AOP编程

    Aspect Oriented Programming(AOP),面向切面编程。AOP主要解决的问题是针对业务处理过程中对一些逻辑进行切面提取,它可以分散在处理...

    小蜜蜂
  • 23种设计模式详解(六)

    南风
  • (52) 抽象容器类 / 计算机程序的思维逻辑

    查看历史文章,请点击上方链接关注公众号。 从38节到51节,我们介绍的都是具体的容器类,上节我们提到,所有具体容器类其实都不是从头构建的,它们都继承了一些抽象容...

    swiftma
  • 自己动手写区块链-发起一笔交易(Java版)

    本文我们将会做以下事情: 1、创建一个钱包(wallet)。 2、使用我们的前面创建的区块链发送一笔签名的交易出去。 3、还有其他更叼的事情等等。 听起来是不是...

    ImportSource
  • 依赖注入容器-- Autofac

    Autofac---Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上非常高...

    小世界的野孩子
  • Java面向对象之抽象类,接口

    抽象类: 含有抽象方法的类被声明为抽象类 抽象方法由子类去实现 含有抽象方法的类必须被声明为抽象类 抽象类被子类继承,子类(如果不是抽象类)必须重写抽象类中...

    二十三年蝉
  • 代理模式

    mySoul
  • 设计模式-里氏替换

    先来看个最正宗的定义 如果对每一个类型为S的对象o1,都有类型为T的对 象o2,使得以T定义的所有程序P在所有的对象o1都代换成o2时,程序P的行为没有发生变 ...

    yiduwangkai

扫码关注云+社区

领取腾讯云代金券