简体   繁体   English

为什么C#编译器生成错误,即使使用属性“SpecialName”

[英]Why C# compiler generate error, even if using Attribute “SpecialName”

i write code: 我写代码:

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;
        }
    }
}

compile time error: 编译时错误:

Error 1 Operator '+' cannot be applied to operands of type 'ConsoleApplication21.MyClass1' and 'ConsoleApplication21.MyClass1' 错误1运算符“+”无法应用于“ConsoleApplication21.MyClass1”和“ConsoleApplication21.MyClass1”类型的操作数

So, c# compiler did not like line "int j = new MyClass1() + new MyClass1();" 所以,c#编译器不喜欢行“int j = new MyClass1()+ new MyClass1();” When i open ILDASM, i got same code of operator overloadings: 当我打开ILDASM时,我得到了相同的运算符过载代码:

Method #1 (06000003) 
-------------------------------------------------------
    MethodName: op_Subtraction (06000003)
    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 : (08000002) Name : i flags: [none] (00000000)
        (2) ParamToken : (08000003) Name : j flags: [none] (00000000)

Method #2 (06000004) 
-------------------------------------------------------
    MethodName: op_Addition (06000004)
    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 : (08000004) Name : i flags: [none] (00000000)
        (2) ParamToken : (08000005) Name : j flags: [none] (00000000)

So, why C# compiler generates an error? 那么,为什么C#编译器会产生错误?

Really, strange behavior: if i reference the MyClass1 as DLL, it works fine! 真的,奇怪的行为:如果我将MyClass1作为DLL引用,它工作正常!

在此输入图像描述 Thanks! 谢谢!

Really, strange behavior: if i reference the MyClass1 as DLL, it works fine! 真的,奇怪的行为:如果我将MyClass1作为DLL引用,它工作正常!

That explains a lot. 这解释了很多。 The CLR compiles the code into an assembly. CLR将代码编译为程序集。 Before it does that, it evaluates the code you have without taking the special name signature in consideration. 在此之前,它会在不考虑特殊名称签名的情况下评估您拥有的代码。 That code gives an compilation error since at that time , there is no matching overload yet. 该代码给出了编译错误,因为那时还没有匹配的重载。 It still has to be embedded and compiled. 它仍然必须嵌入和编译。 (It is a chicken or the egg problem) (这是鸡还是蛋的问题)

The compiled assembly can be used from another project, since there the assembly has compiled entirely. 编译的程序集可以从另一个项目中使用,因为程序集已完全编译。

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

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