使用AssemblyBuilder保存DynamicMethod

时间:2018-10-26 06:28:37

标签: .net-assembly dynamicmethod

我有DynamicMethod,我想保存到磁盘中(.exe或.dll)。我发现以下链接Saving a DynamicMethod to disk是与我需要的内容相似的类似帖子。

  

*步骤1:

   var assemblyName = new AssemblyName("SomeName");
          var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave, @"c:");
          var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name +  ".dll");

  TypeBuilder builder = moduleBuilder.DefineType("Test", TypeAttributes.Public);
  var methodBuilder = builder.DefineMethod("DynamicCreate", MethodAttributes.Public, typeof(T), new[] { typeof(IDataRecord) }); 
  

第2步:

  /* this line is a replacement for your  new DynamicMethod(....)  line of code
  /* GENERATE YOUR IL CODE HERE */
  

第3步:

  var t = builder.CreateType();
  assemblyBuilder.Save(assemblyName.Name + ".dll");

我理解步骤1和步骤3,但不理解步骤2,有人可以解释步骤2吗?我已经创建了DymaicMethod,那么如何传递给生成器?

0 个答案:

没有答案