📜  C#反射

📅  最后修改于: 2020-10-31 10:30:09             🧑  作者: Mango

C#反射

在C#中,反射是在运行时获取某种类型的元数据的过程。 System.Reflection命名空间包含进行反射所需的类,例如:

  • 类型
  • 会员信息
  • 构造器信息
  • 方法信息
  • 现场信息
  • 物业信息
  • 类型信息
  • 事件信息
  • 模组
  • 部件
  • AssemblyName
  • 指针等

System.Reflection.Emit命名空间包含用于发出元数据的类。

C#Type类

C#Type类代表类类型,接口类型,枚举类型,数组类型,值类型等的类型声明。可以在System名称空间中找到。它继承了System.Reflection.MemberInfo类。

C#类型属性

Type类的重要属性的列表如下:

Property Description
Assembly Gets the Assembly for this type.
AssemblyQualifiedName Gets the Assembly qualified name for this type.
Attributes Gets the Attributes associated with the type.
BaseType Gets the base or parent type.
FullName Gets the fully qualified name of the type.
IsAbstract is used to check if the type is Abstract.
IsArray is used to check if the type is Array.
IsClass is used to check if the type is Class.
IsEnum is used to check if the type is Enum.
IsInterface is used to check if the type is Interface.
IsNested is used to check if the type is Nested.
IsPrimitive is used to check if the type is Primitive.
IsPointer is used to check if the type is Pointer.
IsNotPublic is used to check if the type is not Public.
IsPublic is used to check if the type is Public.
IsSealed is used to check if the type is Sealed.
IsSerializable is used to check if the type is Serializable.
MemberType is used to check if the type is Member type of Nested type.
Module Gets the module of the type.
Name Gets the name of the type.
Namespace Gets the namespace of the type.

C#类型方法

下面列出了Type类的重要方法:

Method Description
GetConstructors() Returns all the public constructors for the Type.
GetConstructors(BindingFlags) Returns all the constructors for the Type with specified BindingFlags.
GetFields() Returns all the public fields for the Type.
GetFields(BindingFlags) Returns all the public constructors for the Type with specified BindingFlags.
GetMembers() Returns all the public members for the Type.
GetMembers(BindingFlags) Returns all the members for the Type with specified BindingFlags.
GetMethods() Returns all the public methods for the Type.
GetMethods(BindingFlags) Returns all the methods for the Type with specified BindingFlags.
GetProperties() Returns all the public properties for the Type.
GetProperties(BindingFlags) Returns all the properties for the Type with specified BindingFlags.
GetType() Gets the current Type.
GetType(String) Gets the Type for the given name.

C#反射示例:获取类型

using System;
public class ReflectionExample
{
   public static void Main()
    {
        int a = 10;
        Type type = a.GetType();
        Console.WriteLine(type);
    }
}

输出:

System.Int32

C#反射示例:获取程序集

using System;
using System.Reflection;
public class ReflectionExample
{
   public static void Main()
    {
        Type t = typeof(System.String);
        Console.WriteLine(t.Assembly); 
   }
}

输出:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

C#反射示例:打印类型信息

using System;
using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type t = typeof(System.String);
        Console.WriteLine(t.FullName);
        Console.WriteLine(t.BaseType);
        Console.WriteLine(t.IsClass);
        Console.WriteLine(t.IsEnum);
        Console.WriteLine(t.IsInterface);
    }
}

输出:

System.String
System.Object
true
false
false

C#反射示例:打印构造函数

using System;
using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type t = typeof(System.String);
        
        Console.WriteLine("Constructors of {0} type...", t);
        ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
        foreach (ConstructorInfo c in ci)
        {
            Console.WriteLine(c);
        }
    }
}

输出:

Constructors of System.String type...
Void .ctor(Char*)
Void .ctor(Char*, Int32, Int32)
Void .ctor(SByte*)
Void .ctor(SByte*, Int32, Int32)
Void .ctor(SByte*, Int32, Int32, System.Text.Encoding)
Void .ctor(Char[], Int32, Int32)
Void .ctor(Char[])
Void .ctor(Char, Int32)

C#反射示例:打印方法

using System;
using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type t = typeof(System.String);
        
        Console.WriteLine("Methods of {0} type...", t);
        MethodInfo[] ci = t.GetMethods(BindingFlags.Public | BindingFlags.Instance);
        foreach (MethodInfo m in ci)
        {
            Console.WriteLine(m);
        }
    }
}

输出:

Methods of System.String type...
Boolean Equals(System.Object)
Boolean Equals(System.String)
Boolean Equals(System.String, System.StringComparison)
Char get_Chars(Int32)
Void copyTo(Int32, char[], Int32, Int32)
Char[] ToCharArray()
....

C#反射示例:打印字段

using System;
using System.Reflection;
public class ReflectionExample
{
    public static void Main()
    {
        Type t = typeof(System.String);
        
        Console.WriteLine("Fields of {0} type...", t);
        FieldInfo[] ci = t.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
        foreach (FieldInfo f in ci)
        {
            Console.WriteLine(f);
        }
    }
}

输出:

Fields of System.String type...
System.String Empty
Int32 TrimHead
Int32 TrimTail
Int32 TrimBoth
Int32 charPtrAlignConst
Int32 alignConst