Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中动态重写抽象方法#_C# - Fatal编程技术网

C# 在c中动态重写抽象方法#

C# 在c中动态重写抽象方法#,c#,C#,我有以下不能更改的抽象类 public abstract class AbstractThing { public String GetDescription() { return "This is " + GetName(); } public abstract String GetName(); } 现在,我想实现一些新的动态类型 AssemblyName assemblyName = new AssemblyName(); assemblyName.Name

我有以下不能更改的抽象类

public abstract class AbstractThing {
   public String GetDescription() {
      return "This is " + GetName();
   }
   public abstract String GetName();
}
现在,我想实现一些新的动态类型

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);
但是,当我尝试通过

typeBuilder.CreateType();
我得到一个异常,说GetName没有实现。我有什么地方做错了吗。我看不出有什么问题


另外,按名称实例化此类类有哪些限制?例如,如果我试图通过“My.TempAssembly.x_Thing”进行实例化,那么在没有生成类型的情况下,它是否可以用于实例化?

我将使用Func。动态更改方法不需要反射

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";
公共类抽象事物{
公共字符串GetDescription(){
返回“This is”+GetName();
}
public Func GetName{get;set;}
}
var thing=新抽象事物();
thing.GetName=()=>“某个名字”;//或任何其他方法
thing.GetDescription()=“这是一个名字”;

动态创建的函数的返回类型错误。它应该是
string
,而不是
void

typeBuilder.DefineMethod("GetName",   
MethodAttributes.Public | 
MethodAttributes.ReuseSlot |
MethodAttributes.Virtual | 
MethodAttributes.HideBySig,
typeof(string),
Type.EmptyTypes);

这并不是我想要的答案,这是无法改变的。目标是一个字符串结果My.TempAssembly.x_,我可以动态实例化它。这不能回答问题,它需要Func,而不是Action。我知道它不能直接回答问题,只是另一种方式。你说的“按名称实例化这样一个类”是什么意思?除非在生成中包含生成的程序集,否则无法在程序中直接引用其中的类型。您必须使用反射或
Activator.CreateInstance(Type)
.Yep,这正是我的意思,键入.GetType(“My.TempAssembly.x_Thing”),然后转到Activator.CreateInstance(Type)。它工作得很好。