“Missing compiler required member” error being thrown multiple times with almost no changes to code

后端 未结 12 1245
既然无缘
既然无缘 2020-12-13 22:46

Today after deploying some changes to a C# MVC site that I run, I went back to make some more modifications and came across this error:

Missing compil

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 23:19

    The actual error comes from the fact that your 2.0 assembly that causes the error contains this code:

    namespace System.Runtime.CompilerServices
    {
        public class ExtensionAttribute : Attribute { }
    }
    

    The Code Above allows the .NET 2.0 Assembly to use extension methods (see Using extension methods in .NET 2.0?). While it confuses the compiler if you target .NET 4.0 and reference a 2.0 Assembly (containing above code) as the mscorlib.dll(4.0) contains the same class in the same namespace.

    I fixed this

    • by compiling the original 2.0 assembly again without the attribute targeting 4.0
    • by removing the assembly (obviously)
    • by adding a third Extension Attribute in the target you compile (it seems to overrule the referenced definitions)

提交回复
热议问题