C# Programming Guide
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:

C#
// 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:

C#
// 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:

Related Sections

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

Tags : reflection


Community Content

Thomas Lee
Reflection
Microsoft's .NET stores type information as Metadata. Reflection is a mechanism to access this Metadata information and enable us to access it with our .NET Code.
Tags :

Anand22
Get-Type using PowerShell
# 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
#


Anand22
GetFull Name using PowerShell
# 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"


Page view tracker