473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using Generics in Dynamic Code

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
4 1998
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
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

"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
4
by: consumer62000 | last post by:
Hello All I found some *issue* with the generics in C#. It looks like an issue to me : In C++ the following code compiles fine class Person { public: char last;
4
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
2
by: joseph_gallagher | last post by:
Hi, I've been playing with generics and I was wondering if anyone could tell me why the following doesnt work, and if there is another way to do it public class A<Twhere T : B, new() {...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.