demberto
demberto

Reputation: 540

Workaround to template specialization

Abstract Base Class

abstract class Base
{
    public abstract void Method<T>(T args) { }
}

Derived Class several of these exist, each with a replacement for T

class Derived : Base
{
    public override void Method<int>(int args) { }
}

Now, I know this isn't supported, but I need it because somewhere else in my code

class AnotherBaseClass<T, E>
    where E : Base
{
    E e;  // Will be actually one of the derived classes
    T t;  // Simple types like string or int
    
    // I want this to work basically
    public void Func()
    {
        e.Method(t);
    }
}

this is going on.

What is the easiest solution to this? If I am able to do this, it will save me from writing a lot of code.

Upvotes: 0

Views: 75

Answers (2)

Eldar S
Eldar S

Reputation: 591

If you're fine with making Base a generic class, this is how:

abstract class Base<T>
{
     public abstract void Method(T args);
}

class Derived : Base<int>
{
    public override void Method(int args) {}
}

class AnotherBaseClass<T, E>
    where E : Base<T>
{
    E e;
    T t;

    public void Func()
    {
        e.Method(t);
    }
}

If you need Base to not be generic, you can add this:

abstract class Base
{
    public void Method<T>(T args)
    {
        var genericSelf = this as Base<T>;
        genericSelf.Method(args);
    }
}

and make Base<T> inherit Base and ensure your concrete classes always derive Base<T> and never Base directly.

Upvotes: 2

MikeJ
MikeJ

Reputation: 1369

I'm not exactly sure what problem you're sovling but under .Net 5 what you have in your question works as is...

abstract class Base
{
    public abstract void Method<T>(T args);
}

class Derived : Base
{
    public override void Method<T>(T args)
    {
        Console.Write($"{nameof(Derived)}.Method<{typeof(T)}>({args})");
    }
}

class AnotherBaseClass<T, E> where E : Base
{
    readonly E e;
    readonly T t;

    public AnotherBaseClass(T t, E e)
    {
        this.e = e;
        this.t = t;
    }

    public void Func()
    {
        e.Method(t);
    }
}

Run like so:

    static void Main(string[] args)
    {
        Derived d = new Derived();

        var another = new AnotherBaseClass<int,Derived>(5, d);

        another.Func();
    }

Produces: "Derived.Method<System.Int32>(5)"

Upvotes: 0

Related Questions