如何为班级提供自定义演员表支持?

如何为班级提供自定义演员表支持?,第1张

如何为班级提供自定义演员表支持?

您需要使用

implicit
explicit
取决于您是否希望用户强制转换它,或者是否希望它自动发生,从而覆盖转换运算符。通常,一个方向将始终有效,这是您使用的地方
implicit
,而另一个方向有时会失败,这是您使用的地方
explicit

语法如下:

public static implicit operator dbInt64(Byte x){    return new dbInt64(x);}

要么

public static explicit operator Int64(dbInt64 x){    if (!x.defined)        throw new DataValueNullException();    return x.iVal;}

以您的示例为例,从您的自定义类型中说(

MyType
->
byte[]
将始终有效):

public static implicit operator byte[] (MyType x){    byte[] ba = // put pre here to convert x into a byte[]    return ba;}

要么

public static explicit operator MyType(byte[] x){    if (!CanConvert)        throw new DataValueNullException();    // Factory to convert byte[] x into MyType    MyType mt = MyType.Factory(x);    return mt;}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5567354.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存