Suppose you have an interface like this:
public interface IDoSomething<out T>
{
T DoSomething(object value);
}
To be able to call DoSomething
on that interface without knowing the type of T
, you have to create this:
public interface IDoSomething
{
object DoSomething(object value);
}
Modify your original interface to inherit from the new one:
public interface IDoSomething<out T> : IDoSomething
{
new T DoSomething(object value);
}
And then do this:
((IDoSomething)obj).DoSomething();
'''But''' then you have to implement the DoSomething
method on your implementation twice; once explicitly and once implicitly.
One solution that partly works is to do this:
((IDoSomething<object>)obj).DoSomething();
But this only works if the generic type is a reference type; value types like DateTime
and int
won't work, even though they too inherit from object
. Why is this?!
Is there a solution to this conundrum yet (without employing abstract classes!)?