468,119 Members | 2,086 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Dynamically call a generic?

Ok, lets say I have a generic method:

public T Testing<T>() : where T:class
{
T myT = getStuff() as T;
// Do something with myT
return myT;
}

How do I call it dynamically? The following code does not work, but is
there any way to do it? My understnding of C# generics tells me that I
can... It is not compile-time like C++... I just don't know how.

public object Helper(Type t)
{
return Testing<t>();
}

Help is appreciated :)

Aug 9 '06 #1
2 1575

"Bilz" <Br**********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Ok, lets say I have a generic method:

public T Testing<T>() : where T:class
{
T myT = getStuff() as T;
// Do something with myT
return myT;
}

How do I call it dynamically? The following code does not work, but is
there any way to do it? My understnding of C# generics tells me that I
can... It is not compile-time like C++... I just don't know how.

public object Helper(Type t)
{
return Testing<t>();
}

Help is appreciated :)
Reflection is your friend.

using System;
using System.Collections.Generic;
using System.Reflection;
namespace csTest
{
class Program
{
public static object Helper(Type t)
{
MethodInfo mi = typeof(Program).GetMethod("Testing");
return mi.MakeGenericMethod(t).Invoke(null, null);
}

public static T Testing<T>() where T : class, new()
{
T myT = new T();
// Do something with myT
return myT;
}

class Foo
{
public Foo()
{
}

public override string ToString()
{
return "A Foo";
}
}
static void Main(string[] args)
{
Console.WriteLine(Helper(typeof(Foo)));
Console.ReadKey();
}
}
}
David
Aug 9 '06 #2

David Browne wrote:
"Bilz" <Br**********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Ok, lets say I have a generic method:

public T Testing<T>() : where T:class
{
T myT = getStuff() as T;
// Do something with myT
return myT;
}

How do I call it dynamically? The following code does not work, but is
there any way to do it? My understnding of C# generics tells me that I
can... It is not compile-time like C++... I just don't know how.

public object Helper(Type t)
{
return Testing<t>();
}

Help is appreciated :)

Reflection is your friend.

using System;
using System.Collections.Generic;
using System.Reflection;
namespace csTest
{
class Program
{
public static object Helper(Type t)
{
MethodInfo mi = typeof(Program).GetMethod("Testing");
return mi.MakeGenericMethod(t).Invoke(null, null);
}

public static T Testing<T>() where T : class, new()
{
T myT = new T();
// Do something with myT
return myT;
}

class Foo
{
public Foo()
{
}

public override string ToString()
{
return "A Foo";
}
}
static void Main(string[] args)
{
Console.WriteLine(Helper(typeof(Foo)));
Console.ReadKey();
}
}
}
David
Thanks! I had thought about reflection, but was hoping there might be
a quicker way.

B

Aug 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Alex Lyman | last post: by
1 post views Thread by Me, Myself, and I | last post: by
4 posts views Thread by puzzlecracker | last post: by
reply views Thread by kjvspam | last post: by
3 posts views Thread by kj | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.