views:

163

answers:

5

I am trying to create instance of class by using reflection in ASP.net web site. Class ClassName is defined and located in App_code folder. Following line returns null, what could be wrong.

Type type = Type.GetType("NameSpace.ClassName", false, true);
+6  A: 

Supplying only the type name works only in the following scenarios:

  • The type in question is in the currently executing assembly (i.e., the same assembly as your code)

OR

  • The type in question is in mscorlib.dll.

In all other cases, you have to supply the assembly-qualified name of the type. This is what allows it to locate the appropriate assembly and load it.

Adam Robinson
Yes, NameSpace.ClassName is located in same assembly where this code is.
afin
In this case the assembly name should be `App_Code.dll` or `__code.dll`
Programming Hero
@afin: No. It's in the same project, but it's compiled to a different assembly.
SLaks
@Hero: What should I do to get the type of given string
afin
@SLaks: It's in the same project and same assembly. ie., class is defined and class file located at App_folder and this code is in another PageController.aspx.cs file. both of them are in same assembly.
afin
@Afin: Yes, but the two files get compiled to different assemblies.
SLaks
@SLaks: You are correct, even though files are in same web site it may compiled to differenct assemblies. I got to know this by seeing the AssemblyQualified name.
afin
@Adam: Thank you for your solution.
afin
A: 

Try using Assembly.GetType(), that should look in the referenced assembly. Of course, you'd need to load the appropriate Assembly class, which would be GetCallingAssembly() if the type shares assembly with your executing code, or something else otherwise, in which case you'd use one of the static LoadSomething() methods within the Assembly class.

For instance:

Type type = LoadFrom("App_code\ClassAssembly.dll").GetType("Namespace.ClassName");
// Load assembly, then type!
Type type2 = GetCallingAssembly().GetType("Namespace.ClassName");
// If it's the same assembly as the calling code.
Kyte
+2  A: 

Afin, try this. This is to piggyback on Adam Robinson's answer and to demonstrate what you need to do to test the statements made in the answer and the comments for yourself.

Type t = typeof(YourNamespace.YourClass);
string assemblyQualifiedName = t.AssemblyQualifiedName;
Type type = Type.GetType(assemblyQualifiedName, false, true);
// type will not be null

The assembly qualified name will be something like "Sample.Foo, App_Code.qwijwhsy, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null".

Anthony Pegram
I do believe typeof("something") would return type String...
Kyte
Had an errant pair of quotes. Removed.
Anthony Pegram
@Kyte: typeof definitely returns instance of System.Type.
CodeRipper
Just a thought - won't t and type be the same?
CodeRipper
@CodeRipper, yes. The purpose was to demonstrate to the OP what was required to get the type via a string name (re: the assembly qualified name), not to show anything fancy. t and type are the same, just obtained different ways (the second being the method the OP was trying to achieve).
Anthony Pegram
@Anthony: It works cool. AssemblyQualifiedName for current website assembly is "MyClassName, App_Code.pz-wmjud, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" Since I need to create instance for given classname string can I code as followsType type = Type.GetType("MyClassName" + ", App_Code.pz-wmjud, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",false,true);I am concerning whether the string "App_Code.pz-wmjud" will be the constant or it may vary depending on deployement/web server/framework
afin
@Anthony Pegram: I just think that building that string by hand isn't best practice. I posted IMHO cleaner approach.
CodeRipper
@CodeRipper, that wasn't the point. Like I said in the answer, it was just piggybacking on another answer, but a comment would not have been sufficient for the text and code format. @afin, the string name can change if you modify the code in the application. If you change code in App_Code, the application will recompile and you could end up with a different name. There may be a mechanism to guarantee the same name, but I do not know of it.
Anthony Pegram
@afin, however, if you know of a type in the assembly, say, Sample.Foo, you can still get at your other type dynamically. Something like `Type type = typeof(Sample.Foo).Assembly.GetType("Sample.SomeOtherType");` Sample.Foo is your way in to grab any other type within the assembly by its simpler name.
Anthony Pegram
@Athony: Thank you, I thought the same.
afin
A: 

Very simple code. I think that it doesn't need any explaining.

using System.Reflection;

Assembly assembly = Assembly.LoadFrom("MyLibrary.dll");
Type type = assembly.GetType("NameSpace.ClassName", false, true);
CodeRipper
A: 

if its in App_Code try

Type t = Type.Get("Namespace.ClassName, __code");
prabir