I have an Interface called "IOperation"
interface IOperation<T,U>
{
T FirstOperand { get; set; }
U SecondOperand { get; set; }
int Result { get; }
}
FirstOperand and SecondOperand can either be an int or another IOperation. What is the best way to implement such behaviour? Example of an inherited class:
class Subtraction<T, U> : IOperation<T, U>
{
public T FirstOperand { get; set; }
public U SecondOperand { get; set; }
public int Result
{
get
{
var first = 0;
var second = 0;
if(FirstOperand is Double)
{
first = (Double) FirstOperand;
}
if(FirstOperand is IOperation)
{
first = (IOperation) FirstOperand;
}
// TODO: Same thing with SecondOperand
return FirstOperand - SecondOperand;
}
}
}
Well, compilation error, apparently a direct cast doesn't work for generics. This problem could be fixed by casting into object first, but I'm pretty sure that would be a terrible approach and not good practice at all. Do you have any suggestions how I can solve this problem in a good way?