首页 > 解决方案 > C#访问基类的隐式运算符

问题描述

我有以下设置:Entity来自MonoBehaviour. MonoBehaviour 实现了到 bool 的隐式转换。现在,如果我在 Entity 中实现到 bool 的隐式转换,它会覆盖 MonoBehaviour 中的转换。如果我现在想同时访问旧转换和新转换,我必须强制转换回基类

public class Entity : MonoBehaviour 
{ 
    private float CurrentHealthPoints { get; set; }

    public static implicit operator bool(Entity entity) 
        => (MonoBehaviour)entity && entity.CurrentHealthPoints > 0;
}

现在我的问题是,有没有一种不同的方法而不必强制转换为基类?我尝试使用base关键字,但无法使用。

标签: c#oopimplicit-conversion

解决方案


据我所知,除非使用的类型使用逆变,否则显式转换是不可避免的。您也可以使用该链接


推荐阅读