By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

Using Generics in Dynamic Code

P: n/a
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.

Aug 17 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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<Tand then implement an interface which you can use to abstract access
to it).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"HaySeed" <Ha*****@discussions.microsoft.comwrote in message
news:CE**********************************@microsof t.com...
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.
Aug 17 '07 #2

P: n/a
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<Twill 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<Tto 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 [.NET/C# MVP]" wrote:
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<Tand then implement an interface which you can use to abstract access
to it).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"HaySeed" <Ha*****@discussions.microsoft.comwrote in message
news:CE**********************************@microsof t.com...
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.
Aug 17 '07 #3

P: n/a

"HaySeed" <Ha*****@discussions.microsoft.comwrote in message
news:04**********************************@microsof t.com...
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<Twill 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<Tto 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<>).MakeGeneri cType("myClassName"));
>
Thanks

"Nicholas Paldino [.NET/C# MVP]" wrote:
>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<Tand then implement an interface which you can use to abstract
access
to it).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"HaySeed" <Ha*****@discussions.microsoft.comwrote in message
news:CE**********************************@microso ft.com...
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.

Aug 20 '07 #4

P: n/a

Slick - Thanks Ben

"Ben Voigt [C++ MVP]" wrote:
>
"HaySeed" <Ha*****@discussions.microsoft.comwrote in message
news:04**********************************@microsof t.com...
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<Twill 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<Tto 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<>).MakeGeneri cType("myClassName"));

Thanks

"Nicholas Paldino [.NET/C# MVP]" wrote:
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<Tand then implement an interface which you can use to abstract
access
to it).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"HaySeed" <Ha*****@discussions.microsoft.comwrote in message
news:CE**********************************@microsof t.com...
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.



Aug 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.