Sample Code
public void FindContainingAssembly(string strName) { foreach (System.Reflection.Assembly a in System.AppDomain.CurrentDomain.GetAssemblies()) { try { string[] strAssemblyName = a.FullName.Split(new char[]{','}); string strNameTemp = strAssemblyName[0] + "." + strName; if (System.Activator.CreateInstance(a.FullName, strNameTemp) != null) { // Creating Instance Sample a = (Sample)System.Activator.CreateInstance(a.FullName, strNameTemp)
} } catch (System.Exception) { continue; } }
}
Description :
In the above function strName is the Name of the Class in any of the assemly which You passing as a parameter Ex: Sample
foreach: statement reads all the Assemblies in the Current Domain(Solution)
Assembly's Full name in the following format : "<Assemlyname>, Version=somthing, Culture=something,PublicToken=something"
strNameTemp: Concating <Assemlyname> with the strName(Class Name)
System.Activator.CreateInstance : is Checking the strNameTemp is existing in the Assemply or not , if exist u can create instance of the class dynamically by casting with the class name
I hope this article give u some idea. good luck everybody.
|
| Author: critic 02 Jul 2004 | Member Level: Bronze Points : 0 |
Can you kindly refine the sample code? What is the purpose of the 'm_ContainingAssembly' ? Please make it more clear.
|
| Author: Sweet Cousin 02 Jul 2004 | Member Level: Bronze Points : 0 |
Thanks man the programme works fine.. and was really helpful.
|