C# - 反射

C# - 反射 首页 / C#入门教程 / C# - 反射

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

  • Type
  • MemberInfo
  • ConstructorInfo
  • MethodInfo
  • FieldInfo
  • PropertyInfo
  • TypeInfo
  • EventInfo
  • Module
  • Assembly
  • AssemblyName
  • Pointer etc.

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

无涯教程网

C#类型类

C#类型类表示类类型、接口类型、枚举类型、数组类型、值类型等的类型声明。位于 System 命名空间中。它继承 System.Reflection.MemberInfo 类。

C#类型属性

下面列出了Type类的重要属性:

PropertyDescription
Assembly获取此类型的程序集。
AssemblyQualifiedName获取此类型的程序集限定名称。
Attributes获取与类型关联的属性。
BaseType获取基类型或父类型。
FullName获取类型的完全限定名称。
IsAbstract用于检查类型是否为抽象。
IsArray用于检查类型是否为 Array。
IsClass用于检查类型是否为 Class。
IsEnum用于检查类型是否为 Enum。
IsInterface用于检查类型是否为接口。
IsNested用于检查类型是否为嵌套。
IsPrimitive用于检查类型是否为 Primitive。
IsPointer用于检查类型是否为Pointer。
IsNotPublic用于检查类型是否不是Public。
IsPublic用于检查类型是否为 Public。
IsSealed用于检查类型是否为密封。
IsSerializable用于检查类型是否可序列化。
MemberType用于检查类型是否为嵌套类型的成员类型。
Module获取类型的模块。
Name获取类型的名称。
Namespace获取类型的命名空间。

C#类型方法

Type类的重要方法列表如下:

MethodDescription
GetConstructors()返回该类型的所有公共构造函数。
GetConstructors(BindingFlags)返回具有指定 BindingFlags 的 Type 的所有构造函数.
GetFields()返回该类型的所有公共字段。
GetFields(BindingFlags)返回具有指定 BindingFlags 的 Type 的所有公共构造函数。
GetMembers()返回该类型的所有公共成员。
GetMembers(BindingFlags)返回具有指定 BindingFlags 的 Type 的所有成员。
GetMethods()返回该类型的所有公共方法。
GetMethods(BindingFlags)返回具有指定 BindingFlags 的 Type 的所有方法。
GetProperties()返回该类型的所有公共属性。
GetProperties(BindingFlags)返回具有指定 BindingFlags 的 Type 的所有属性。
GetType()获取当前类型。
GetType(String)获取给定名称的类型。

C#反射示例:获取类型

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

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-reflection.html

来源:LearnFk无涯教程网

System.Int32

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

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

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-reflection.html

来源:LearnFk无涯教程网

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);
    }
}

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-reflection.html

来源:LearnFk无涯教程网

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);
        }
    }
}

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-reflection.html

来源:LearnFk无涯教程网

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);
        }
    }
}

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-reflection.html

来源:LearnFk无涯教程网

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);
        }
    }
}

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-reflection.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

机器学习40讲 -〔王天一〕

如何做好一场技术演讲 -〔极客时间〕

后端技术面试 38 讲 -〔李智慧〕

性能测试实战30讲 -〔高楼〕

如何读懂一首诗 -〔王天博〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

遗留系统现代化实战 -〔姚琪琳〕

云计算的必修小课 -〔吕蕴偲〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)