Stack Exchange
Dynamically overriding an abstract method in c#
I have the following abstract class which CAN NOT be changed.
public abstract class AbstractThing {
   public String GetDescription() {
      return "This is " + GetName();
   }
   public abstract String GetName();
}

Now I would like to implement some new dynamic types from this like so.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "My.TempAssembly";
AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =  assemblyBuilder.DefineDynamicModule("DynamicThings");
TypeBuilder typeBuilder = moduleBuilder.DefineType(someName + "_Thing", 
                TypeAttributes.Public | 
                TypeAttributes.Class, 
                 typeof(AbstractThing));
MethodBuilder methodBuilder = typeBuilder.DefineMethod("GetName",    
                MethodAttributes.Public | 
                MethodAttributes.ReuseSlot |
                MethodAttributes.Virtual | 
                MethodAttributes.HideBySig,
                null,
                Type.EmptyTypes);

ILGenerator msil = methodBuilder.GetILGenerator();

msil.EmitWriteLine(selectionList);
msil.Emit(OpCodes.Ret);

However when I try to instantiate via 
typeBuilder.CreateType();

I get an exception saying that there is no implementation for GetName. Is there something I am doing wrong here. I can not see the problem.
Also, what would be the restrictions on instantiating such a class by name? For instance if I tried to instantiate via "My.TempAssembly.x_Thing" would it be availble for instantiation without the Type generated?

What do you mean by "instantiating such a class by name"? Unless you include the generated assembly in your build, you cannot refer to types in it in your program directly. You shall have to use Reflection or Activator.CreateInstance(Type).

Yep, thats exactly what I mean, Type.GetType("My.TempAssembly.x_Thing") then on to Activator.CreateInstance(type). It worked fine.

You have the wrong return type on the dynamically created function. It should be string instead of void:
typeBuilder.DefineMethod("GetName",   
MethodAttributes.Public | 
MethodAttributes.ReuseSlot |
MethodAttributes.Virtual | 
MethodAttributes.HideBySig,
typeof(string),
Type.EmptyTypes);


I would use Func instead. It doesn't require reflection to dynamically change the method.
public class AbstractThing {
   public String GetDescription() {
      return "This is " + GetName();
   }
   public Func<String> GetName { get; set; }
}

var thing = new AbstractThing();
thing.GetName = () => "Some Name"; // Or any other method
thing.GetDescription() == "This is Some Name";


This is not really the answer I am looking for, AbstractThing can not be changed. The goal is a string result My.TempAssembly.x_Thing that I can instantiate dynamically.

This doesn't answer the question, and it would need to Func, not Action

I know it doesn't answer the question directly, but just another way.
