Monday, December 15, 2008

.NET and PowerShell

This week, Jeffrey Snover has posted an excellent post, Explore Your [Environment], that examines both some of the details of the [System.Environment] class but also on how PowerShell integrates with .NET. As I said in a comment on the blog, I wish I’d seen this post many years ago, as I struggled in the early days to understand how you could reach into .NET with PowerShell.

As Jeffrey describes, the .NET class has thousands of types, or types of objects. For example, System.Int32, a 32-bit integer. To access this type in Powerhell, simply enclose the type name in square brackets, as follows:

PS C:\Users\tfl> [system.int32] IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType

You can also pipe the class to Get-Member to discover some of the properties and methods this type supports, as follows:

PS C:\Users\tfl> [system.int32] | get-member TypeName: System.RuntimeType Name MemberType Definition ---- ---------- ---------- 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() 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;}

As you can see, the System.INT32 class has a number of methods and properties. A method is something you can invoke to do something on the object, for example ToString() produces a string representing the value of the system.int32. A property is some attribute of the type, for example Name which is the name of the object. Within .NET there are two types of methods: static and object. Object methods are methods that you can invoke on a particular occurrence of a type. The ToString() method, for example, converts a specific System.Int32 to a string while TryParse() takes a value and sees if it will fit inside a 32-bit integer vaue. Static methods are methods that the class provides but that are not tied to a specific instance of a class. Classes can also have static fields, or values you can use in a PowerShellscript. In the case of system.int32, there are two static fields MaxValue and MinValue.

Here is a look at the TryParse static method, the ToString object method, plus the two static fields.

PS C:\Users\tfl> $ok=1 PS C:\Users\tfl> [system.int32]::tryparse("12345",[ref] $ok) True PS C:\Users\tfl> [system.int32]::tryparse("1234567890123",[ref] $ok) False PS C:\Users\tfl> (23456).tostring() 23456 PS C:\Users\tfl> [system.int32]::maxvalue 2147483647 PS C:\Users\tfl> [system.int32]::minvalue -2147483648

To see the static methods, object methods and object properties, you can use Get-Member or for fuller information, see the MSDN Library documentation on-line. To find this imploration simply, just Google for the type name (enclosed in square brackets) – this should show a link to the class definition as the first result, from which you can drill down to see more details about the class.

Technorati Tags: ,

No comments: