简体   繁体   English

在 C# 中重载抽象泛型方法

[英]Overloading abstract generic methods in C#

I'm trying to implement a generic abstract method with a type constraint, then Implement it multiple times using different specified types.我正在尝试使用类型约束实现一个通用抽象方法,然后使用不同的指定类型多次实现它。

public abstract class Ability
{
   public abstract void BindToStation<T>(T station) where T : Station;
}

public class DashAbility : Ability
{
    public override void BindToStation<NavStation>(NavStation station){ }
    public override void BindToStation<CannonStation>(CannonStation station){ }
}

But I get an error which says the method has already been defined with the same paramater types.但是我收到一个错误,说该方法已经用相同的参数类型定义了。

I'm guessing that the compiler treats any generic paramater as the same in terms of the method signature, so these two methods look the same to it.我猜测编译器在方法签名方面将任何通用参数视为相同,因此这两种方法看起来相同。

Still though, I'm wondering if theres a way to have generic method overloading using specific types.. ?尽管如此,我想知道是否有办法使用特定类型重载泛型方法..?

You can't do exactly what you want, but you can try an approach like this:您不能完全按照自己的意愿行事,但可以尝试以下方法:

interface IBindableTo<T> where T : Station
{
    void BindToStation(T station);
}

abstract class Ability
{
    public abstract void BindToStation<T>(T station) where T : Station;
}

class DashAbility : Ability, IBindableTo<NavStation>, IBindableTo<CannonStation>
{
    public override void BindToStation<T>(T station)
    {
        if (this is IBindableTo<T> binnder)
        {
            binnder.BindToStation(station);
            return;
        }

        throw new NotSupportedException();
    }

    void IBindableTo<NavStation>.BindToStation(NavStation station)
    {
        ...
    }

    void IBindableTo<CannonStation>.BindToStation(CannonStation station)
    {
        ...
    }
}

Hope this helps.希望这可以帮助。

C# doesn't support specialization in that way, and neither does C++ easily when you want to specialize on runtime type. C# 不支持这种方式的专业化,当您想要专业化运行时类型时,C++ 也不支持这种方式。

But you can use polymorphism, so you can use double-dispatch:但是你可以使用多态,所以你可以使用双调度:

public abstract class Station {
    internal abstract void DashBindToStation();
}

public class NavStation : Station {
    internal override void DashBindToStation() {
        throw new NotImplementedException();
    }
}

public class CannonStation : Station {
    internal override void DashBindToStation() {
        throw new NotImplementedException();
    }
}

public abstract class Ability {
    public abstract void BindToStation(Station station);
}

public class DashAbility : Ability {
    public override void BindToStation(Station station) {
        station.DashBindToStation();
    }
}

Another possibility with C# is to use runtime dispatching using dynamic : C# 的另一种可能性是使用dynamic使用运行时调度:

public abstract class Station {
}

public class NavStation : Station {
}

public class CannonStation : Station {
}

public abstract class Ability {
    public abstract void BindToStation(Station station);
}

public class DashAbility : Ability {
    public void BindToStation(NavStation station) {
    }
    public void BindToStation(CannonStation station) {
    }

    public override void BindToStation(Station station) {
        BindToStation((dynamic)station);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM