问题描述
我有一个VB类,它重载了Not
运算符;在C#应用程序中似乎无法使用.
I have a VB class which overloads the Not
operator; this doesn't seem to be usable from C# applications.
Public Shared Operator Not(item As MyClass) As Boolean
Return False
End Operator
我可以在VB.NET中使用它:
I can use this in VB.NET:
If Not MyClassInstance Then
' Do something
End If
我正在C#应用程序中尝试使用它,但它无法构建.
I am trying to us this in a C# application but it won't build.
if (!MyClassInstance)
{
// do something
}
我得到了错误
谁能告诉我我想念的东西吗?
Can anyone tell me what I am missing?
推荐答案
VB.NET中的Not
运算符是 bitwise 运算符,它产生其操作数的补码.它不具有C#的!
运算符(逻辑运算符)的等效项.您必须在C#中使用等效的按位运算符才能使用VB.NET运算符重载:
The Not
operator in VB.NET is a bitwise operator, it produces the one's complement of its operand. It doesn't have the equivalent of C#'s !
operator, a logical operator. You must use the equivalent bitwise operator in C# to use your VB.NET operator overload:
if(~MyClassInstance)
{
// do something
}
您可以在VB.NET中编写一个函数,该函数将映射到C#逻辑运算符.看起来应该像这样:
You can write a function in VB.NET that will map to the C# logical operator. That needs to look like this:
<System.Runtime.CompilerServices.SpecialName> _
Public Shared Function op_LogicalNot(item As MyClass) As Boolean
Return False
End Function
这篇关于从C#使用重载的VB.NET Not运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!