Updated on Kisan Patel
If you want to an assembly that contains one or more classes, you can find out the methods and properties contained in that class. This information is useful, for example, if you have a DLL that contains classes you want to use, but you are unsure of how to use them.
Using reflection techniques and methods, you can examine each class in an assembly and understand whatever information you need to know about that class.
For Example,
Assembly assembly = Assembly.LoadFrom(AssemblyName); Type[] types = assembly.GetTypes(); foreach(Type type in types) { MethodInfo[] methods = t.GetMethods(); }
You can see that C# is consistent in how it implements reflection. You simply call the Get<x>
routine, where <x>
is the portion of the reflection information you are interested in, whether it is a Method, a Property, or a Parameter. This routine returns an array of <x>Info
objects, which contain further information about each of the individual elements in that type.
Note that the returned information can be accessed with a for…each loop.
Let’s take one example to understand how to obtain member information from a class using reflection in C#.
using System; using System.Reflection; namespace ObtainMemberInformation { class Program { public static void ShowClasses() { // Get assembly. Assembly assembly = Assembly.GetCallingAssembly(); if (assembly != null) { // Get array of types. Type[] types = assembly.GetTypes(); foreach (Type t in types) { Console.WriteLine("Class Name: {0}", t.FullName); ShowMethods(t); } } } public static void ShowMethods(Type t) { // Get methods. MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo m in methods) { Console.WriteLine("Method Name : {0}", m.Name); } } static void Main(string[] args) { ShowClasses(); } } }
the above program will produce below output…