Get List<T> for a generated type

  • Thread starter Thread starter Peter Morris
  • Start date Start date
P

Peter Morris

Hi all

I am using TypeBuilder to generate an assembly dynamically, a property on
one of the classes needs to be List<OtherGeneratedClass>. I am trying the
code in Jon Skeet's book

Type.GetType("System.Collections.Generic.List`1[System.String]")

which works fine, but when I need to use the other class....


Type otherClassType = otherTypeBuilder.UnderlyingSystemType;
Type genericType =
Type.GetType(string.Format("System.Collections.Generic.List`1[{0}]",
otherClassType.FullName));

I always get NULL as the return type. Any ideas?



--
Pete
=========================================
I use Enterprise Core Objects (Domain driven design)
http://www.capableobjects.com/
=========================================
 
I am using TypeBuilder to generate an assembly dynamically, a property on
one of the classes needs to be List<OtherGeneratedClass>. I am trying the
code in Jon Skeet's book

Glad to hear it :)
Type.GetType("System.Collections.Generic.List`1[System.String]")

which works fine, but when I need to use the other class....

Type otherClassType = otherTypeBuilder.UnderlyingSystemType;
Type genericType =
Type.GetType(string.Format("System.Collections.Generic.List`1[{0}]",
otherClassType.FullName));

I always get NULL as the return type. Any ideas?

Yup - use the assembly qualified name instead of just FullName. It's
the same as anything other call to Type.GetType(string) - if you don't
specify the assembly, it only checks in mscorlib and the currently
executing assembly. The only difference here is that you've got two
different types involved (List<T> and its element type).

Do you have a particular reason to use Type.GetType(string) instead of
building the constructed type from the generic type? For instance:

Type genericType = typeof(List<>).MakeGenericType(otherClassType);

I can't test it right now, but I think that should work.

Jon
 
Type genericType = typeof(List<>).MakeGenericType(otherClassType);

It does, thanks very much!




--
Pete
=========================================
I use Enterprise Core Objects (Domain driven design)
http://www.capableobjects.com/
=========================================
 

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