System.Type类
System.Type类
——摘自 C#高级编程(第3版)
引用:
引用:
Type t = typeof(double)
Type t = d.GetType();
Type t = Type.GetType("System.Double");
屬性:
属 性 | 返 回 值 |
Name | 数据类型名 |
FullName | 数据类型的完全限定名(包括命名空间名) |
Namespace | 定义数据类型的命名空间名 |
属 性 | 返回对应的Type引用 |
BaseType | 这个Type的直接基本类型 |
UnderlyingSystemType | 这个Type在 .NET 运行库中映射的类型 (某些.NET基类实际上映射由IL识别的特定预定义类型) |
其它一些Boolean 属性表示这个类型是一个类、还是一个枚举等。这些属性包括IsAbstract、IsArray、IsClass、IsEnum、IsInterface、IsPointer、IsPrimitive(一种预定义的基本数据类型)、 IsPublic、IsSealed和IsValueType
方法:用于获取对应数据类型的成员信息:构造函数、属性、方法和事件等。
返回的对象类型 | 方法 (名称为复数形式的方法返回一个数组) |
ConstructorInfo | GetConstructor(), GetConstructors() |
EventInfo | GetEvent(), GetEvents() |
FieldInfo | GetField(), GetFields() |
InterfaceInfo | GetInterface(), GetInterfaces() |
MemberInfo | GetMember(), GetMembers() |
MethodInfo | GetMethod(), GetMethods() |
PropertyInfo | GetProperty(), GetProperties() |
一個小例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Type t = typeof(String);
MethodInfo substr = t.GetMethod("Substring",
new Type[] { typeof(int), typeof(int) });
Object result =
substr.Invoke("Hello, World!", new Object[] { 7, 5 });
Console.WriteLine("{0} returned \"{1}\".", substr, result);
輸出:System.String Substring(Int32, Int32) returned "World".
MethodInfo substr = t.GetMethod("Substring",
new Type[] { typeof(int), typeof(int) });
Object result =
substr.Invoke("Hello, World!", new Object[] { 7, 5 });
Console.WriteLine("{0} returned \"{1}\".", substr, result);
輸出:System.String Substring(Int32, Int32) returned "World".