I have a generic class Foo<T> where T: SomeBaseType.
And in the specific case where T is SpecificDerivedType, I want my class to have an additional method.
Something like:
class Foo<T> where T: SomeBaseType
{
    //.... lots of regular methods taking T, outputting T, etc.
    //in pseudo code
    void SpecialMethod() visible if T: SpecificDerivedType
    {
        //...code...
    }
}
How can I achieve this?
Make an extension method for Foo<T>:
public static void SpecialMethod<T>(this Foo<T> foo)
    where T : SpecificDerivedType
{
}
                        How can I achieve this?
You can't. Create a specialized Foo.
class SpecialFoo<T> : Foo<T> where T: SpecificDerivedType
{
    void SpecialMethod()
    {
        //...code...
    }
}
Other suggest an extension method, which is obviously not the same as what you ask. It might be a workaround though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With