Skip to content

Reflection

Mario Gutierrez edited this page Jan 7, 2017 · 1 revision

Classes used for reflection are located in the System.Reflection namespace.

  • Assembly - Contains members that allow loading, investigation, and manipulation of an assembly.
  • AssemblyName - Used to find details about the identity of an assembly.
  • EventInfo
  • FieldInfo
  • MemberInfo
  • MethodInfo
  • Module - Provides access to a given module within a multifile assembly.
  • ParameterInfo
  • PropertyInfo

The -Info classes provide information and manipulation for those things.

System.Type

The Type class is another class used heavily in reflection.

To get a reference to a type you can use typeof() or GetType().

Reflecting on Generic Types

When calling Type.GetType() for a generic type, you must make use of a special syntax involving a "back tick" character.

For example, for System.Collections.Generic.List<T>, do:

System.Collections.Generic.List`1

The number at the end is the number of generic parameters the type has.

Late Binding

You can use Assembly.Load() to load in an assembly unknown at compile-time. Then, you can use System.Activator.CreateInstance() to create an instance of a class from that assembly using late-binding.

Clone this wiki locally