简体   繁体   English

调用方法从抽象类重载

[英]Call Method overloaded from abstract class

I need to call a Method of a an abstract class implementation. 我需要调用一个抽象类实现的方法。 But the compiler always defaults to use the overloaded method from the concrete class, since the signature just requires an inherited type. 但是编译器总是默认使用具体类中的重载方法,因为签名只需要一个继承类型。

Just have a look at the code: 看看代码吧:

-- This is obviously a vastly simplified version of the actual code that I'm using ;) - 这显然是我正在使用的实际代码的大大简化版本;)

public class ModelBase { }
public class ModelA : ModelBase { }
public interface IInterface<in TModel>
{
    void Do(TModel model);
}
public abstract class AbstractClass<TModel> : IInterface<TModel>
{
    public abstract void Do(TModel model);
}
public class ConcreteClass : AbstractClass<ModelA>, IInterface<ModelBase>
{
    public override void Do(ModelA model)
    {
        // I'd like to invoke this method
    }

    public void Do(ModelBase model)
    {
        // how do I invoke the method above??
        Do((ModelA)model);
    }
}

^^ This results in a recursion ^^这会导致递归

What I've tried is: 我试过的是:

((IClass<ModelA>)this).Do((ModelA)model);

^^ doesn't change anything ^^不会改变任何东西

base.Do((ModelA)model);

^^ throws an error "Cannot call an abstract base member: 'AbstractClass.Do(ModelA)'" ^^抛出错误“无法调用抽象基础成员:'AbstractClass.Do(ModelA)'”

How do I call the "public override void Do(ModelA model)" method? 如何调用“public override void Do(ModelA model)”方法?


btw. 顺便说一句。 If I change the class to this 如果我将课程更改为此

public class ConcreteClass : IInterface<ModelA>, IInterface<ModelBase>

it works. 有用。

Cast this to AbstractClass<ModelA> before call: thisAbstractClass<ModelA>呼叫之前:

public void Do(ModelBase model)
{
    ((AbstractClass<ModelA>)this).Do((ModelA)model);
}

Use named parameters: 使用命名参数:

public class ConcreteClass : AbstractClass<ModelA>, IInterface<ModelBase>
{
    public override void Do(ModelA modelA) // change 'model' into 'modelA'
    {
        // I'd like to invoke this method
    }

    public void Do(ModelBase model)
    {
        // how do I invoke the method above??
        Do(modelA : (ModelA)model);
    }
}

Now, the compiler knows that you want to call the Do(ModelA) method and not the Do(ModelBase) method. 现在,编译器知道您要调用Do(ModelA)方法而不是Do(ModelBase)方法。

Try to implement the interface explicitly. 尝试显式实现接口。 From now on you can call the method IInterface<ModelBase>.Do(ModelBase model) only from a reference of type IInterface<ModelBase> 从现在开始,您只能从类型IInterface<ModelBase>的引用调用方法IInterface<ModelBase>.Do(ModelBase model)

public class ModelBase { }
public class ModelA : ModelBase { }
public interface IInterface<in TModel>
{
    void Do(TModel model);
}
public abstract class AbstractClass<TModel> : IInterface<TModel>
{
    public abstract void Do(TModel model);
}
public class ConcreteClass : AbstractClass<ModelA>, IInterface<ModelBase>
{
    public override void Do(ModelA model)
    {
        Console.Write("Do - Override AbstractClass<ModelA> ");
    }

    void IInterface<ModelBase>.Do(ModelBase model)
    {
        Console.Write("Do - IInterface<ModelBase> ");
        Do(model as ModelA);
    }
}

void Main()
{
    var modelA = new ModelA();

    (new ConcreteClass() as IInterface<ModelBase>).Do(modelA); //output -> Do - IInterface<ModelBase> Do - Override AbstractClass<ModelA> 
    Console.WriteLine();
    new ConcreteClass().Do(modelA); //output -> Do - Override AbstractClass<ModelA> 
}

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

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