Search This Blog

Tuesday 7 August 2012

How to get all classes and methods from a dll in ASP.Net C# using Reflection


Use of reflection in ASP.Net/How to extract dll file in ASP.Net,C#
By using Reflection class we can access all types of objects in a dll file. Now we are going to demonstrate how we can access all list of classes and methods by browsing a dll file. We can also run a particular method from the dll by passing proper parameters. We are using Reflection class to accept all types from a dll file in ASP.Net, C#.  The System.Reflection namespace contains classes and interfaces that provide a managed view of loaded types, methods, and fields, with the ability to dynamically create and invoke types.
What is Reflection in ASP.Net/C#?
  • Reflection is a collection of classes which allow you to query assembly (classes/objects) metadata at runtime. Using reflection you can also create new types and their instances at runtime and invoke methods on these new type instances.
  • Reflection enables you to find out information about types in your assemblies during runtime. Using reflection, you can find out the details of an object’s methods in terms of its access modifier ( private, public etc.), you can discover the name and types of parameters in a methods signature.
  • Reflection is the ability to read metadata at runtime. Using reflection, it is possible to uncover the methods, properties, and events of a type, and to invoke them dynamically. Reflection also allows us to create new types at runtime.
  • Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them.
How to List all classes in a dll file using Asp.Net,C# using reflection?
The following function accept dll file and extracting dll file using reflection class and return all classes in the dll file. We can call this function by dll file path as parameter then it will return all classes in the dll. In our project we are using a openfiledialog control to browse dll file and once the user select the dll file from local folder it will call below mentioned function with parameter as dll path. Then  it will return all classes in the dll file and we are displaying this list of class names in a list so that user can clearly seen all classes in the dll.
Funtion for listing all classes/types in a dll file in ASP.Net,C#
/// <summary>
/// Return all types loaded from desired DLL
/// </summary>
/// <param name="dllName">The DLL in which to parse and get the types from</param>
/// <returns>A filled ArrayList object containing all types </returns>
public ArrayList GetAllTypesFromDLLstring(string dllName)
{
Assembly _Assemblies = null;
try
{
_Assemblies = Assembly.LoadFrom(dllName);
}
catch (Exception ex)
{
Console.WriteLine("\n\nError - couldn't obtain assemblies from " + dllName);
Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
ArrayList _Quit = new ArrayList(1);
_Quit.Add("QUIT");
return _Quit;
}

Type[] _AllTypes = _Assemblies.GetTypes();

ArrayList _Temp = new ArrayList();

foreach (Type t in _AllTypes)
{
_Temp.Add(t.ToString());
}

return _Temp;
}
 
How to access all methods in a dll file in ASP.Net,C# using reflection library
Next we are going to access all methods in a class in the dll file. Here we have to pass dll file patha and class name to list all methods. The output will be the list of all methods in the particular class in the dll file.
Function for listin all methods in a class from dll file using reflection in ASP.Net,C#
/// <summary>
/// Returns all method names from desired DLL/Class
/// </summary>
/// <param name="dllName">The DLL in which to parse for desired class</param>
/// <param name="className">The class in which to parse for all methods</param>
/// <returns>An ArrayList of each method from desired class</returns>
public ArrayList GetAllTypesFromClass(string dllName, string className)
{
Assembly _Assemblies = Assembly.LoadFrom(dllName);

Type _Type = _Assemblies.GetType(className);

ArrayList _Temp = new ArrayList();

try
{
MethodInfo[] _Methods = _Type.GetMethods();

foreach (MethodInfo meth in _Methods)
{
_Temp.Add(meth.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine("\n\nError - couldn't obtain methods from " + dllName);
Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
_Temp.Clear();
_Temp.Capacity = 1;
_Temp.Add("QUIT");
}

return _Temp;
}

Is it possible to run a method in the dll file at run time in ASP.Net,C# using reflection ?
Yes, we can run a particular method in a dll file from our ASP.Net/C# application using reflection library at run time. Let’s see how we can run a method in the assembly file. The below mentioned function demonstrates how we can run a particular method from the asp.net,c# application at run time.
Function to call/run a method/function in a dll file from ASP.Net/C#  using reflection at runtime
/// <summary>
/// Runs target Method from target Class from target DLL.
/// </summary>
/// <param name="dllName">The DLL to load and use parse for methods</param>
/// <param name="className">The class to load from specific DLL</param>
/// <param name="methodName">The method to call from class</param>
public void RunClass(string dllName, string className, string methodName)
{
// Create the assemblies from our current DLL.
Assembly _Assemblies = Assembly.LoadFrom(dllName);

// Get the type that we want from the assemblies.
//  IE: This would be the fully qualified class name (including namespace)
//  Example: "Reflectionism.Examples.Example1" or "Reflectionism.Examples.Example2"
Type _Type = null;
try
{
_Type = _Assemblies.GetType(className);
}
catch (Exception ex)
{
Console.WriteLine("\n\nError - couldn't obtain classrd from " + className);
Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
return;
}

// Get the desired method we want from the target type.
MethodInfo _MethodInfo = null;
try
{
_MethodInfo = _Type.GetMethod(methodName);
}
catch (Exception ex)
{
Console.WriteLine("\n\nError - couldn't obtain method " +
    methodName + " from " + className);
Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
return;
}

// The first parameter to pass into the Invoke Method coming up.
Object _InvokeParam1 = Activator.CreateInstance(_Type);

// This calls the target method ("DisplayMyself").
//  NOTE: I'm not passing any arguments down to the method being invoked.
//  Therefore, I'm passing null as my argument, otherwise Invoke takes an
//  array of Objects.
_MethodInfo.Invoke(_InvokeParam1, null);
}
}
}
Hence we have discussed all possibilities regarding reflection in asp.net/c# such as how to access types/classes from dll file, how to get/access methods/functions from dll file/assembly using reflection in asp.net, possible way to call/run a method/function in the dll from asp.net/c# at run time. We hope you got a needful help from this post. Thanks..

No comments:

Post a Comment