SIGN UP MEMBER LOGIN:    
ARTICLE

Loading Assembly Dynamically and Calling Method (Modular Programming)

Posted by Aravind BS Articles | Silverlight with C# December 12, 2011
Modular programming with silverlight is a really interesting topic. Because, Modularizing 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.
Reader Level:

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

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

Hi, It can be used in modular programming(especialy in silverlight). Adding all dll/assembly(Any API dll) to project could be hit performance. So, on demand you can load that assembly and unload once its work is done..

Posted by Aravind BS Dec 15, 2011

Can you highlight an area where this applies to? Thanks

Posted by knights Dec 14, 2011

Very helpful article. Thanks for sharing it.

Posted by Alok Pandey Dec 14, 2011

very innovative.

Posted by Sonakshi Singh Dec 14, 2011

Really very helpful article..

Posted by Akash Ahlawat Dec 14, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor