GetType not working?

  • Thread starter Thread starter Darren Clark
  • Start date Start date
D

Darren Clark

I am having some troubles with loading up a type dynamically.

From what i have read

Type myType = Type.GetType("WB.DataAccess.FAQ");

is the exact same as

Type myType = typeof(WB.DataAccess.FAQ);


HOWEVER..... in practise it doesnt quite work..... using typeof always
works.... however using the GetType doesnt it will set
myType to be null or undefined.

What am i doing wrong? i need to load up a type from a string value.
 
Darren

try this.

Type myType = myAssembly.GetType("WB.DataAccess.FAQ");

myType wont be null..


Shak.
 
hi,

probably GetType() requires the run type expression of a type, can you try
something like

MyType t1 = new MyType();
Type t = t1.GetType();

rgds
Joyjit
 
Hi Darren,

I think that in your case myType is set to "null".
Probably, the reason is that this type is stored in
assembly that did not load.

Look at: Assembly.Load in .NET docs.

Regards

Marcin
 
MyNews said:
probably GetType() requires the run type expression of a type, can you try
something like

MyType t1 = new MyType();
Type t = t1.GetType();

No, the method being talked about here is the static method
Type.GetType(string).
 
Darren Clark said:
I am having some troubles with loading up a type dynamically.

From what i have read

Type myType = Type.GetType("WB.DataAccess.FAQ");

is the exact same as

Type myType = typeof(WB.DataAccess.FAQ);

No, it's not. Type.GetType(string) will, when given just the type name
and no assembly information, only look in mscorlib and the current
assembly. typeof actually uses completely different (and more
efficient) IL, I believe.

If you need to fetch a type from another assembly, either use
Assembly.GetType or call Type.GetType with the assembly name in the
string as well.
 
Back
Top