pass regular type to generic method

H

Henri.Chinasque

Hi all,

I've tried searching for this one, but can't seem to come up with the
proper terms.

I want to know how to pass a type to a generic method. Something like
this:

public void MyMethod(Type objType){
DoSomething<objType>();
}

This of course doesn't compile, any ideas what I can do to get it to
work?

DoSomething is just a generic method

public void DoSomething<T>(){
Console.WriteLine("Do Something");
}

thanks,
Henri
 
P

Peter Morris

Answer courtesy of Jon Skeets book (plug)
http://www.amazon.co.uk/C-Depth-Wha...=sr_1_1?ie=UTF8&s=books&qid=1213645816&sr=8-1

I'm only about 100 (ish) pages into it and it's great!


Pete




namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
new Tester().DoSomething(typeof(string));
}


}

public class Tester
{
public void DoSomething(Type type)
{
MethodInfo methodInfo = GetType().GetMethod("GenericMethod");
MethodInfo genericMethodInfo = methodInfo.MakeGenericMethod(new Type[]
{ type });
genericMethodInfo.Invoke(this, null);
}

public void GenericMethod<T>()
{
Console.WriteLine(typeof(T).Name);
}
}

}
 
H

Henri.Chinasque

Thanks alot Pete.

Although it does seem a wee bit complicated... I guess I don't
understand enough about .NET to comprehend why I would need to use
reflection for (what seems to me to be) such a trivial exercise.

-H.C.
 
Q

qglyirnyfgfo

One way is to change the caller method...But I get the feeling that
this is not really something that you want to do:

public void MyMethod<T>(T objType)
{
DoSomething<T>();
}
 
H

Henri.Chinasque

One way is to change the caller method...But I get the feeling that
this is not really something that you want to do:

public void MyMethod<T>(T   objType)
{
    DoSomething<T>();

}


Yep, its a possibility, I know. I'm involved in a bit of a "refactor
for generics" thing, which led to this question. I can pretty much
work around everything, but now I'm just plain curious to see if there
is a trivial (non reflection) way to do what I've described above.

-H.C.
 
P

Peter Morris

Although it does seem a wee bit complicated... I guess I don't
understand enough about .NET to comprehend why I would need to use
reflection for (what seems to me to be) such a trivial exercise.

Imagine this

DoSomething<T>()

is a set of overloads like this

DoSomething(string)
DoSomething(byte)

etc

You'd need code like this

void CallDoSomething(object value)
{
if (value is string)
DoSomething( (string)value);
else if (value is byte)
DoSomething( (byte)value);
}

If you wanted to do it dynamically you'd need to use reflection and Invoke.
If you think about generic methods as overloaded methods which do not exist
until runtime maybe that makes it a bit more clear?



--
Pete
=========================================
I use Enterprise Core Objects (Domain driven design)
http://www.capableobjects.com/
=========================================
 
J

Jeroen Mostert

Although it does seem a wee bit complicated... I guess I don't
understand enough about .NET to comprehend why I would need to use
reflection for (what seems to me to be) such a trivial exercise.
Because you're trying to fit a round peg into a square hole. The Type class
is for passing type information at runtime; generics are for passing type
information at compile time.

Think of generics as a way to insert a ____ blank in a sentence -- you have
to fill it in *before* you have a complete sentence you can read aloud.
Using a Type parameter, on the other hand, is like saying "Take any type,
let's call it T. Given this T, I want to do the following...". Here you
always use complete sentences, but it's a much more roundabout way of doing
things and fundamentally incompatible with the placeholder approach. The
compiler, of course, demands that your sentences always make sense in every
circumstance, even though they might not be true at runtime.

If you really want this you could implement it the other way around: have
the method that takes a Type do the work and have the generic implementation
delegate to it by means of typeof. That's the "right way around", and the
solution there really is trivial. Of course, the method that uses Type may
be anything but.
 
H

Henri.Chinasque

Jeroen & Peter

thanks for the explanations, the generics/compile time and Type/
runtime thing is clear now.

-H.C.
 

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

Top