Reputation: 6016
Is it possible in C# to pass a generic to an overloaded method, and get it to resolve to a non-generic version of the method? For example:
class Program {
static void Main(string[] args) {
A a = new A();
Process(a);
Console.ReadLine();
}
static void Process<T>(T item) {
Writer(item);
}
// Writer methods...
static void Writer<T>(T item) {
Console.WriteLine("Type: " + item.GetType());
Console.WriteLine("You lose");
}
static void Writer(A item) {
item.Write();
}
}
class A {
public void Write() {
Console.WriteLine("You found me!");
}
}
This code outputs:
Type: Foo.A
You lose
And I'd like to see:
You found me!
Is there any way of doing this, or something similar? I'm using C# 4.
Upvotes: 2
Views: 544
Reputation: 144126
You can use dynamic
to defer overload resolution until runtime:
static void Process<T>(T item)
{
dynamic d = item;
Writer(d);
}
Upvotes: 5