Type.GetType

  • Thread starter Thread starter Rene
  • Start date Start date
R

Rene

Well the documentation says that you are supposed to pass the
"AssemblyQualifiedName" however, would you mind posting the chunk of code
that reproduces the behavior you are seeing. I am only asking because I
tried passing the type name just like your example and it worked so I am
curious what you are doing to run into this issue.
 
Type asdf = Type.GetType(t.GetType().FullName);

How come something like that returns a null type when t is a generic type?
It works fine for non-generic types.

Could you post a short but complete example? Two things come to mind:

1) FullName returns the name of the type but not the assembly.
Type.GetType only looks in mscorlib and the currently executing
assembly if you don't include the assembly name.

2) From memory, Type.FullName may not give generics back in the same
format as you need to ask for them with Type.GetType. However, I can't
easily test this. (Annoyingly enough, I know it's in my book in
chapter 3, but I don't have that with me either...) Print out
t.GetType().FullName and see what it looks like. Type.GetType() has
fairly complete MSDN documentation around what it should look like for
generic types. Note that the type arguments need to be appropriately
qualified too.

As always, a short but complete program demonstrating the problem
would be welcome :)

Jon
 
Type asdf = Type.GetType(t.GetType().FullName);



How come something like that returns a null type when t is a generic type?
It works fine for non-generic types.
 
Jon Slaughter said:
Type asdf = Type.GetType(t.GetType().FullName);



How come something like that returns a null type when t is a generic type?
It works fine for non-generic types.

heres the test code that produces the error:

s =

System.Collections.Generic.LinkedList`1[[System.Object, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

static void Main(string[] args)
{
LinkedList<object> l = new LinkedList<object>();
l.AddLast("sadf");
l.AddLast(34);

string s = l.GetType().FullName;
Type t = Type.GetType(s);

return;
}
 
Type asdf = Type.GetType(t.GetType().FullName);
How come something like that returns a null type when t is a generic type?
It works fine for non-generic types.

heres the test code that produces the error:

s =

System.Collections.Generic.LinkedList`1[[System.Object, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

Okay, and that's because LinkedList<T> is in the System assembly,
which is neither mscorlib nor the assembly you're calling Type.GetType
from - so you need to qualify the type name with the assembly. Use
Type.AssemblyQualifiedName instead of Type.FullName and it should
work.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top