[英]C# Access to implicit operators of base class
I have the following setup: Entity
is deriving from MonoBehaviour
.我有以下设置:
Entity
来自MonoBehaviour
。 MonoBehaviour implements an implizit conversion to bool. MonoBehaviour 实现了到 bool 的隐式转换。 Now if I implement an implicit conversion to bool in Entity, it overrides the one from MonoBehaviour.
现在,如果我在 Entity 中实现到 bool 的隐式转换,它会覆盖 MonoBehaviour 中的转换。 If I now want to access both the old and the new conversion, I have to do cast back to the base class
如果我现在想同时访问旧转换和新转换,我必须转换回基础 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?现在我的问题是,有没有一种不同的方法而不必转换到基础 class? I tried to use the
base
keyword, but couldn't get it to work.我尝试使用
base
关键字,但无法正常工作。
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.
您也可以使用该链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.