Partager via


IL2046 : L’ensemble des implémentations d’interface et des remplacements de méthode doivent avoir des annotations correspondant à l’interface ou à la méthode virtuelle substituée « RequiresUnreferencedCodeAttribute »

Cause

Il existe une incompatibilité dans les annotations RequiresUnreferencedCodeAttribute entre une interface et son implémentation ou une méthode virtuelle et son remplacement.

Exemple

Un membre de base a l’attribut, mais le membre dérivé n’a pas l’attribut.

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() {}
}

Un membre dérivé a l’attribut, mais le membre de base substitué n’a pas l’attribut.

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() {}
}

Un membre d’interface a l’attribut, mais son implémentation n’a pas l’attribut.

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 () { }
}

Un membre d’implémentation a l’attribut, mais l’interface qu’il implémente n’a pas l’attribut.

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 () { }
}