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

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..

About these ads
  1. mayank
    December 21, 2011 at 7:21 pm | #1

    thanks a lot through your this simple and easiest way to make understand.
    now i have solved many problems regarding Reflaction.
    Mayank

  2. December 21, 2011 at 7:36 pm | #2

    Thank you. Please keep visiting the blog and post valuable topics and comments

  3. January 23, 2012 at 2:41 am | #3

    Very nice. And very useful for me for a project. Thank you for good explanation.

  4. June 29, 2012 at 11:02 am | #4

    This is awesome….. This makes concept very clear coz of questions and their answers…. Thank You…
    infotuvian

  5. June 30, 2012 at 7:24 pm | #5

    Thank you Pravin and please keep visiting my blog and expecting your valuable comments and suggetions.

  6. Ato Ngam Ngam
    July 3, 2012 at 7:37 am | #6

    Thank you very much.

  7. Harsh
    July 6, 2012 at 12:51 pm | #7

    Thanx buddy.. :)

  8. SUMAN
    July 31, 2012 at 2:00 pm | #8

    Thanks Great article

  9. SUMAN
    July 31, 2012 at 2:01 pm | #9

    SUMAN :
    Thanks a lot Great article

  10. Jithesh
    April 19, 2013 at 4:00 pm | #10

    Great article. Thanks a TON!!!

  11. Max
    June 18, 2013 at 12:31 pm | #11

    I got this error Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

  12. June 19, 2013 at 12:26 pm | #12

    Hi Max,

    Could you post the codes that you tried so that I can verify and let you know the solution ASAP.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 42 other followers

%d bloggers like this: