次の方法で共有


IL2046: すべてのインターフェイスの実装とメソッドのオーバーライドには、インターフェイスまたはオーバーライドされた仮想メソッド 'RequiresUnreferencedCodeAttribute' の注釈に一致する注釈が必要です

原因

インターフェイスとその実装、または仮想メソッドとそのオーバーライド間で RequiresUnreferencedCodeAttribute 注釈に不一致があります。

ベース メンバーには属性がありますが、派生メンバーには属性がありません。

public class Base
{
  [RequiresUnreferencedCode("Message")]
  public virtual void TestMethod() {}
}

public class Derived : Base
{
  // IL2046: Base member 'Base.TestMethod' with 'RequiresUnreferencedCodeAttribute' has a derived member 'Derived.TestMethod()' without 'RequiresUnreferencedCodeAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
  public override void TestMethod() {}
}

派生メンバーには属性がありますが、オーバーライドされたメンバーには属性がありません。

public class Base
{
  public virtual void TestMethod() {}
}

public class Derived : Base
{
  // IL2046: Member 'Derived.TestMethod()' with 'RequiresUnreferencedCodeAttribute' overrides base member 'Base.TestMethod()' without 'RequiresUnreferencedCodeAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
  [RequireUnreferencedCode("Message")]
  public override void TestMethod() {}
}

インターフェイス メンバーには属性がありますが、その実装には属性がありません。

interface IRUC
{
  [RequiresUnreferencedCode("Message")]
  void TestMethod();
}

class Implementation : IRUC
{
  // IL2046: Interface member 'IRUC.TestMethod()' with 'RequiresUnreferencedCodeAttribute' has an implementation member 'Implementation.TestMethod()' without 'RequiresUnreferencedCodeAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
  public void TestMethod () { }
}

実装メンバーには属性がありますが、実装するインターフェイスには属性がありません。

interface IRUC
{
  void TestMethod();
}

class Implementation : IRUC
{
  [RequiresUnreferencedCode("Message")]
  // IL2046: Member 'Implementation.TestMethod()' with 'RequiresUnreferencedCodeAttribute' implements interface member 'IRUC.TestMethod()' without 'RequiresUnreferencedCodeAttribute'. For all interfaces and overrides the implementation attribute must match the definition attribute.
  public void TestMethod () { }
}