ARTICLE

Late Binding With Reflection

Posted by Sam Haidar Articles | .NET Assemblies October 20, 2003
Component based technology has done great things for software development, allowing reuse and leading to better-written code that separates subjects and entities into distinct components.
Reader Level:

Component based technology has done great things for software development, allowing reuse and leading to better-written code that separates subjects and entities into distinct components. One of the potential side effects of working with components, however, is that at times we find ourselves faced with a dll assembly that contains several classes where only the method names are documented. This is especially true in poorly documented libraries, with classes that implement well known interfaces. Luckily, the System.Reflection namespace of the .Net framework arms us with enough functionality to overcome this problem.

The Assembly class

The System.Reflection.Assembly class provides us with the functionality needed to probe into what the assembly has to offer. In this case, our plan is to load the assembly of interest into our application domain, and then query for all the types included in it. We can loop through all the types, finding out whether the method that we are interested in is a member of any of those types.

Once the method is found, we create an instance of the type that contains the method and invoke that method.

Querying types in an assembly

Let us assume that we are interested in calling a mthod from an assembly named SomeControls.dll, that we have it stored locally in C:\AssemblyDir. The method we need to call is named GetUserName, with the following signature:

string GetUserName(string account)

The first thing we need to do is to load the assembly, using the file name of the dll:

Assembly assemblyInstance = Assembly.LoadFrom(@"c:\AssemblyDir\SomeControls.dll");

After we get an instance of the loaded assembly, we can call the GetTypes() method of the Assembly class to get an array containing all the types defined in the asembly:

Type[] types = assemblyInstance.GetTypes();

Once we get an array containing all the types in the assembly, we can loop through and look for the method in each type. The GetMethod(string) member method of the Type class returns a MethodInfo object if the method happens to be a member of that type, and null if not found:

foreach (Type t in types)
{
MethodInfo mi = t.GetMethod(GetUserName);

Creating a type instance

Once the method is found in one of the types, We create an instance of that type so that we can call its method. We have two options for creating an instance of a type: using Reflection or using the Activator class of the System namespace.

To use reflection, we need to call the CreateInstance(string) instance method of the Assembly class, which takes the type name as an argument.

If we use the Activator class, we have to call its CreateInstance(Type) static method, which takes the type object as an argument.

Invoking the method

Once we have an instance of the type containing the method, the final step is to call the InvokeMember() method of the Type class, passing in the instance of the type and the Account string argument.

The code below shows all the steps described so far:

MethodInfo mi;
object result = null;
object[] args = new object[] {"ABC123"};
try
{
Assembly assemblyInstance = Assembly.LoadFrom(@"c:\AssemblyDir\SomeControls.dll");
Type[] types = assemblyInstance.GetTypes();
foreach (Type t in types)
{
mi = t.GetMethod("GetUserName");
if (mi != null)
{
string typeName = t.FullName;
object lateBoundObj = Asm.CreateInstance(typeName);
result = t.InvokeMember (MethodName, BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
null, lateBoundObj, args);
break;
}
}
string userName = result.ToString();
MessageBox("User Name is: {0}", userName)
}
catch(Exception ex)
{
MessageBox.Show(ex.Message)
}

Login to add your contents and source code to this article
post comment
     

as subject

Posted by tonypower Jul 07, 2011

This article is a guide to building a .net component ,using it in a Vb6 project at runtime using late binding, attaching it's events and get a callback.

http://www.codeproject.com/KB/cs/csapivb6callback2.aspx

Posted by Yusuf Apr 26, 2010

Hi
   I am loading a dll dynamically as explained in the above artice and also invoking methods.
   What I would like to know is
   how to invoke events present in the dll which is loaded dynamically.

thanks
 harry
 

Posted by hari babu Aug 21, 2009
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.