473,507 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1744

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

Similar topics

5
2453
by: Alex Lyman | last post by:
Does anyone have any code handy (or know what a good direction for me to head in), to call functions, if you have an address of the function, its declspec (for my app, it's limited to _stdcall and...
1
1726
by: Me, Myself, and I | last post by:
First off, i apologize if my terminology is off... I am currently in a project that is basically a front-end to a database. In coding this, I am taking into account that it has the *potential*...
1
1835
by: Andy | last post by:
Can anyone tell me if it is possible to dynamically create generic collections? simply put I want to be able to something like this : public object test(SomeBaseClass param1) { if...
4
10433
by: puzzlecracker | last post by:
I have seen these terms used in Gang of 4 but could never thoroughly understand what it meant I perceive that C++ is both, but apparently it is only a latter. Can someone explain it? thx
0
944
by: kjvspam | last post by:
I have a bunch of reports that are defined as metadata in a database. Each report has an ID and a collection of filters (e.g. filter the report where X is between 5 and 8). I need to expose the...
9
6971
by: Allen | last post by:
The arguments of function is variable. Given function address, argument type and data, how to dynamically call the function? The following is pseudo code. int count = 0; int offset = 0; char...
3
1466
by: kj | last post by:
I've tried a bazillion ways to code dynamically generated methods, to no avail. The following snippet is a very simplified (and artificial) demo of the problem I'm running into, featuring my...
20
4258
by: Nickolai Leschov | last post by:
Hello all, I am programming an embedded controller that has a 'C' library for using its system functions (I/O, timers, all the specific devices). The supplied library has .LIB and .H files. ...
4
7928
by: alan | last post by:
AFAIK a template parameter must be completely determinable by the compiler at compile time, is this correct? Currently my problem is, I have a class which contains a std::vector. The size of...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.