What I would like to do is to have an interface like IResult result = new SomeResult(), and then depending on some if's, access some specific fields after casting.
if (a == b)
{
result = (SomeOtherResult) result;
result.fieldFromSomeOtherResult = 42;
}
Obviously now I can't do that, because result's interface doesn't have this field, neither does SomeResult class, only SomeOtherResult class. How should I solve this?
IResult,SomeResultandSomeOtherResultrelated to each other?SomeResultandSomeOtherResultimplementsIResult.IResult, what field, what are you trying, etc.) will help to get specific to your case answers. There could be an existing pattern already. Maybe use generics to passSomeOtherResulttype or perhaps it shouldn't beIResult, but a more suitable interface (implemented bySomeOtherResult).