用PowerShell探索System.RuntimeType类型

来源:互联网 发布:ubuntu java 路径 编辑:程序博客网 时间:2024/06/05 07:46

PowerShell绝对是学习.Net Framework的好工具, 这句话是PowerShell团队中某人说的, 实际确实如此, 我们可以通过Get-Member来窥探.Net中很多神秘的元素. 要学习.Net Framework, 我想很多人会拜读Jeffrey Richter所编写的<<Applied Microsoft .NET Framework Programming>>一书.

.Net Framework中的类在加载到应用域(Application Domain)时, 转化为System.RuntimeType的一个实例, System.RuntimeType是一个非公开的类, 你无法在MSDN上找到详细信息, PowerShell提供了简单途径来窥探System.RuntimeType中的成员. 在Jeffrey Richter书中也介绍过System.RuntimeType的内容, 我们下面以System.String为例, 用PowerShell进行详细介绍:

PS C:/> [System.String].GetType().FullName
System.RuntimeType

我们看到了神秘System.RuntimeType类的实例, 我们可以用Get-Member看看这个类上都有哪些成员:

PS C:/> [System.String] | Get-Member | Sort-Object MemberType, Name
   TypeName: System.RuntimeType

Name                           MemberType Definition
----                           ---------- ----------
Assembly                       Property   System.Reflection.Assembly Assembly {get;}
AssemblyQualifiedName          Property   System.String AssemblyQualifiedName {get;}
Attributes                     Property   System.Reflection.TypeAttributes Attributes {get;}
BaseType                       Property   System.Type BaseType {get;}
ContainsGenericParameters      Property   System.Boolean ContainsGenericParameters {get;}
DeclaringMethod                Property   System.Reflection.MethodBase DeclaringMethod {get;}
DeclaringType                  Property   System.Type DeclaringType {get;}
FullName                       Property   System.String FullName {get;}
GenericParameterAttributes     Property   System.Reflection.GenericParameterAttributes GenericParameterAttributes {g...
GenericParameterPosition       Property   System.Int32 GenericParameterPosition {get;}
GUID                           Property   System.Guid GUID {get;}
HasElementType                 Property   System.Boolean HasElementType {get;}
IsAbstract                     Property   System.Boolean IsAbstract {get;}
IsAnsiClass                    Property   System.Boolean IsAnsiClass {get;}
IsArray                        Property   System.Boolean IsArray {get;}
IsAutoClass                    Property   System.Boolean IsAutoClass {get;}
IsAutoLayout                   Property   System.Boolean IsAutoLayout {get;}
IsByRef                        Property   System.Boolean IsByRef {get;}
IsClass                        Property   System.Boolean IsClass {get;}
IsCOMObject                    Property   System.Boolean IsCOMObject {get;}
IsContextful                   Property   System.Boolean IsContextful {get;}
IsEnum                         Property   System.Boolean IsEnum {get;}
IsExplicitLayout               Property   System.Boolean IsExplicitLayout {get;}
IsGenericParameter             Property   System.Boolean IsGenericParameter {get;}
IsGenericType                  Property   System.Boolean IsGenericType {get;}
IsGenericTypeDefinition        Property   System.Boolean IsGenericTypeDefinition {get;}
IsImport                       Property   System.Boolean IsImport {get;}
IsInterface                    Property   System.Boolean IsInterface {get;}
IsLayoutSequential             Property   System.Boolean IsLayoutSequential {get;}
IsMarshalByRef                 Property   System.Boolean IsMarshalByRef {get;}
IsNested                       Property   System.Boolean IsNested {get;}
IsNestedAssembly               Property   System.Boolean IsNestedAssembly {get;}
IsNestedFamANDAssem            Property   System.Boolean IsNestedFamANDAssem {get;}
IsNestedFamily                 Property   System.Boolean IsNestedFamily {get;}
IsNestedFamORAssem             Property   System.Boolean IsNestedFamORAssem {get;}
IsNestedPrivate                Property   System.Boolean IsNestedPrivate {get;}
IsNestedPublic                 Property   System.Boolean IsNestedPublic {get;}
IsNotPublic                    Property   System.Boolean IsNotPublic {get;}
IsPointer                      Property   System.Boolean IsPointer {get;}
IsPrimitive                    Property   System.Boolean IsPrimitive {get;}
IsPublic                       Property   System.Boolean IsPublic {get;}
IsSealed                       Property   System.Boolean IsSealed {get;}
IsSerializable                 Property   System.Boolean IsSerializable {get;}
IsSpecialName                  Property   System.Boolean IsSpecialName {get;}
IsUnicodeClass                 Property   System.Boolean IsUnicodeClass {get;}
IsValueType                    Property   System.Boolean IsValueType {get;}
IsVisible                      Property   System.Boolean IsVisible {get;}
MemberType                     Property   System.Reflection.MemberTypes MemberType {get;}
MetadataToken                  Property   System.Int32 MetadataToken {get;}
Module                         Property   System.Reflection.Module Module {get;}
Name                           Property   System.String Name {get;}
Namespace                      Property   System.String Namespace {get;}
ReflectedType                  Property   System.Type ReflectedType {get;}
StructLayoutAttribute          Property   System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute...
TypeHandle                     Property   System.RuntimeTypeHandle TypeHandle {get;}
TypeInitializer                Property   System.Reflection.ConstructorInfo TypeInitializer {get;}
UnderlyingSystemType           Property   System.Type UnderlyingSystemType {get;}
Clone                          Method     System.Object Clone()
Equals                         Method     System.Boolean Equals(Object obj), System.Boolean Equals(Type o)
FindInterfaces                 Method     System.Type[] FindInterfaces(TypeFilter filter, Object filterCriteria)
FindMembers                    Method     System.Reflection.MemberInfo[] FindMembers(MemberTypes memberType, Binding...
GetArrayRank                   Method     System.Int32 GetArrayRank()
GetConstructor                 Method     System.Reflection.ConstructorInfo GetConstructor(BindingFlags bindingAttr,...
GetConstructors                Method     System.Reflection.ConstructorInfo[] GetConstructors(BindingFlags bindingAt...
GetCustomAttributes            Method     System.Object[] GetCustomAttributes(Boolean inherit), System.Object[] GetC...
GetDefaultMembers              Method     System.Reflection.MemberInfo[] GetDefaultMembers()
GetElementType                 Method     System.Type GetElementType()
GetEvent                       Method     System.Reflection.EventInfo GetEvent(String name, BindingFlags bindingAttr...
GetEvents                      Method     System.Reflection.EventInfo[] GetEvents(BindingFlags bindingAttr), System....
GetField                       Method     System.Reflection.FieldInfo GetField(String name, BindingFlags bindingAttr...
GetFields                      Method     System.Reflection.FieldInfo[] GetFields(BindingFlags bindingAttr), System....
GetGenericArguments            Method     System.Type[] GetGenericArguments()
GetGenericParameterConstraints Method     System.Type[] GetGenericParameterConstraints()
GetGenericTypeDefinition       Method     System.Type GetGenericTypeDefinition()
GetHashCode                    Method     System.Int32 GetHashCode()
GetInterface                   Method     System.Type GetInterface(String fullname, Boolean ignoreCase), System.Type...
GetInterfaceMap                Method     System.Reflection.InterfaceMapping GetInterfaceMap(Type ifaceType)
GetInterfaces                  Method     System.Type[] GetInterfaces()
GetMember                      Method     System.Reflection.MemberInfo[] GetMember(String name, MemberTypes type, Bi...
GetMembers                     Method     System.Reflection.MemberInfo[] GetMembers(BindingFlags bindingAttr), Syste...
GetMethod                      Method     System.Reflection.MethodInfo GetMethod(String name, BindingFlags bindingAt...
GetMethods                     Method     System.Reflection.MethodInfo[] GetMethods(BindingFlags bindingAttr), Syste...
GetNestedType                  Method     System.Type GetNestedType(String fullname, BindingFlags bindingAttr), Syst...
GetNestedTypes                 Method     System.Type[] GetNestedTypes(BindingFlags bindingAttr), System.Type[] GetN...
GetObjectData                  Method     System.Void GetObjectData(SerializationInfo info, StreamingContext context)
GetProperties                  Method     System.Reflection.PropertyInfo[] GetProperties(BindingFlags bindingAttr), ...
GetProperty                    Method     System.Reflection.PropertyInfo GetProperty(String name, BindingFlags bindi...
GetType                        Method     System.Type GetType(), System.Type GetType()
InvokeMember                   Method     System.Object InvokeMember(String name, BindingFlags bindingFlags, Binder ...
IsAssignableFrom               Method     System.Boolean IsAssignableFrom(Type c)
IsDefined                      Method     System.Boolean IsDefined(Type attributeType, Boolean inherit)
IsInstanceOfType               Method     System.Boolean IsInstanceOfType(Object o)
IsSubclassOf                   Method     System.Boolean IsSubclassOf(Type type)
MakeArrayType                  Method     System.Type MakeArrayType(), System.Type MakeArrayType(Int32 rank)
MakeByRefType                  Method     System.Type MakeByRefType()
MakeGenericType                Method     System.Type MakeGenericType(Type[] instantiation)
MakePointerType                Method     System.Type MakePointerType()
ToString                       Method     System.String ToString()

我们可以通过System.RuntimeType上的GetContructors方法知道System.String上的构造函数:

PS C:/> [System.String].GetConstructors() | %{ $_.ToString() }
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)

下面是MSDN上关于System.String类构造器的定义:

  Name Description String(Char*) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters. String(array<Char>[]()[]) Initializes a new instance of the String class to the value indicated by an array of Unicode characters. String(SByte*) Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers. String(Char, Int32) Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times. String(Char*, Int32, Int32) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters, a starting character position within that array, and a length. String(array<Char>[]()[], Int32, Int32) Initializes a new instance of the String class to the value indicated by an array of Unicode characters, a starting character position within that array, and a length. String(SByte*, Int32, Int32) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting character position within that array, and a length. String(SByte*, Int32, Int32, Encoding) Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting character position within that array, a length, and an Encoding object.

是不是很方便呢??

原创粉丝点击