如何动态发送并保存到磁盘组件? [英] How Do I Emit And Save To Disk An Asembly Dynamically?

查看:79
本文介绍了如何动态发送并保存到磁盘组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下内容



I have written the following

namespace SimpleDynamic
{
    class Program
    {
        public class SimpleClass
        {
         public string Name;
         public int Age;

        }



        static void Main(string[] args)
        {
         AppDomain appDomain = AppDomain.CurrentDomain;
         AssemblyName aName = new AssemblyName("SimpleDyn");
         AssemblyBuilder assemBuilder = appDomain.DefineDynamicAssembly(aName,
           AssemblyBuilderAccess.Save, @"c:\assemblies");
         ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule("SimpleDyn.dll",
           "SimpleDyn.dll");


         TypeBuilder tb = modBuilder.DefineType("SimpleDyn.SimpleClass",
            TypeAttributes.Public | TypeAttributes.BeforeFieldInit, typeof(object));

         FieldBuilder field1 = tb.DefineField("Name", typeof(string), FieldAttributes.Public);
         FieldBuilder field2 = tb.DefineField("Age", typeof(int), FieldAttributes.Public);

         tb.CreateType();
         assemBuilder.Save("SimpleDyn.dll");


        }
    }
}



然而当我比较两个SimpleClass的清单时使用ildasm(静态和动态)它们完全不同,似乎动态类型清单已被切断,除了我从其他项目静态引用生成的dll,动态SimpleClass不显示其成员。

please有人可以帮助我吗?


however using ildasm when I compare the manifest of both SimpleClass (static and dynamic )they are totally different, it seems that the dynamic type manifest has been cut off,besides when I reference statically the generated dll from other project, dynamic SimpleClass does not show its members.
please can someone help me?

推荐答案

这是一次尝试,这次是Dobbs博士命名模式,

现在我意识到了什么Sergei可能意味着说两次保存,第一个静态定义的Simpleclass,第二个动态定义的SimpleClass是不同的,因为它们分别位于不同的名称空间,SimpleDynamic和SimpleDyn,在前面的代码片段中并不清楚,因为顶级名称空间声明不可见但是,在这次尝试中,我已将动态构建的类重命名为SimpleClass1,这两个构建目的是相同的并排课程是使用ildasm进行比较。











Here it is one attempt more, this time following Dr. Dobbs naming pattern,
now I realize what Sergei could mean when saying saving twice,the first statically defined Simpleclass, and the second dynamically defined SimpleClass are different,because they live in different namespaces,SimpleDynamic and SimpleDyn respectively,in the previous snippets was not clear cause the top namespace declaration was not visible,however in this attempt I have renamed the dynamically built class to SimpleClass1,the purpose of building these two identical classes side by side is to compare them using ildasm.





namespace SimpleDynamic
{
    class Program
    {
        public class SimpleClass
        {
         public string Name;
         public int Age;

        }



        static void Main(string[] args)
        {
         AppDomain appDomain = AppDomain.CurrentDomain;
         AssemblyName aName = new AssemblyName();
            aName.Name = "SimpleReflection";
         AssemblyBuilder assemBuilder = appDomain.DefineDynamicAssembly(aName,
           AssemblyBuilderAccess.Save);
         ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule(aName.Name,
           "SimpleDyn.dll");


         TypeBuilder tb = modBuilder.DefineType("SimpleDyn.SimpleClass1",
            TypeAttributes.Public | TypeAttributes.BeforeFieldInit, typeof(object));

         FieldBuilder field1 = tb.DefineField("Name", typeof(string), FieldAttributes.Public);
         FieldBuilder field2 = tb.DefineField("Age", typeof(int), FieldAttributes.Public);

         tb.CreateType();
         assemBuilder.Save("SimpleDyn.dll");


        }
    }


请参阅我对该问题的评论。

这篇MSDN文章中的代码示例解释了所有内容: AssemblyBuilder类(System.Reflection.Emit) [ ^ ]。



参见使用Reflection.Emit在运行时生成代码Dobb博士 [ ^ ]。



请注意,在创建模块时,只需要保存一次程序集,以及模块中的代码。在这种方法中,你应该在最后输出一个文件。



-SA
Please see my comment to the question.
The code sample in this MSDN article explains it all: AssemblyBuilder Class (System.Reflection.Emit)[^].

See also Generating Code at Run Time With Reflection.Emit | Dr Dobb's[^].

Note that you need to save the assembly only once, when the module is created, as well as the code in the module. In this approach, you should have only one file on output, at the very end.

—SA

这篇关于如何动态发送并保存到磁盘组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆