I love generics in C#, but sometimes working with them can get a bit complicated. The problem below I run into every now and then. Is there any way of making this scenario simpler? I can't see how, but I'm hoping someone can :)
Given three base classes:
public abstract class Inner
{
}
public abstract class Outer<T>
where T : Inner
{
}
public abstract class Handler<TOuter, TInner>
where TOuter : Outer<TInner>
where TInner : Inner
{
public abstract void SetValue(TInner value);
}
And some simple implementations:
public class In : Inner
{
}
public class Out : Outer<In>
{
}
public class HandleOut : Handler<Out, In>
{
public override void SetValue(In value) { }
}
Now my question is: For HandleOut, the type of TInner is given by the type "Out", so is there any way to simplify the definition of HandleOut to something like public class HandleOut : Handler<Out> and still be able to use the inner type as a parameter to SetValue?
This is a very simple example, but I sometimes get a long list of generic types in definitions, when usually all of them can be logically deduced from the first type. Are there any tricks I'm missing?