Chinaunix首页 | 论坛 | 博客
  • 博客访问: 931811
  • 博文数量: 74
  • 博客积分: 10001
  • 博客等级: 上将
  • 技术积分: 2609
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-04 19:54
文章存档

2015年(1)

2009年(2)

2008年(71)

我的朋友

分类: WINDOWS

2008-05-25 10:07:07

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

.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
Initializes a new instance of the class to the value indicated by a specified pointer to an array of Unicode characters.
Initializes a new instance of the class to the value indicated by an array of Unicode characters.
Initializes a new instance of the class to the value indicated by a pointer to an array of 8-bit signed integers.
Initializes a new instance of the class to the value indicated by a specified Unicode character repeated a specified number of times.
Initializes a new instance of the 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.
Initializes a new instance of the class to the value indicated by an array of Unicode characters, a starting character position within that array, and a length.
Initializes a new instance of the 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.
Initializes a new instance of the 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 object.

是不是很方便呢??

阅读(3087) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-11-14 14:34:20

[System.String].GetConstructors() | %{ $_.ToString() }中, %{ $_.ToString() }是什么意思?%不是取模运算符吗?