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

前端之家收集整理的这篇文章主要介绍了为什么C#编译器会生成错误,即使使用属性“SpecialName”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写代码
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 : (

Error 1 Operator '+' cannot be applied to operands of type
'ConsoleApplication21.MyClass1' and 'ConsoleApplication21.MyClass1'

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)

解决方法

Really,strange behavior: if i reference the MyClass1 as DLL,it works fine!

这解释了很多. CLR将代码编译为程序集.在此之前,它会在不考虑特殊名称签名的情况下评估您拥有的代码.该代码给出了编译错误,因为那时还没有匹配的重载.它仍然必须嵌入和编译. (这是chicken or the egg问题)

编译的程序集可以从另一个项目中使用,因为程序集已完全编译.

原文链接:https://www.f2er.com/csharp/99520.html

猜你在找的C#相关文章