In C#, reflection is a process of getting metadata of a type that is currently running.
The System.Reflection namespace contains classes that allow you to get information about the application and dynamically add types, values, and objects to the application.
Reflection provides objects (of type Type) describing modules and types. We can use Reflection to dynamically create an instance of a type, link the type to an existing object, or get the type from an existing object and call its methods or access its fields and properties. If we use attributes in our code, Reflection allows us to access them.
Here is a simple example on Reflection, using the static GetType method inherited from the Object class to get the type of a variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Program { static void Main(string[] args) { int a = 5; Type type = a.GetType(); Console.WriteLine(type); Console.ReadLine(); } } |
Output:
1 2 3 | System.Int32 |
In the following examples, we will use the String class as an example.
Reflection example: Displaying information about a given type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Program { static void Main(string[] args) { // the typeof operator is used to get the type of the object Type t = typeof(System.String); // retrieve the full name of the type. Console.WriteLine(t.FullName); // retrieve the base or parent type. Console.WriteLine(t.BaseType); // check if the type is an interface. Console.WriteLine(t.IsInterface); // check if the type is a class. Console.WriteLine(t.IsClass); // check if the type is Enum. Console.WriteLine(t.IsEnum); // check if the type is public. Console.WriteLine(t.IsPublic); // check if the type is Abstract. Console.WriteLine(t.IsAbstract); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 | System.String System.Object False True False True False |
Example of Reflection: Display the Constructors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Program { static void Main(string[] args) { Type t = typeof(System.String); Console.WriteLine("Constructors of type {0} is:", t); ConstructorInfo[] infos = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance); foreach (ConstructorInfo info in infos) { Console.WriteLine(info); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 | Constructors of type System.String is: Void .ctor(Char*) Void .ctor(Char*, Int32, Int32) Void .ctor(SByte*) Void .ctor(SByte*, Int32, Int32) Void .ctor(SByte*, Int32, Int32, System.Text.Encoding) Void .ctor(Char[], Int32, Int32) Void .ctor(Char[]) Void .ctor(Char, Int32) |
Reflection example: Display the methods available in a given type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Program { static void Main(string[] args) { Type t = typeof(System.String); Console.WriteLine("The methods available in type {0} are :", t); MethodInfo[] infos = t.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (MethodInfo info in infos) { Console.WriteLine(info); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | The methods available in type System.String are : Boolean Equals(System.Object) Boolean Equals(System.String) Boolean Equals(System.String, System.StringComparison) Void CopyTo(Int32, Char[], Int32, Int32) Char[] ToCharArray() Char[] ToCharArray(Int32, Int32) Int32 GetHashCode() System.String[] Split(Char[]) System.String[] Split(Char[], Int32) System.String[] Split(Char[], System.StringSplitOptions) System.String[] Split(Char[], Int32, System.StringSplitOptions) System.String[] Split(System.String[], System.StringSplitOptions) System.String[] Split(System.String[], Int32, System.StringSplitOptions) System.String Substring(Int32) System.String Substring(Int32, Int32) System.String Trim(Char[]) System.String TrimStart(Char[]) System.String TrimEnd(Char[]) Boolean IsNormalized() Boolean IsNormalized(System.Text.NormalizationForm) System.String Normalize() System.String Normalize(System.Text.NormalizationForm) Int32 CompareTo(System.Object) Int32 CompareTo(System.String) Boolean Contains(System.String) Boolean EndsWith(System.String) Boolean EndsWith(System.String, System.StringComparison) Boolean EndsWith(System.String, Boolean, System.Globalization.CultureInfo) Int32 IndexOf(Char) Int32 IndexOf(Char, Int32) Int32 IndexOfAny(Char[]) Int32 IndexOfAny(Char[], Int32) Int32 IndexOf(System.String) Int32 IndexOf(System.String, Int32) Int32 IndexOf(System.String, Int32, Int32) Int32 IndexOf(System.String, System.StringComparison) Int32 IndexOf(System.String, Int32, System.StringComparison) Int32 IndexOf(System.String, Int32, Int32, System.StringComparison) Int32 LastIndexOf(Char) Int32 LastIndexOf(Char, Int32) Int32 LastIndexOfAny(Char[]) Int32 LastIndexOfAny(Char[], Int32) Int32 LastIndexOf(System.String) Int32 LastIndexOf(System.String, Int32) Int32 LastIndexOf(System.String, Int32, Int32) Int32 LastIndexOf(System.String, System.StringComparison) Int32 LastIndexOf(System.String, Int32, System.StringComparison) Int32 LastIndexOf(System.String, Int32, Int32, System.StringComparison) System.String PadLeft(Int32) System.String PadLeft(Int32, Char) System.String PadRight(Int32) System.String PadRight(Int32, Char) Boolean StartsWith(System.String) Boolean StartsWith(System.String, System.StringComparison) Boolean StartsWith(System.String, Boolean, System.Globalization.CultureInfo) System.String ToLower() System.String ToLower(System.Globalization.CultureInfo) System.String ToLowerInvariant() System.String ToUpper() System.String ToUpper(System.Globalization.CultureInfo) System.String ToUpperInvariant() System.String ToString() System.String ToString(System.IFormatProvider) System.Object Clone() System.String Trim() System.String Insert(Int32, System.String) System.String Replace(Char, Char) System.String Replace(System.String, System.String) System.String Remove(Int32, Int32) System.String Remove(Int32) System.TypeCode GetTypeCode() System.CharEnumerator GetEnumerator() Char get_Chars(Int32) Int32 get_Length() Int32 IndexOf(Char, Int32, Int32) Int32 IndexOfAny(Char[], Int32, Int32) Int32 LastIndexOf(Char, Int32, Int32) Int32 LastIndexOfAny(Char[], Int32, Int32) System.Type GetType() |
Reflection example: Recovering the assembly
1 2 3 4 5 6 7 8 9 10 11 12 | class Program { static void Main(string[] args) { Type t = typeof(System.String); Console.WriteLine(t.Assembly); Console.ReadLine(); } } |
Output:
1 2 3 | mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |