There are many applications for reflection. Here there is coding for looping through classes in a solution and also for looping through properties in the classes in the solution
First we can look at the coding that loops through all the classes in the solution
- private List<string> GetClassNames(string DllName)
- {
- string pathName = Request.PhysicalApplicationPath + "bin\\";
- Assembly assembly = Assembly.LoadFile(pathName + DllName);
- return assembly.GetTypes().OrderBy(b => b.FullName).Select(a => a.FullName.ToString()).ToList();
- }
The above method takes the dll name as input, loads the assembly file and then loads all the classes in the assembly to a list.
The second method given below takes the dll name and the class name as input, loads the assembly file and the loads all the properties of the given class to a list.
- private List<string> GetPropertiesName(string DllName, string ClassName)
- {
- string pathName = Request.PhysicalApplicationPath + "bin\\";
- Assembly assembly = Assembly.LoadFile(pathName + DllName);
- Type type = assembly.GetType(ClassName);
- return Type.GetType(type.AssemblyQualifiedName).GetProperties().Select(a => a.ToString()).ToList();
- }
These two methods can be combined and can be used in many applications.