C
Christophe
class A {}
class B {}
interface MyInterface
{
void method(A a);
void method(B b);
}
class Foo
{
public Foo(MyInterface myInterface)
{
_myInterface = myInterface;
}
private bool Process<T>(T t)
{
if (t == null) return false;
_myInterface.method(t);
// The previous line generates 2 errors
// Error 1: The best overload method match for 'MyInterface.method(A)'
// has some invalid argument
// Error 2: cannot convert from 'T' to 'A'
return true;
}
// I would like to avoid to implement the core of Process
// for each type as follows:
// private bool Process(A a)
// {
// if (a == null) return false;
// _myInterface.method(a);
// return true;
// }
// private bool Process(B b)
// {
// if (b == null) return false;
// _myInterface.method(a);
// return true;
// }
public void Process(object o)
{
if (Process(o as A)) return;
if (Process(o as B)) return;
throw new Exception();
}
private MyInterface _myInterface;
}
I would like to avoid to implement Process for each type. How can I do
this in C# ?
Thanx.
class B {}
interface MyInterface
{
void method(A a);
void method(B b);
}
class Foo
{
public Foo(MyInterface myInterface)
{
_myInterface = myInterface;
}
private bool Process<T>(T t)
{
if (t == null) return false;
_myInterface.method(t);
// The previous line generates 2 errors
// Error 1: The best overload method match for 'MyInterface.method(A)'
// has some invalid argument
// Error 2: cannot convert from 'T' to 'A'
return true;
}
// I would like to avoid to implement the core of Process
// for each type as follows:
// private bool Process(A a)
// {
// if (a == null) return false;
// _myInterface.method(a);
// return true;
// }
// private bool Process(B b)
// {
// if (b == null) return false;
// _myInterface.method(a);
// return true;
// }
public void Process(object o)
{
if (Process(o as A)) return;
if (Process(o as B)) return;
throw new Exception();
}
private MyInterface _myInterface;
}
I would like to avoid to implement Process for each type. How can I do
this in C# ?
Thanx.