using System.Runtime.CompilerServices; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { int i = new MyClass1() - new MyClass1(); int j = new MyClass1() + new MyClass1(); } } public class MyClass1 { public static int operator -(MyClass1 i,MyClass1 j) { return 5; } [SpecialName] public static int op_Addition(MyClass1 i,MyClass1 j) { return 5; } } }
编译时错误:
Error 1 Operator ‘+’ cannot be applied to operands of type
‘ConsoleApplication21.MyClass1’ and ‘ConsoleApplication21.MyClass1’
所以,c#编译器不喜欢行“int j = new MyClass1()new MyClass1();”
当我打开ILDASM时,我得到了相同的运算符过载代码:
那么,为什么C#编译器会产生错误?
真的,奇怪的行为:如果我将MyClass1作为DLL引用,它工作正常!
Method #1 (using System.Runtime.CompilerServices; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { int i = new MyClass1() - new MyClass1(); int j = new MyClass1() + new MyClass1(); } } public class MyClass1 { public static int operator -(MyClass1 i,MyClass1 j) { return 5; } } }
003)
-------------------------------------------------------
MethodName: op_Subtraction (using System.Runtime.CompilerServices; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { int i = new MyClass1() - new MyClass1(); int j = new MyClass1() + new MyClass1(); } } public class MyClass1 { public static int operator -(MyClass1 i,MyClass1 j) { return 5; } } }
003)
Flags : [Public] [Static] [HideBySig] [ReuseSlot] [SpecialName] (00000896)
RVA : 0x00002078
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
ReturnType: I4
2 Arguments
Argument #1: Class ConsoleApplication21.MyClass1
Argument #2: Class ConsoleApplication21.MyClass1
2 Parameters
(1) ParamToken : (Error 1 Operator '+' cannot be applied to operands of type
'ConsoleApplication21.MyClass1' and 'ConsoleApplication21.MyClass1'002) Name : i flags: [none] (00000000)
(2) ParamToken : (Error 1 Operator '+' cannot be applied to operands of type
'ConsoleApplication21.MyClass1' and 'ConsoleApplication21.MyClass1'003) Name : j flags: [none] (00000000)
Method #2 (
using System.Runtime.CompilerServices; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { int i = new MyClass1() - new MyClass1(); int j = new MyClass1() + new MyClass1(); } } public class MyClass1 { public static int operator -(MyClass1 i,MyClass1 j) { return 5; } } }
004)
-------------------------------------------------------
MethodName: op_Addition (using System.Runtime.CompilerServices; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { int i = new MyClass1() - new MyClass1(); int j = new MyClass1() + new MyClass1(); } } public class MyClass1 { public static int operator -(MyClass1 i,MyClass1 j) { return 5; } } }
004)
Flags : [Public] [Static] [HideBySig] [ReuseSlot] [SpecialName] (00000896)
RVA : 0x0000208c
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
ReturnType: I4
2 Arguments
Argument #1: Class ConsoleApplication21.MyClass1
Argument #2: Class ConsoleApplication21.MyClass1
2 Parameters
(1) ParamToken : (Error 1 Operator '+' cannot be applied to operands of type
'ConsoleApplication21.MyClass1' and 'ConsoleApplication21.MyClass1'004) Name : i flags: [none] (00000000)
(2) ParamToken : (using System.Runtime.CompilerServices; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { int i = new MyClass1() - new MyClass1(); int j = new MyClass1() + new MyClass1(); } } public class MyClass1 { public static int operator -(MyClass1 i,MyClass1 j) { return 5; } } }using System.Runtime.CompilerServices; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { int i = new MyClass1() - new MyClass1(); int j = new MyClass1() + new MyClass1(); } } public class MyClass1 { public static int operator -(MyClass1 i,MyClass1 j) { return 5; } } }005) Name : j flags: [none] (00000000)Error 1 Operator '+' cannot be applied to operands of type
'ConsoleApplication21.MyClass1' and 'ConsoleApplication21.MyClass1'