简体   繁体   中英

C# Access to implicit operators of base class

I have the following setup: Entity is deriving from MonoBehaviour . MonoBehaviour implements an implizit conversion to bool. Now if I implement an implicit conversion to bool in Entity, it overrides the one from MonoBehaviour. If I now want to access both the old and the new conversion, I have to do cast back to the base class

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

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

Now my question, is there a different method without having to cast to the base class? I tried to use the base keyword, but couldn't get it to work.

As far as I know, unless the type being used utilizes contravariance , the explicit casting is inevitable. You can make use of the link too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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