cil - Creating a DynamicType in .NET implementing an interface but using member implementations from a base class -


i attempting generate dynamic class implementing interface, 1 or more of members exists in base. compiled following code in c# , examined in reflector see c# compiler does.

class baseclass {     public string bob     {         { return "bob"; }     } }  interface istuff {     string bob { get; } }  class subclass : baseclass, istuff { } 

reflector not show implementation in subclass.

.class private auto ansi beforefieldinit subclass     extends enterprise.services.operationalactions.business.filters.baseclass     implements enterprise.services.operationalactions.business.filters.istuff { } 

but if not emit member explicitly, typebuilder.createtype() throws invalidoperationexception stating member not have implementation. question is, how tell typebuilder interface member should take it's implementation base?

it looks typebuilder have add private pass-thru, make happy (below). try using ikvm builder - identical api, might not have limitation.

using system; using system.reflection; using system.reflection.emit; public class baseclass {     public string bob     {         { return "bob"; }     } }  public interface istuff {     string bob { get; } } static class program {     static void main()     {         var name = new assemblyname("foo");         var asm = appdomain.currentdomain.definedynamicassembly(name, assemblybuilderaccess.run);         var mod = asm.definedynamicmodule("foo");         var parent = typeof(baseclass);         var type = mod.definetype("subclass", parent.attributes, parent);         type.addinterfaceimplementation(typeof(istuff));          var bob_get = type.definemethod("bob_get", methodattributes.virtual | methodattributes.private,             typeof(string), type.emptytypes);         var il = bob_get.getilgenerator();         il.emit(opcodes.ldarg_0);         il.emitcall(opcodes.callvirt, parent.getproperty("bob").getgetmethod(), null);         il.emit(opcodes.ret);         type.definemethodoverride(bob_get, typeof(istuff).getproperty("bob").getgetmethod());         var final = type.createtype();         istuff obj = (istuff) activator.createinstance(final);         console.writeline(obj.bob);     } } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -