Reflection (C# Programming Guide)
Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them. For more information, see Attributes.
Here's a simple example of Reflection using the static method GetType - inherited by all types from the Object base class - to obtain the type of a variable:
// Using GetType to obtain type information: int i = 42; System.Type type = i.GetType(); System.Console.WriteLine(type);
The output is:
System.Int32
In this example, Reflection is used to obtain the full name of a loaded assembly:
// Using Reflection to get information from an Assembly: System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll"); System.Console.WriteLine(o.GetName());
The output is:
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Reflection Overview
Reflection is useful in the following situations:
-
When you need to access attributes in your program's metadata. See the topic Accessing Attributes With Reflection.
-
For examining and instantiating types in an assembly.
-
For building new types at runtime. Use classes in System.Reflection.Emit.
-
For performing late binding, accessing methods on types created at run time. See the topic Dynamically Loading and Using Types.
Related Sections
For more information:
C# Language Specification
For more information, see the following sections in the C# Language Specification:
-
1.12 Attributes
-
7.5.11 The typeof operator
See Also
# get-type.ps1
# Uses GetType to obtain type information
# Thomas Lee - tfl@psp.co.uk[int] $i= 42
$type=$i.GetType()
$type | fl*
This script produces the following output
PS C:\foo> .\get-type.ps1
Module : CommonLanguageRuntimeLibrary
Assembly : mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
TypeHandle : System.RuntimeTypeHandle
DeclaringMethod :
BaseType : System.ValueType
UnderlyingSystemType : System.Int32
FullName : System.Int32
AssemblyQualifiedName : System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Namespace : System
GUID : a310fadd-7c33-377c-9d6b-599b0317d7f2
GenericParameterAttributes :
IsGenericTypeDefinition : False
IsGenericParameter : False
GenericParameterPosition :
IsGenericType : False
ContainsGenericParameters : False
StructLayoutAttribute : System.Runtime.InteropServices.StructLayoutAttribute
Name : Int32
MemberType : TypeInfo
DeclaringType :
ReflectedType :
MetadataToken : 33554626
TypeInitializer :
IsNested : False
Attributes : AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, Serializable, BeforeFieldInit
IsVisible : True
IsNotPublic : False
IsPublic : True
IsNestedPublic : False
IsNestedPrivate : False
IsNestedFamily : False
IsNestedAssembly : False
IsNestedFamANDAssem : False
IsNestedFamORAssem : False
IsAutoLayout : False
IsLayoutSequential : True
IsExplicitLayout : False
IsClass : False
IsInterface : False
IsValueType : True
IsAbstract : False
IsSealed : True
IsEnum : False
IsSpecialName : False
IsImport : False
IsSerializable : True
IsAnsiClass : True
IsUnicodeClass : False
IsAutoClass : False
IsArray : False
IsByRef : False
IsPointer : False
IsPrimitive : True
IsCOMObject : False
HasElementType : False
IsContextful : False
IsMarshalByRef : False
#
- 11/10/2008
- Thomas Lee
- 12/1/2008
- Anand22
# Get-Assemblyname
# Gets assembly full name of a loaded assembly
# Thomas Lee - tfl@psp.co.uk
# Using Reflection to get information from an Assembly:
$o =[System.Reflection.Assembly]::Load("mscorlib.dll")
$name = $o.GetName()
"Full name = `"{0}`"" -f $name
This code produces the followign output:
PS C:\foo> .\get-fullname.ps1
Full name = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
- 11/16/2008
- Thomas Lee
- 12/1/2008
- Anand22
- 7/18/2008
- vijay_samco
- 7/18/2008
- Thomas Lee