473,322 Members | 1,398 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,322 software developers and data experts.

How to use a generic type passing the type parameter in a dynamic way?

Hi,
is there a way, maybe using reflection, to use a generic class passing the
type parameter dynamicly (not kwnowing the exact type at compile time)?
I tried the next example, but doesn't work:

public abstract class Vehicle
{ ... }

public class Aircraft : Vehicle
{ ... }

public class SimpleList<ATypewhere AType:Vehicle
{
....
}

call:
Aircraft Boeing747 = new Aircraft();
SimpleList<typeof(Aircraft)TheList = new
SimpleList<Boeing747.GetType()>();
....

Thanks,
Néstor.
Jan 17 '07 #1
1 4833
is there a way, maybe using reflection, to use a generic class passing
the
type parameter dynamicly (not kwnowing the exact type at compile
time)?
I tried the next example, but doesn't work:
public abstract class Vehicle
{ ... }
public class Aircraft : Vehicle
{ ... }
public class SimpleList<ATypewhere AType:Vehicle
{
...
}
call:
Aircraft Boeing747 = new Aircraft();
SimpleList<typeof(Aircraft)TheList = new
SimpleList<Boeing747.GetType()>();
...
Thanks,
The example that you presented doesn't make much sense (since you obviously
know that you're dealing with an Aircraft). However, you can use reflection
to create a generic type using a System.Type as the generic type argument:

Aircraft boeing747 = new Aircraft();

Type simpleListType = typeof(SimpleList<>);
Type boeingListType = simpleListType.MakeGenericType(boeing747.GetType() );

object theList = Activator.CreateInstance(boeingListType);

However, if you have to write code like that, you probably aren't using generics
in a way that makes sense. You might want to rethink your design.

Best Regards,
Dustin Campbell
Developer Express Inc.
Jan 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tom Bradford | last post by:
Let me first say that I'm sure that this subject has come up before, and so forgive me for beating a dead horse. Secondly, let me say that Python's strength is its dynamic nature, and I don't...
3
by: Otis Mukinfus | last post by:
I've been wrestling with this for a while and can't figure it out. I have a generic method with the following signature: //this compiles OK public abstract class DataMethod { public...
5
by: Anders Borum | last post by:
Hello! I'm trying to call a generic method by providing the T parameter at runtime, as opposed to compile time (i.e. by specifying the T parameter in the method call). I was under the impression...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
5
by: Michi Henning | last post by:
I can pass a generic collection as ICollection<Tjust fine: static void flatCollection(ICollection<intc) {} // ... List<intl = new List<int>(); flatCollection(l); // Works fine Now I...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: jon | last post by:
I'm trying to write a block of code that can create an instance of a generic object, with the Type coming in as a dynamic string. It isn't working yet. Any advice how I can dynamically create the...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
1
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....

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.