Pages

Search

Wednesday, February 22, 2012

Invoking Member from a class using AssemblyQualifiedName

Generally, we invoke a instance method creating an object of the required class. In case of a static method, the method can be invoked using the class name.
Using “AssemblyQualifiedName”, it will be possible to invoke members of a class which belongs to given assembly name.
An assembly qualified name looks like
SubLib.Class1, SubLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Since, it is possible to invoke members of a class using its qualified name, it is much easier to add new business components to a system with the help of factory deisgn pattern and using the assembly qualified name.
Below example helps to understand invoking members from a class using qualified name.
Interface : MainLib.Int1
Class : MainLib.MessageInfo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MainLib
{
public interface Int1 : IDisposable
{
void Message();
void Message(MessageInfo argMessageInfo);
}
public class MessageInfo
{
public MessageInfo()
{
}
public string Message;
}
}

Class : Class1 implementing Int1
Class : Class3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MainLib;
using System.Windows.Forms;
using System.Collections;

namespace SubLib
{
public class Class1 : Int1
{
public Class1()
{
MessageBox.Show("Hi this is instance cons");
}

public Class1(MessageInfo argMessageInfo)
{
Message(argMessageInfo);
}

static Class1()
{
MessageBox.Show("Hi this is static cons");
}

public void Message()
{
MessageBox.Show("Hi This is class1");
}

public void Dispose()
{

}

public static void Msg()
{
MessageBox.Show("Hi this is static msg");
}
public void Message(MessageInfo argMessageInfo)
{
MessageBox.Show(argMessageInfo.Message);
}
}

public class Class3
{
public string str;
public string Mem1
{
get;
set;
}
private string Mem2
{
get;
set;
}

public void set_Mem1()
{
}
public Class3()
{
MessageBox.Show("Cons 1");
}
public Class3(int a)
{
MessageBox.Show("int Cons 1");
}
public Class3(string a)
{
MessageBox.Show("str Cons 1");
}
public Class3(MessageInfo oinfo)
{
MessageBox.Show("MessageInfo Cons 1");
}
}
}

Below code is to understand the invocations using Reflection assembly.

try
{
//SubLib.Class1 and MainLib.MessageInfo qualified names
string strClass1Type = "SubLib.Class1, SubLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
string strMessageInfoType = "MainLib.MessageInfo, MainLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

//Getting the type based on qualified name
Type messageInfoType = Type.GetType(strMessageInfoType);

//Creating an instance or object for MessageInfo Class (calling its constructor)
MessageInfo obj = messageInfoType.InvokeMember(null, BindingFlags.CreateInstance, null, null, null) as MessageInfo;

//setting "Message" Property
messageInfoType.InvokeMember("Message", BindingFlags.Instance | BindingFlags.SetField | BindingFlags.Public, null, obj, new object[] { "Test Message" });

//Getting the type based on qualified name
Type classType = Type.GetType(strClass1Type);
//Calling the "Msg" static function from Class1
classType.InvokeMember("Msg", BindingFlags.InvokeMethod, null, null, null);

//Invoking the Class1 constructor
ConstructorInfo objINfo = classType.GetConstructor(System.Type.EmptyTypes);
Int1 objInt1 = objINfo.Invoke(null) as Int1;

//calling the "Message" function directly from the object
objInt1.Message();

//Invoking the "Message" function
classType.InvokeMember("Message", BindingFlags.InvokeMethod, null, objInt1, null);

//Invoking the Class1 Parameterised constructor
objINfo = classType.GetConstructor(new Type[] { obj.GetType() });
objInt1 = objINfo.Invoke(new object[] { obj }) as Int1;

//calling the "Message" function directly from the object
objInt1.Message();

//Creating an instance or object for Class1 Class (calling its constructor)
objInt1 = classType.InvokeMember(null, BindingFlags.CreateInstance, null, null, new object[] { obj }) as Int1;
objInt1.Message();


//Class3 qualified name
string strClass3Type = "SubLib.Class3, SubLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

//getting the type
Type Class3Type = Type.GetType(strClass3Type);

//Creating an instance or object for Class3 Class (calling its constructor) and sending the message info object after setting the Message Property
object objClass3 = Class3Type.InvokeMember(null, BindingFlags.CreateInstance, null, null, new object[] { new MessageInfo() { Message = "hello" } });

//setting the "str" public variable
Class3Type.InvokeMember("str", BindingFlags.SetField, null, objClass3, new object[] { "hi srinivas" });

//setting the "Mem1" public property
Class3Type.InvokeMember("Mem1", BindingFlags.SetProperty, null, objClass3, new object[] { "hello" });

//setting the "Mem2" private property
Class3Type.InvokeMember("Mem2",
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, objClass3, new object[] { "hello private" });

}
catch (Exception exp)
{
MessageBox.Show(exp.Message, "Error");
}




Reference: http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx

No comments:

Post a Comment