用PowerShell探索System.RuntimeType類型

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.

是不是很方便呢??

發佈了110 篇原創文章 · 獲贊 10 · 訪問量 54萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章