Click here to Skip to main content
12,633,990 members (24,375 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 28-Nov-12 8:55am
Shafik505
Updated 28-Nov-12 9:39am
v9
Comments
Shafik 28-Nov-12 13:56pm
   
Here className Exactly my another Class Name.
Shafik 28-Nov-12 13:59pm
   
Exception: Value cannot be null.
Parameter name: type
ProgramFOX 28-Nov-12 14:02pm
   
You can edit your question with the "Improve question" button.
That's better then posting edits as comments.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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


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


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


This page might be useful as well... http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx[^]
  Permalink  
v2
Comments
Shafik 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.
Shafik 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.
Shafik 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?
Jason Gleim 28-Nov-12 16:06pm
   
Are you asking me why it doesn't work or are you just telling me thanks for answering that question?
Shafik 28-Nov-12 16:14pm
   
KY.Revenue.ReportsDisplay.dll my assemble. where i suppose to put full assemble name in Type typeClass = Type.GetType(className)
Jason Gleim 28-Nov-12 16:59pm
   
Did you read the message thread on StackOverflow that I referenced above? There is a discussion there of how to do this... but in a nutshell, GetType("type name, assembly name").
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Chances are that you haven't qualified your name sufficiently.
For example
Type type = Type.GetType("MyForm");
will return null, whereas
Type type = Type.GetType("MyNamespace.MyForm");
will return a valid type.
  Permalink  
Comments
Shafik 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.
Shafik 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


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100