在C#中,反射是一个进程,用于在运行时获取某个类型的元数据。System.Refltion命名空间包含反射所需的类,例如:
System.Reflection.Emit命名空间包含用于发射元数据的类。
C#类型类表示类类型、接口类型、枚举类型、数组类型、值类型等的类型声明。位于 System 命名空间中。它继承 System.Reflection.MemberInfo 类。
下面列出了Type类的重要属性:
Property | Description |
---|---|
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 | 获取类型的命名空间。 |
Type类的重要方法列表如下:
Method | Description |
---|---|
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) | 获取给定名称的类型。 |
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
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
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
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)
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() ....
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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕