Click here to Skip to main content
Click here to Skip to main content

Tagged as

Dynamic Loading of Objects using XML - Part 2

, 1 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
In Part 2 of Dynamic Loading of objects, I have created a custom Dependency Injection Framework

Objective

In this post, I try to extend what I explained in Part 1.

The objective is to design a custom Dependency Injection Framework. I personally like what Unity Framework does. But I would like to do the job myself.

Let's get on the job.

Continuing from Part I, the first step I did was do some code refactoring. This just makes the code a bit more readable and efficient.

Then I go ahead and add a class library project to my solution and name it as DI. Let's just delete the class that gets created with it.

I move the Container class from the existing project to the DI project.

I build the DI project and add a reference to the XMLReader project.

Now my solution looks as below:

image001.jpg

Container class is as shown below:

using System;
using System.Xml;
using System.Reflection;

namespace DI
{
  public static  class Container
    {
      public static Type CreateInstance()
      {
          var reader = new XmlTextReader
			(@"C:\Mahadesh\Blogs\SimpleDI\XMLReader\XML\Sample.xml");
          var assem = Assembly.GetCallingAssembly();

          while (reader.Read())
              if (reader.NodeType == XmlNodeType.Element)
              {
                  Type type = null;
                  while (reader.MoveToNextAttribute()) // Read attributes

                      if (reader.Name == "Name")
                      {
                          type = assem.GetType(reader.Value); // typeName is a string
                      }

                  return type;
              }

          reader.Close();
          Console.ReadLine();
          return null;
      }
    }
}

As can be seen, this time around I use:

var assem = Assembly.GetCallingAssembly();  instead of GetExecutingAssembly();

This is because I am trying to access the XML from another class assembly. We cannot move the XML to another assembly since XML is part of the User configuration and should always reside on the client side. GetCallingAssembly() will load the assembly from which the call is being made, which in our case is XMLReader.

Well, that’s pretty much it .

There are no changes to Class A in XMlReader assembly... I have just removed unnecessary statements from the Program class in XMlReader assembly.

The code for the Program class in XMLReader looks as below:

using System;
using DI;

namespace XMLReader
{
    class Program
    {
        static void Main(string[] args)
        {
            var obj = Activator.CreateInstance( Container.CreateInstance());
            var m = Container.CreateInstance().GetMethod("SampleMethod");
            m.Invoke(obj, new Object[] { 42 });
            Console.ReadLine();
        }
    }
}

Finally let's run our XMLReader project.

image002.jpg

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 1 Jul 2011
Article Copyright 2011 by Mahadesh Mahalingappa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid