Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am putting my Code.

selectReport = this.CreateReportController(_reportName + "Controller");//Calling

Method:

public IActionController CreateReportController(string className)
{



Type typeClass = Type.GetType(className);
string path = Assembly.GetAssembly(typeClass).Location;

Assembly asm = Assembly.LoadFile(path);
if (asm != null)
{
Object objTest = Activator.CreateInstance(Type.GetType(className));

IActionController reportController = (IActionController)objTest ;

return reportController;
}
else
{
return null;
}
}

typeClass and objTest is getting NULL. Do you have any Idea. How can I make object className(string) exactly Implentation class name.

Thanks,
Shafik
Posted
Updated 28-Nov-12 8:39am
v9
Comments
[no name] 28-Nov-12 13:56pm    
Here className Exactly my another Class Name.
[no name] 28-Nov-12 13:59pm    
Exception: Value cannot be null.
Parameter name: type
Thomas Daniels 28-Nov-12 14:02pm    
You can edit your question with the "Improve question" button.
That's better then posting edits as comments.

Try either the typed constructor:
C#
yourClass objTest = Activator.CreateInstance<yourclass>();


or properly using the GetType method:
C#
yourClass objTest = Activator.CreateInstance(yourClass.GetType());


or if you have an instance of the class as an object:
C#
yourClass objTest = Activator.CreateInstance(typeof(yourClassInstance));


This page might be useful as well... http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx[^]
 
Share this answer
 
v2
Comments
[no name] 28-Nov-12 14:35pm    
Thank you very much for ur reply. Can u plz loaak at my code
Jason Gleim 28-Nov-12 14:38pm    
Looks like if you change Type.GetType(className) to typeClass (so you are passing in the type you already resolved) it should work.
[no name] 28-Nov-12 14:45pm    
Thank you a lot. Type typeClass = Type.GetType(className); here typeClass gets NULL. How can i go further? I am Stuck at First Line. className is Exactly myAnother Class name which I want to make instance.
Jason Gleim 28-Nov-12 15:27pm    
Ok... so now that I look a little closer, I think you might have a problem. You can't GetType a class that is defined in a different assembly without including the name of the assembly in the string that you pass to the method. Have a look here: http://stackoverflow.com/questions/1825147/type-gettypenamespace-a-b-classname-returns-null

This might be a problem for you because it seems you are trying to use the class as a handle to find the assembly. So you are in a catch-22... you can't resolve the class type without the assembly name and you can't resolve the assembly name without the class type. You might have to approach this differently.

There are a couple of articles about locating and loading assemblies at runtime here on CP that might be a help. I have used an interface library before with a static class that would locate an assembly which implemented a given interface, load it, and instantiate concrete objects from that assembly. You might have to do something similar.
[no name] 28-Nov-12 16:04pm    
Thanks for your Answer. "Type typeClass = Type.GetType(className);" Type typeClass why i can not make my claasName(string) to Type?
Chances are that you haven't qualified your name sufficiently.
For example
C#
Type type = Type.GetType("MyForm");
will return null, whereas
C#
Type type = Type.GetType("MyNamespace.MyForm");
will return a valid type.
 
Share this answer
 
Comments
[no name] 28-Nov-12 14:34pm    
Thanks for Reply. I already Define The Namespace. Can U plz look at my code
OriginalGriff 28-Nov-12 14:44pm    
Your code does not show what you are setting as the value of "className" - if it is not qualified with an appropriately full namespace, then GetType returns a null. For the current executing assembly, the assemblyname.class is sufficeint, otherwise it must be the full qualified name.
[no name] 28-Nov-12 14:54pm    
Thanks Now I used: Type typeClass = Type.GetType("KY.Revenue.ATSReportsDisplay.Factory.ATSReportControl."+className);
Still i am not getting any value in typeClass
Do u have any other Idea

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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