gpt4 book ai didi

C# : Custom implicit cast operator failing

转载 作者:太空狗 更新时间:2023-10-29 17:50:50 26 4
gpt4 key购买 nike

好吧,一段时间以来我一直在努力寻找这方面的任何信息。我构建了一个小类来查看为字符串实现类型安全枚举有多难,因为我想将它们用于数据库字段名称等。我从来都不喜欢枚举只能用于整数这一事实。
然而,即使我已经为这个类实现了一个隐式运算符,每次我尝试使用它时,它都会给我一个无效的转换异常。我不知所措,因为此时我看不出我的代码有任何问题。
这是类(class):

/// <summary>
/// SBool - type-safe-enum for boolean strings
/// </summary>
public sealed class SBool
{

private readonly String name;
private readonly int value;

// these guys were for conversions. They might work better in another case,
// but for this one, they weren't very helpful.
// ((I.e. they didn't work either.))
//private static readonly Dictionary<SBool, String> stringsByBool = new Dictionary<SBool, String>();
//private static readonly Dictionary<String, SBool> boolsByString = new Dictionary<String, SBool>();

public static readonly SBool True = new SBool( 1, "true" );
public static readonly SBool False = new SBool( 0, "false" );

private SBool( int value, String name )
{
this.name = name;
this.value = value;
//stringsByBool[this] = name;
//boolsByString[name] = this;
}

private SBool( SBool sbool )
{
this.name = sbool.name;
this.value = sbool.value;
//stringsByBool[this] = name;
//boolsByString[name] = this;
}

public override String ToString()
{
return name;
}

/// <summary>
/// allows implicit casting of SBools to strings
/// </summary>
/// <param name="sbool">the SBool to cast into a string</param>
/// <returns>the string equivalent of the SBool (its value)</returns>
public static implicit operator String( SBool sbool )
{
if ( sbool == SBool.True )
return SBool.True.name;
else
return SBool.False.name;
}

/// <summary>
/// implicitly cast a string into a SBool.
/// </summary>
/// <param name="str">the string to attempt to cast as a SBool</param>
/// <returns>the SBool equivalent of the string,
/// SBool.False if not either "true" or "false".</returns>
public static explicit operator SBool( String str )
{
if ( !String.IsNullOrEmpty(str) && str.ToLower() == "true" )
return SBool.True;
else
return SBool.False;
}

public static bool operator ==( SBool left, SBool right )
{
return left.value == right.value;
}

public static bool operator !=( SBool left, SBool right )
{
return left.value != right.value;
}
}


这是在检查 session 变量时失败:
if( ( (string)Session["variable"] ) == SBool.False ) 带有 InvalidCastException,
坦率地说,我不知道为什么。

提前致谢;任何可以解释为什么这不起作用的人的 cookie(并非在所有地区都提供 cookie)。我将修复其他问题,但如果有任何不清楚的地方,请告诉我。有关类型安全枚举的更多信息,请参阅此处的 one of the SO posts我基于这个类。

[MetaEDIT] 忽略这一点。我大错特错了。 [/编辑]

最佳答案

用户定义的隐式和显式运算符完全是一种编译时机制,而不是运行时机制。一旦代码被编译,运行时就不知道任何用户定义的转换运算符。

当编译器进行类型检查时,发现预期的是 Foo 但实际值​​是 Bar,它将首先检查内置语言的隐式转换运算符以查看是否存在适当的转换。如果没有,它会检查 FooBar 的定义以查找隐式转换运算符,如果找到,它将添加对相关静态方法的调用执行转换。一旦进入运行时,将仅应用内置语言隐式运算符。

在这种情况下,您不是从 SBool 转换为 string,而是从 object 转换为 string(就编译器而言)并且没有转换运算符来处理它。

您需要首先将 Session 变量的结果转换为 SBool(它实际上是什么),然后再转换为其他类型,以便能够利用用户定义的转换运算符。所以:

if( ( (SBool)Session["variable"] ) == SBool.False )

会工作得很好。

关于C# : Custom implicit cast operator failing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15767308/

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