gpt4 book ai didi

c# - 为什么 C# 不允许使用组合的类型对接口(interface)进行隐式转换?

转载 作者:太空狗 更新时间:2023-10-29 21:24:55 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why can't I use interface with explicit operator?

当我这样做时:

public struct Effect
{
public IEffect IEffect { get; private set; }

public Effect ( IEffect effect )
{
this.IEffect = effect;
}

public static implicit operator IEffect ( Effect effect )
{
return effect.IEffect;
}

public static explicit operator Effect ( IEffect effect )
{
return new Effect ( effect );
}
}

我得到这样的编译器错误:

'ImageEditor.Effect.implicit operator ImageEditor.IEffect(ImageEditor.Effect)': user-defined conversions to or from an interface are not allowed.

为什么不允许?这不是一个好的做法吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com