Loading Assembly Dynamically and Calling Method (Modular Programming)


Modular programming with silverlight is a really interesting topic. Because, Modularizing a program plays a very important role while developing rich internet applications. As applications grow, there could be possibilities of performance hit due to not handling correctly.

So, I tried to elaborate it here.

Step 1:

Create a Silverlight 4 application and I have named it as ARVTestModuleLoading

Step 2:

Add a Class library application to the ARVTestModuleLoading solution and I have named it here as ARtModule.

MethodSil1.gif

I have added one more class called ARVClass. Also I have wrotten one method called "GetName"

So, here my intention is to call this method using a load-on-demanad method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///<Summary>
///
Author:Aravind
///Module:ArvClass
///Created On:
///Modified On:
///Modified By:
///</Summary>
namespace ARVModule
{
    //Class to have some data which will be accessed from other class.
    public class ArvClass
    {
        public string GetName()
        {
            return "Arvind does't run behind the money, Money runs behind Aravind";
        }
    }
}


Step 3:

Build this application. Once you build the application, dll will be created under bin folder(ARVModule).

Step 4:

Create one folder called "Control" and copy ARVModule.dll to that folder.

MethodSil2.gif

Step 5:

In ARVTestModuleLoading Silverlight application, write a code for load assembly dynamically.

Copy the "ARVModule.dll" path(it should be URl) as given bellow. Here, I have found like this http://localhost:49805/Control/ARVModule.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
///<Summary>
///
Author:Aravind
///Module:MainPage
///Created On:
///Modified On:
///Modified By:
///</Summary>
namespace ARVTestModuleLoding
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            var WbClnt = new WebClient();
            WbClnt.OpenReadCompleted += (a, b) =>
                {
                    if (b.Error == null)
                    {
                        AssemblyPart assmbpart = new AssemblyPart();
                        Assembly assembly = assmbpart.Load(b.Result);
                        Object Objclss = assembly.CreateInstance("ARVModule.ArvClass");
                        //Type type = System.Reflection.Assembly.LoadFrom(assembly.ManifestModule.ToString()).GetType(assembly.ManifestModule.ToString()+"."+"ArvClass");        
                        if (Objclss != null)
                        {

                            //Calling method name.
                         MethodInfo ob=  Objclss.GetType().GetMethod("GetName");
                        object obsmsg= ob.Invoke(Objclss, null);
 
                        MessageBox.Show(obsmsg.ToString());                           

                        }
                    }
                };
            WbClnt.OpenReadAsync(newUri("http://localhost:49805/Control/ARVModule.dll",UriKind.Absolute));
          
        }
    }
}


Basically, here it will load the assembly first and then create' instance of that assembly. GetMethod returns method info of the given method. If we doesn't know the method name, then we can use GetMethodNamee() method which returns all the method's info.

Invoke accepts two parameters. One is object which contains that method and other one is parameter.

Step 6:

Run the Silverlight application. You will get alert message.

MethodSil3.gif