[C#] Reflection – Overview

In .NET, reflection is the process of runtime type discovery. Using reflection, you can programmatically retrieve the metadata information (Assemblies, Types, Methods, Properties, etc…) Most reflection-related classes are defined in the “System.Reflection” namespace.

 

1. System.Type

The “System.Type” class represents type declarations. This class includes the properties and methods to access the metadata information of the type.

Please refer to the MSDN documentation (http://msdn.microsoft.com/en-us/library/system.type.aspx).

The most commonly used properties and methods are the followings:

public string FullName { get; }

public ConstructorInfo GetConstructor(Type[] types)
public ConstructorInfo[] GetConstructors()

public MethodInfo GetMethod(string name, Type[] types)
public MethodInfo[] GetMethods()

public FieldInfo GetField(string name)
public FieldInfo[] GetFields()

public PropertyInfo GetProperty(string name)
public PropertyInfo[] GetProperties()

public Type GetInterface(string name)
public Type[] GetInterfaces()

 

2. Obtaining a Type Reference

To look at type information, you need to obtain a Type reference:

  • System.Object.GetType(): instance method
  • System.Type.GetType(): static method
  • typeof(): operator

When you use the “Type.GetType()” method, you can pass 2 more arguments as well as the type name: “bool throwOnError” and “bool ignoreCase

Type[] types = new Type[]
{
  (new System.Int32()).GetType(),
  Type.GetType("system.string", false, true), // ignore case
  typeof(System.Decimal),
  Type.GetType("MyLib.MyClass, MyLib"), // external assembly
  Type.GetType("MyLib.MyClass+NestedType") // nested type
}
foreach (Type t in types)
{
  Console.WriteLine($"{t.FullName}");
}

Note that the “+” sign is used to specify the nested type.

 

3. System.Reflection.MemberInfo

The abstract “System.Reflection.MemberInfo” class represents the information about the members of the type. The following classes are derived from “MemberInfo

  • EventInfo
  • FieldInfo
  • MethodBase, MethodInfo
  • PropertyInfo
public virtual Module Module { get; }
public abstract string Name { get; }

 

4. System.Reflection.MethodBase

The abstract “System.Reflection.MethodBase” class provides information about methods and constructors. “MethodInfo” and “ConstructorInfo” classes are derived from “MethodBase

public bool IsAbstract { get; }
public bool IsConstructor { get; }
public bool IsFinal { get; }
public bool IsPrivate { get; }
public bool IsPublic { get; }
public bool IsStatic { get; }
public bool IsVirtual { get; }

public Object Invoke(Object obj, Object[] parameters)

The interesting method is “Invoke()“. You can invoke an instance method or a static method by setting the first parameter (null for static methods).

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s