.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Dynamic Loading of objects using XML - Part 2

Posted By:Mahadesh Mahalingappa       Posted Date: August 06, 2011    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

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 .

Dynamic Loading of objects using XML  - Part 2:

Objective of this article:

In this article I try to extend what I explained in the Part 1 posted here
http://www.dotnetspark.com/kb/4219-dynamic-loading-objects-using-xml.aspx .

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 .

Lets get on the job .

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

Then I go ahead and add a class library project to my solution and I name it as DI. Lets just delete the class that's 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 .

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;

  return null;



As can be seen that 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 can not 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 });
Finally lets run our XMLReader Project .
Great Works . 
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend