Click here to Skip to main content
15,905,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Assembly a = Assembly.Load("Dll file");
 Type theType = a.GetType("ClassName");   
dynamic agDrvr = Activator.CreateInstance(ClassType);

this is the common function to create the instance of some Class by use reflection

in the above of common function ,I just know the class name ,not the class type ,

so how can I make the result of the [Activator.CreateInstance(ClassType)] from the

type of Object to the type of class whitch name is [className];
Posted
Updated 12-Dec-10 23:52pm
v2
Comments
Toniyo Jackson 13-Dec-10 5:52am    
Always put your code inside code block

1 solution

You could use this:
Activator.CreateInstance("Dll file", "ClassName");


You can get the assembly name (firs parameter of CreateInstance above) from the Assembly object opr could also directly create an instance using that Assembly object:
Assembly a = Assembly.Load("Dll file");
MyObject o = a.CreateInstance("ClassName");

http://msdn.microsoft.com/en-us/library/dex1ss7c.aspx[^]

Good luck!
 
Share this answer
 
Comments
lmhf004 13-Dec-10 6:24am    
But I don't know the type of [MyObject ]
E.F. Nijboer 13-Dec-10 7:25am    
Ah, ok. In that case you can simply use the GetTypes method of Assembly to traverse all of the defined types.
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900