There will be lots of instances in your programming life where you need to dynamically invoke a method on a class. If you know the behavior of the class, and if you find that all classes which needs to be dynamically invoked have a common behavior then interfaces will help a lot.

Architecture for the application will contain 3 projects. The first will be the interface library where we need to define the common behavior of the classes. Secondly we need to implement the interface in some class to test our goal, and last will be the our main project which will try to use System.Reflection to load the assemblies and invoke the behavior on that class.

//Code and explanation starts here.

//1. common behavior project

//ExecutableLib.csproj will have just one file “IExecutable.cs” which will host our interface. This library project will build in to “ExecutableLib.dll”

using System;
using System.Text;

namespace ExecutableLib
{
public interface IExecutable
{
string Execute();
}
}

//2. Implementation of common behavior in some classes.

//ReflectInterfacesLib.csproj will contain 2 test implementations for our interface. This library project will build into “ReflectInterfacesLib.dll”; it has reference to “ExecutableLib.dll”

//Standalone.cs

using System;
using System.Text;
using ExecutableLib;
namespace ReflectInterfacesLib
{
public class Standalone : IExecutable
{

#region IExectable Members

string IExecutable.Execute()
{
return “Executing a stand-alone application.”;
}

#endregion
}
}

//WebApplication.cs

using System;
using System.Text;
using ExecutableLib;
namespace ReflectInterfacesLib
{
public class WebApplication : IExecutable
{

#region IExectable Members

string IExecutable.Execute()
{
return “Executing a web application.”;
}

#endregion
}
}

//3.  Our test application which will try to invoke the common behavior on Standalone and WebApplication classes using Reflection.

//ReflectInterfaces.csproj is an console based application project having reference to “”ExecutableLib.dll”.

// So we know the needed behavior to invoke at runtime.

using System;
using System.Text;
using System.Reflection;
using ExecutableLib;
namespace ReflectInterfaces
{
public class Program
{
public static void Main()
{
try
{
Assembly testAss = Assembly.LoadFrom(“ReflectInterfacesLib.dll”);
foreach (Type type in testAss.GetTypes())
{
if (type.IsClass == true)
{
Console.WriteLine(“Found Class : {0}”, type.FullName);
}
if (type.GetInterface(“ExecutableLib.IExecutable”) == null)
{
continue;
}
IExecutable iexe = (IExecutable)Activator.CreateInstance(type);
string tempStr = iexe.Execute();
Console.WriteLine(tempStr);

}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}

// Output from the above console application will be as follows:

Found Class : ReflectInterfacesLib.Standalone
Executing a stand-alone application.
Found Class : ReflectInterfacesLib.WebApplication
Executing a web application.

//Code and explanation ends here.

So our goal was achieved by defining the common behavior in an interface and then implementing a stack of classes which are needed to show the common behavior.  Reflection can be studied for building loosely coupled components and for runtime bindings.

-Bugs!

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: