简体   繁体   English

在ac#类中重载vb.net和运算符

[英]Overloading the vb.net & operator in a c# class

I have an incredibly unique problem here. 我这里有一个非常独特的问题。 Our business application has been built using c# and vb.net. 我们的业务应用程序是使用c#和vb.net构建的。 We have been trying to approach a standard and trim the fat for some of our core, already duplicated, objects. 我们一直试图接近一个标准并削减我们的一些核心,已经重复的对象的脂肪。 We are getting really close, but when trying to consolidate a duplicate object into c# our vb.net code now starts throwing the error "Operator '&' is not defined for type 'CSType' and 'String', when I try to do vb.net string concatenation using the ampersand(&). The funny thing is that if I use the '&' in c# with CSType (after properly overloading it) i get the string concatenation I expect. 我们变得非常接近,但是当我试图将一个重复的对象合并到c#中时,我们的vb.net代码现在开始抛出错误“运算符'&'未定义为类型'CSType'和'String',当我尝试执行vb时使用和号(&)的.net字符串连接。有趣的是,如果我在c#中使用带有CSType的'&'(在正确重载之后),我得到了我期望的字符串连接。

Here are my basic overloads on CSType: 以下是我对CSType的基本重载:

public static string operator &(CSType c1, string s2)
{
    return c1.ToString() + s2;
}
public static string operator &(string s1, CSType c2) 
{
    return s1 + c2.ToString();
}

When I run the '&' operator in c# with a CSType and a string, I get the expected results, when I attempt to execute that in vb.net the code will not compile giving me an error that: 当我使用CSType和字符串在c#中运行'&'运算符时,我得到了预期的结果,当我尝试在vb.net中执行时,代码将无法编译,从而给出了一个错误:

"Operator '&' is not defined for types 'CSType' and 'String'" “运算符'&'未定义类型'CSType'和'字符串'”

CSType also implicitly converts to most data types, so I was thinking that there may have been some issue with the '&' assuming that it was a bitwise operator, but I would guess that would fails by giving me messed up execution, not a compile error. CSType也隐式转换为大多数数据类型,所以我认为'&'可能存在一些问题,假设它是一个按位运算符,但我猜想通过给我乱搞执行而不是编译会失败错误。

Anyway, I'm of half a mind to place this class in c++ where I know I can get what I need out of it, but isn't 2 languages enough already. 无论如何,我有点想把这个课放在c ++中,我知道我可以从中得到我需要的东西,但已经不够用2种语言了。

The & operator in C# is the bitwise AND operator. C#中的&运算符是按位AND运算符。 So when you overload it like 所以当你重载它时

public static string operator &(CSType c1, string s2)
{
    return c1.ToString() + s2;
}
public static string operator &(string s1, CSType c2) 
{
    return s1 + c2.ToString();
}

you can use it in VB.Net with the And operator: 你可以使用And运算符在VB.Net中使用它:

Dim a = New CSType("Foo")
Dim b = "Bar"
Dim c = a And b

However, to overload VB.Net's & operator outside of VB.Net (eg C#), you have to create a method named op_Concatenate and use the SpecialName attribute: 但是,要在VB.Net之外重载VB.Net的&运算符(例如C#),您必须创建一个名为op_Concatenate的方法并使用SpecialName属性:

[SpecialName]
public static string op_Concatenate(CSType c1, string s2)
{
    return c1.ToString() + s2;
}

[SpecialName]
public static string op_Concatenate(string s1, CSType c2)
{
    return s1 + c2.ToString();
}

Then the following code will work: 然后以下代码将起作用:

Dim a = New CSType("Foo")
Dim b = "Bar"
Dim c = a & b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM