重载vb.net和放大器;运营商在C#类 [英] Overloading the vb.net & operator in a c# class
问题描述
我在这里有一个令人难以置信的独特问题。我们的业务应用程序一直使用C#和vb.net建成。我们一直在努力接近标准和修剪脂肪一些我们的核心,已复制,对象。我们正在非常接近,但试图巩固重复的对象转换为C#,当我们的vb.net code现在开始引发错误操作员'和;没有定义类型CSType'和'串',当我尝试用符号做vb.net字符串连接(安培)。有趣的是,如果我使用'和;在C#与CSType(后适当超载)我得到的字符串连接我的期望。
I have an incredibly unique problem here. Our business application has been built using c# and 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.
下面是对CSType我的基本重载:
Here are my basic overloads on 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();
}
当我运行'和;运营商在C#与CSType和一个字符串,我得到预期的结果,当我试图执行一个在vb.net的code不能编译,给我的错误是:
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'和'串'。
"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.
反正我是有点想放在类C ++的,我知道我能得到什么,我需要出来的,而不是2种语言够多了。
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#中是按位与运算。所以,当你重载它像
The &
operator in C# is the bitwise AND operator. 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();
}
您可以使用它在VB.Net与和
操作符:
you can use it in VB.Net with the And
operator:
Dim a = New CSType("Foo")
Dim b = "Bar"
Dim c = a And b
然而,超负荷VB.Net的&安培; VB.Net(例如C#)之外
运营商,你必须创建一个名为方法 op_Concatenate
和使用<一个href="http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k%28System.Runtime.CompilerServices.SpecialNameAttribute%29;k%28TargetFrameworkMoniker-.NETFramework,Version=v4.5%29;k%28DevLang-csharp%29&rd=true"相对=nofollow> SpecialName
属性:
However, to overload VB.Net's &
operator outside of VB.Net (e.g. C#), you have to create a method named op_Concatenate
and use the SpecialName
attribute:
[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();
}
那么下面code将工作:
Then the following code will work:
Dim a = New CSType("Foo")
Dim b = "Bar"
Dim c = a & b
这篇关于重载vb.net和放大器;运营商在C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!