473,327 Members | 2,055 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,327 software developers and data experts.

Get the class of a Type C#

Hey All,
I was just wondering if this is possible.

I have a List<x> object, and from this, I need to be able to get x. and then use x's class as the generic type for another method.

ie.

private R GetData<R>() {


//this should get me the type of the generic. ie List<string>, this should return //string
Type listType = data.GetType().GetGenericArguments()[0];

//Call another method with the generic type of the list
MethodB<listType>(args);

}

How can this be done? Right now, I cannot specify the type in MethodB, as I think it wants the class. Is there any way to do this?

Thanks.
Apr 3 '08 #1
7 1213
Plater
7,872 Expert 4TB
All objects have a .GetType() that returns the Type for the object that they are.
Apr 3 '08 #2
When I try and call MethodB: MethodB<listType>, the compiler does not allow this, because it says:
Error 2 The type or namespace name 'listType' could not be found (are you missing a using directive or an assembly reference?

I think it's because it's expecting a class, like "string", "int" etc, I can't give it a type can I?
Apr 3 '08 #3
Plater
7,872 Expert 4TB
Well lets say you have:
Expand|Select|Wrap|Line Numbers
  1. Myclass fred = new Myclass();
  2.  
  3. MethodB<typeof(fred)>
  4. //or
  5. MethodB<fred.GetType()>
  6.  
Either of those should work.
Apr 3 '08 #4
Here is what I am trying to say, because I am not following your reply.

I have a list of string. ie. List<string>.

I want to call a method such that:
MethodB<string>();

But I won't know what the list type is until runtime. ie.

List<?> myList;

MethodB<?>;

If I get the generic Type of the list, that returns me a Type object.
ie. Type listType = data.GetType().GetGenericArguments()[0];

You cannot go List<listType>, because it seems to expect a class only.

Does that make sense?
Apr 3 '08 #5
Looks like you can do this through reflection and the calling of your own method. Anyone know of a more elegant solution?
Apr 3 '08 #6
Plater
7,872 Expert 4TB
Ok I see what is going on then. Yes, you should be able to do that using the System.Reflection namespace I think?
Apr 3 '08 #7
Ya, what I did was basically call the method again via reflection. I'm guessing that's really the only way to do it.

Thanks,
Nelson
Apr 3 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
3
by: George Sakkis | last post by:
I'm trying to write a decorator similar to property, with the difference that it applies to the defining class (and its subclasses) instead of its instances. This would provide, among others, a way...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
5
by: Rob | last post by:
In many articles related to VB.net the word "class" is used... How many meanings are there to this word ? "possible to derived a class from another" "forms are full-fledged classes" "base...
38
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.