I am trying to add an explicit override of a method of a generic type.

Something like this:


     interface TestInterface<TA> {
         TA Value();
     }
 
     class TestClass<TB> : TestInterface<TB> {
         TB TestInterface<TB>.Value() {
             return default(TB);
         }
     }
 

>From what I can tell based on previous messages on this list, to make the 
override directive work properly with generics I need to set the 
DeclaringType property of the MethodReference. However, that object is the 
original MethodDefinition so updating DeclaringType doesn't work. Is there 
a way to create a distinct MethodReference that I can use here or is there 
something else that needs to be done. This is the closest I have gotten.


 using System;
 using Mono.Cecil;
 using Mono.Cecil.Cil;
 
 namespace CecilTest {
     class MainClass {
         public static void Main(string[] args) {
             var assemblyDef = AssemblyDefinition.CreateAssembly(
                 new AssemblyNameDefinition("CecilTestOutput", new Version(1
, 0)),
                 "CecilTestOutput",
                 ModuleKind.Dll
             );
             var modDef = assemblyDef.MainModule;
 
             var iface = new TypeDefinition("", "TestInterface", 
TypeAttributes.Abstract | TypeAttributes.Public | TypeAttributes.Interface);
 
             var ifaceT = new GenericParameter("TA", iface);
             iface.GenericParameters.Add(ifaceT);
 
             var ifaceMethod = new MethodDefinition("Value", 
MethodAttributes.HideBySig | MethodAttributes.Abstract | MethodAttributes.
Virtual | MethodAttributes.NewSlot | MethodAttributes.Public, ifaceT);
             iface.Methods.Add(ifaceMethod);
 
             modDef.Types.Add(iface);
 
             var cls = new TypeDefinition("", "TestClass", TypeAttributes.
Public);
 
             var clsT = new GenericParameter("TB", cls);
             cls.GenericParameters.Add(clsT);
 
             cls.BaseType = modDef.TypeSystem.Object;
 
             var clsBaseIface = new GenericInstanceType(iface);
             clsBaseIface.GenericArguments.Add(clsT);
             cls.Interfaces.Add(clsBaseIface);
 
             var clsMethod = new MethodDefinition("Value_override", 
MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.
NewSlot | MethodAttributes.Final | MethodAttributes.Public, clsT);
             clsMethod.Overrides.Add(ifaceMethod);
             cls.Methods.Add(clsMethod);
 
             modDef.Types.Add(cls);
 
             var local = new VariableDefinition(clsT);
             clsMethod.Body.Variables.Add(local);
 
             var gen = clsMethod.Body.GetILProcessor();
             gen.Emit(OpCodes.Ldloca, local);
             gen.Emit(OpCodes.Initobj, clsT);
             gen.Emit(OpCodes.Ldloc, local);
             gen.Emit(OpCodes.Ret);
 
             assemblyDef.Write("CecilTestOutput.dll");
 
         }
     }
 }
 

Thanks.

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to