Using Generics in Dynamic Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there some way to use Generics in dynamic code using the

Type.GetType("MyClassName")

as an argument?

List<Type.GetType("MyClassName") > oList = new
List<Type.GetType("MyClassName") >

.... or is there a way to declare a generic using strings that hold class
names that have been pulled from XML at run time.
 
HaySeed,

No, there isn't. You will have to use reflection to create the generic
type (from the generic Type, and the Type you want to use as a type
parameter). You would also have to use relfection to access the members,
since you can not place an interface on List<T> (unless you derive from
List<T> and then implement an interface which you can use to abstract access
to it).
 
Nicholas

Thanks for the response. however, I'm not able to follow. Since List<T>
cannot accept List<typeof(myClassName)> it must be requiring something other
than a Type object. I( have written a method that uses reflection to turn
the string "myClassName" into a Type much like like
Type.GetType("myClassName).

However the generic List<T> will not accept a method call or variable as the
argument for T.

The challenge here is that I am getting my class names through XML and must
now instatiate a series of Generics like List<T> to use against my
framework's method signatures.

Could you restate your last reply in code? I think it would help me
understand your your perspective.

Thanks

Nicholas Paldino said:
HaySeed,

No, there isn't. You will have to use reflection to create the generic
type (from the generic Type, and the Type you want to use as a type
parameter). You would also have to use relfection to access the members,
since you can not place an interface on List<T> (unless you derive from
List<T> and then implement an interface which you can use to abstract access
to it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HaySeed said:
Is there some way to use Generics in dynamic code using the

Type.GetType("MyClassName")

as an argument?

List<Type.GetType("MyClassName") > oList = new
List<Type.GetType("MyClassName") >

... or is there a way to declare a generic using strings that hold class
names that have been pulled from XML at run time.
 
HaySeed said:
Nicholas

Thanks for the response. however, I'm not able to follow. Since List<T>
cannot accept List<typeof(myClassName)> it must be requiring something
other
than a Type object. I( have written a method that uses reflection to turn
the string "myClassName" into a Type much like like
Type.GetType("myClassName).

However the generic List<T> will not accept a method call or variable as
the
argument for T.

The challenge here is that I am getting my class names through XML and
must
now instatiate a series of Generics like List<T> to use against my
framework's method signatures.

Could you restate your last reply in code? I think it would help me
understand your your perspective.

IList list = (IList)
Activator.CreateInstance(typeof(List said:
Thanks

Nicholas Paldino said:
HaySeed,

No, there isn't. You will have to use reflection to create the
generic
type (from the generic Type, and the Type you want to use as a type
parameter). You would also have to use relfection to access the members,
since you can not place an interface on List<T> (unless you derive from
List<T> and then implement an interface which you can use to abstract
access
to it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

HaySeed said:
Is there some way to use Generics in dynamic code using the

Type.GetType("MyClassName")

as an argument?

List<Type.GetType("MyClassName") > oList = new
List<Type.GetType("MyClassName") >

... or is there a way to declare a generic using strings that hold
class
names that have been pulled from XML at run time.
 
Slick - Thanks Ben

Ben Voigt said:
HaySeed said:
Nicholas

Thanks for the response. however, I'm not able to follow. Since List<T>
cannot accept List<typeof(myClassName)> it must be requiring something
other
than a Type object. I( have written a method that uses reflection to turn
the string "myClassName" into a Type much like like
Type.GetType("myClassName).

However the generic List<T> will not accept a method call or variable as
the
argument for T.

The challenge here is that I am getting my class names through XML and
must
now instatiate a series of Generics like List<T> to use against my
framework's method signatures.

Could you restate your last reply in code? I think it would help me
understand your your perspective.

IList list = (IList)
Activator.CreateInstance(typeof(List said:
Thanks

Nicholas Paldino said:
HaySeed,

No, there isn't. You will have to use reflection to create the
generic
type (from the generic Type, and the Type you want to use as a type
parameter). You would also have to use relfection to access the members,
since you can not place an interface on List<T> (unless you derive from
List<T> and then implement an interface which you can use to abstract
access
to it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Is there some way to use Generics in dynamic code using the

Type.GetType("MyClassName")

as an argument?

List<Type.GetType("MyClassName") > oList = new
List<Type.GetType("MyClassName") >

... or is there a way to declare a generic using strings that hold
class
names that have been pulled from XML at run time.
 

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