Search code examples
c#monofodycompile-time-weaving

Cast error when trying to implement Fody add-in


I'm trying to learn how to write a Fody module, and was following the Pluralsight Fody course, which includes a walkthrough.

The sample add-in was to add an interface to any classes whose names ended in "DTO". He used the basic add-in from the Fody source, and changed the Execute() method as follows...

public void Execute() {
  typeSystem = ModuleDefinition.TypeSystem;
  TypeDefinition ifDef = new TypeDefinition("MarkerInterfaces", "DtoMarkerInterface", TypeAttributes.Interface | TypeAttributes.Public);
  ModuleDefinition.Types.Add(ifDef);
  IEnumerable<TypeDefinition> dtos = ModuleDefinition.GetTypes().Where(t => t.Name.EndsWith("DTO"));
  foreach (TypeDefinition dto in dtos) {
    dto.Interfaces.Add(ifDef);
  }
}

However, when I do this, I get a compiler error on the last code line...

Argument 1: cannot convert from 'Mono.Cecil.TypeDefinition' to 'Mono.Cecil.InterfaceImplementation'

Anyone any idea what the problem is? I'm not experienced enough in this stuff to know.


Solution

  • try

    dto.Interfaces.Add(new InterfaceImplementation(ifDef));