C#访问基类的隐式运算符

问题描述 投票:1回答:1

我具有以下设置: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# oop implicit-conversion
1个回答
0
投票

据我所知,除非所使用的类型使用contravariance,否则显式转换是不可避免的。您也可以使用the link

© www.soinside.com 2019 - 2024. All rights reserved.