Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘reflection’

Creating a Plugin enabled Application : Part 2 of 2

Posted by JP on May 4, 2011

This article is a continuation of the article Creating a Plugin enabled Application : Part 1 of 2. The code samples would be the same. In the previous article we have seen how to create the Plugin Container, Plugin Interface and the actual Plugin modules. Here I am going to explain how the plugin modules are written and how they are loaded into the application.

The 3 Components

1) Plugin Container

2) Plugin Interface

3) Plugin

Steps Involved

1. The Plugin.Container application is executed

2. The MainForm inside it calls the PluginLoader class – LoadPlugins() method

3. The LoadPlugins() method searches the given folder for any files with *Plugin.dll in name and loads the Assembly

4. The Assembly loaded will be searched for public types implementing interface IPlugin

5. The classes which implements IPlugin will be instantiated and returned to the caller as IList

6. The MainForm calls the plugin.Text property to display the caption in form as a button

7. On click of the button the form is instantiated using plugin.Formproperty

Class Diagram

clip_image002

The IPlugin interface shown above contains 3 properties.

· BackColor – Just a color for the form and button

· Form – The actual plugin form

· Text – The text description of the plugin form

The PluginLoader class contains only one method LoadPlugins which searches and loads the plugins.

The Solution Explorer with all the projects will look like below:

clip_image004

LoadPlugins()Explained

.Net Reflection is used to load the plugin files. The body of the LoadPlugins() method is given below:

public IList<IPlugin> LoadPlugins(string folder)

{

IList<IPlugin> plugins = new List<IPlugin>();

// Get files in folder

string[] files = Directory.GetFiles(folder, “*Plugin.dll”);

foreach (string file in files)

{

Assembly assembly = Assembly.LoadFile(file);

var types = assembly.GetExportedTypes();

foreach (Type type in types)

if (type.GetInterfaces().Contains(typeof(IPlugin)))

{

object instance = Activator.CreateInstance(type);

plugins.Add(instance as IPlugin);

}

}

return plugins;

}

The method will accept a folder path as argument . The Directory.GetFiles() method searches on the folder and returns all files with *Plugin.dll in the file name.

The files array will be used in the foreach loop above, and each file item will be used to load the Assembly. The method Assembly.LoadFile() is used for it.

Once loaded as Assembly we can get all the public types inside the assembly using the method GetExportedTypes(). This method will return all the public types inside the assembly. Iterating through each type (or class) we can find the classes which implements the IPlugin interface.

For the classes which implement IPlugin, we instantiate the class and added to the plugins list. The Activator.CreateInstance() method is used to create the instance dynamically. This method on completion of all plugin files check, returns the list of Plugin objects.

MainForm calling LoadPlugins()

The MainForm calls the LoadPlugins() in the Form_Load event. The code is given below.

private void MainForm_Load(object sender, EventArgs e)

{

string path = GetExecutionFolder();

var plugins = _pluginLoader.LoadPlugins(path);

if (plugins.Count == 0)

MessageBox.Show(“No Plugins found!”);

else

{

foreach (IPlugin plugin in plugins)

{

Button button = new Button() { Width = 200, Height = 40, Left = 2, Top = PluginsPanel.Controls.Count * 40};

button.Text = plugin.Text;

button.BackColor = plugin.BackColor;

button.Tag = plugin;

button.Click += new EventHandler(button_Click);

PluginsPanel.Controls.Add(button);

}

}

}

If no plugins are found a message box is shown. If plugins found, each plugin instance will be iterated and a button is created to represent the plugin. The button is given backcolor based on the plugin.BackColor property. The button’s tag will be setted with the plugin instance for future use.

The button created will be having a Click event which is described below.

void button_Click(object sender, EventArgs e)

{

IPlugin plugin = (sender as Button).Tag as IPlugin;

plugin.Form.Show();

}

From the button click method above, we can see the tag property of button is used to get the exact Plugin instance and the plugin.Form.Show() method is called.

Adding a New Plugin

For creating a new plugin the following steps are needed.

1) Create a new project with name ending with *Plugin.dll. For time being I am using a project named GreenForm.Plugin class library project. Make sure you add reference to System.Windows.Forms and System.Drawing and Plugin.Interface project.

2) Add a class named GreenFormPlugin implementing interface IPlugin. The code will look like below.

namespace GreenForm.Plugin

{

public class GreenFormPlugin : IPlugin

{

public string Text

{

get { return “A Green Form”; }

}

public Form Form

{

get

{

return new Form() { Text = this.Text,

BackColor = Color.Green };

}

}

public Color BackColor

{

get { return Color.Green; }

}

}

}

3) Build the project and copy the Green.Plugin.dll to bin\debug folder of the Plugin.Container project.

4) Run the Plugin.Container.exe from the same folder and you should see the button listed there

clip_image006

Note

In the current example, we are loading plugin by directly parsing the application folder. In real world applications, we should be able to locate plugins in different folder. A configuration file with various plugin locations could be apt for this case.

Summary

In this article we have discussed the implementation part of Plugin loader works as well as adding a new plugin. For real business applications we can reduce the deployment package size by including only the required plugin files chosen by customer. Still the customer has the flexibility to choose additional plugins and we need to only deploy the needed file.

You can download the application from c-sharpcorner.com:

http://www.c-sharpcorner.com/uploadfile/40e97e/creating-a-plugin-enabled-application-part-2-of-2/

Advertisements

Posted in C# | Tagged: , , , , | 11 Comments »

Creating a Plugin enabled Application : Part 1 of 2

Posted by JP on May 4, 2011

 

In this article I am trying to show how to create a plugin enabled application using .Net Reflection. The plugin enabled application loads an associated piece of software during the runtime. There should not be any design time binding for the specified component.

Advantages of Plugins

We can see lots of plugin enabled applications for example Winamp. The plugin enable application provides the following flexibilities:

– Reduced Size in Initial Deployment

– Incrementing the modules as plugins

– Customers can be benefited by choosing Plugin Modules thus reduction in Cost

How Plugins works?

There will be 3 components in a plugin architecture application.

1) Plugin Container

2) Plugin Interface

3) Plugins

The Plugin Container will serve as the host holding all plugins found in runtime.

The Plugin Interface provides as a standard of communication between the Container and Plugins.

The Plugins are the actual piece of software which are located and loaded by the Plugin Container.

Execution

The plugin enabled application executes as depicted in the image below. After the application is executed, it will search for the plugins of associated interfaces. If any plugins found they are instantiated and loaded into the application.

clip_image002

Diving into the Code

I hope we had enough of theories. Now let us focus on how to build the actual code. In our example, we are trying to load plugins which contains forms. There is one Plugin.Core application which will perform as the Plugin Container and n number of plugin dll’s which contains the forms.

Each form found will be having a display name so that we can show it in the toolbar.

Component 1: Plugin Container .exe

This is a Windows Forms Application containing the Main Form to contain the plugins.

Component 2: Plugin. Interface.dll

This would be a Class Library containing the IPlugin interface.

public interface IPlugin

{

string Text { get; }

Form Form { get; }

Color BackColor { get; }

}

There will be a PluginLoader class which basically takes care of finding the plugins in given folder and loading them into the system.

Component 3: BlueForm.Plugin.dll

This will be a Class Library project containing the plugins. The plugins are nothing other than classes which implements the IPlugin interface.

In our project, we are creating a plugin class named BlueFormPlugin which holds a form.

Note

For the time being I have used Windows Forms for plugin example, but we can extend the same idea to ASP.NET, WPF, Windows Mobile applications too.

Summary

In this article the basic components of building a the Plugin based application is explained. In the next part we can see the inner details of how the plugin can be identified and loaded into the application using Reflection.

Posted in C# | Tagged: , , , , | Leave a Comment »

Reflection Comparer in C#

Posted by JP on December 30, 2010

I would like to share a piece of code that can save good amount of time.

Scenario

You are having a Customer class with properties Ag, Dob, Name which are int, datetime and string respectively.

The Customer objects are stored in a list and for custom sorting them based on different properties, we need to write code by implementing IComparer.  This could be a tedious task if lots of UI screens are there and you need to write specific code for each screen.  The problem is worse if you wanted to sort ascending/descending and based on multiple properties.

image

Solution

You can accomplish the above by using the following class which implements IComparer interface.

image

The above class ‘ReflectionComparer’ has constructor which takes the property on which the object to be sorted.

The constructor takes multiple property names so that we can sort on multiple items.  There is one overloaded constructor too, which takes the SortOrder (Ascending/Descending).

Invoking

You can just call the Sort method of the array/ArrayList and pass the instance of above class to it.

Eg: list.Sort(new ReflectionComparer("Age"));

ReflectionComparer Class Explained

It contains 2 constructors which are explained above.

There is one core method named Compare() which takes arguments x and y.

public int Compare(object x, object y)

For each objects passed – the corresponding CompareTo() method is called using Reflection.

The method GetMethodInfo() returns the “CompareTo” method info object.  Some optimizations were used there to reuse the same CompareTo() MethodInfo object to speed up the sorting process.

The method GetValue() returns the value of property to be compared.  Here also Reflection is the underlying mechanism.

Test Application

The attachment contains the test application which was created in Visual Studio 2008.  The test application provides us the UI to test various scenarios and their corresponding time taken for execution in seconds.

image

Summary

Eventhough Reflection comparison will be slower than normal comparisons, we can judiciously use the code above if the sorting speed is within our expectations.  The idea for the above comparer class is to minimize the coding effort on doing the comparison code infra.   In the above screen shot, 10 thousand records were sorted within 1 second in my computer having 3GHz speed.  Reflection is slower but your computer is many times faster than your requirement – then it is a right choice to use the code above.  Happy Coding.

Source

You can download the source from: www.c-sharpcorner.com rocks!!

Posted in Reflection | Tagged: , , , | Leave a Comment »

C# Generic Method for Parsing Value Types

Posted by JP on October 8, 2010

In this article I am going to explain how to create a generic method named GetValue() that can parse int, float, long, double data types. The method would parse any value objects passed through it and prevents throwing errors in case of invalid data. When invalid data is passed the method will return the default value of type passed.

Code Detailed

The int, long, float and double data types contains TryParse() and Parse() methods in common which are static. Our GetValue() method takes the object to be parsed and identifies the return type through Generic Argument T.

In case of errors like the TryParse or Parse methods are missing in the type T, an exception is thrown. In case of errors data errors like argument is null or invalid data the default(T) would be returned.

public T GetValue<T>(object obj)
{
   
if (obj != null
)
    {
       
Type type = typeof
(T);
 
        T value =
default
(T);
       
var methodInfo = (from m in type.GetMethods(BindingFlags.Public | BindingFlags
.Static)
                         
where m.Name ==
"TryParse"
                          select
m).FirstOrDefault();
 
       
if (methodInfo == null
)
           
throw new ApplicationException("Unable to find TryParse method!");

        object result = methodInfo.Invoke(null, new object[] { obj, value });
       
if ((result != null) && ((bool
)result))
        {
            methodInfo = (
from m in type.GetMethods(BindingFlags.Public | BindingFlags
.Static)
                             
where m.Name ==
"Parse"
                              select m).FirstOrDefault();

            if (methodInfo == null)
               
throw new ApplicationException("Unable to find Parse method!");

            value = (T)methodInfo.Invoke(null, new object[] { obj });

            return (T)value;
        }
    }
 
   
return default
(T);
}

Invoking the Method

The following code can be used to invoke the above method:

int i = _util.GetValue<int>("123");

long j = _util.GetValue<long>("abcd");

float k = _util.GetValue<float>("100.01");

double l = _util.GetValue<double>("200.20");

Point of Note

.Net Reflection is used to implement this method. I would like to say that in case fast performance is required, this method is not advisable.

Posted in Reflection | Tagged: , , , | Leave a Comment »