Loop through Classes and Properties in Assembly - C# Reflection

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

  1. private List<string> GetClassNames(string DllName)  
  2. {  
  3.     string pathName = Request.PhysicalApplicationPath + "bin\\";        //Get the Location of the application  
  4.     Assembly assembly = Assembly.LoadFile(pathName + DllName);  
  5.     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. 
  1. private List<string> GetPropertiesName(string DllName, string ClassName)  
  2. {  
  3.     string pathName = Request.PhysicalApplicationPath + "bin\\";        //Get the Location of the application  
  4.     Assembly assembly = Assembly.LoadFile(pathName + DllName);  
  5.     Type type = assembly.GetType(ClassName);  
  6.     return Type.GetType(type.AssemblyQualifiedName).GetProperties().Select(a => a.ToString()).ToList();  

 These two methods can be combined and can be used in many applications. 
 
Ebook Download
View all
Learn
View all