1

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.

harlekintiger
  • 254
  • 6
  • 17

1 Answers1

1

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.

snr
  • 16,197
  • 2
  • 61
  • 88
  • How can I find out if it is, or how can I set it to be covariant or contravariant? – harlekintiger Apr 11 '20 at 22:33
  • 1
    @harlekintiger seemingly, you cannot apply it to your case since they are not generic. So, the fact that your hands have tied coerces you to use the explicit casting. – snr Apr 11 '20 at 22:41