I cannot figure out how to create a method that overrides a base
method. I'm using the following code:
void CreateMethodOverride(TypeDefinition targetType,
TypeDefinition baseClass, string methodName, MethodInfo
methodInfo)
{
// locate the matching base class method, which may
// reside in a different module
MethodDefinition baseMethod = baseClass
.Methods.First(method =>
method.Name.Equals(methodName));
MethodDefinition newMethod = targetType.Copy(methodInfo);
newMethod.Name = baseMethod.Name;
newMethod.Attributes = baseMethod.Attributes &
~Mono.Cecil.MethodAttributes.Virtual;
newMethod.ImplAttributes = baseMethod.ImplAttributes;
newMethod.SemanticsAttributes =
baseMethod.SemanticsAttributes;
newMethod.Overrides.Add(targetType.Module.Import(baseMethod));
targetType.Methods.Add(newMethod);
}
Note that I have an extension method that does a 'deep' copy from the
MethodInfo by creating a new MethodDefinition and then importing all
referenced types into the module.
I thought that by adding the base method to the newMethod.Overrides,
that this would give the correct result, but when I examine the
resulting assembly, the new method has the same signature as the base
method, but without the 'virtual'.
What is the proper way to ensure that the new method overrides the
base?
--
--
mono-cecil