Emit with a human face

本文详细介绍了System.Reflection.Emit命名空间在.NET框架中的动态编程使用方法,通过示例展示了如何创建动态类型、方法和MSIL代码,以及如何将其编译并保存到磁盘。

Introduction

The System.Reflection.Emit namespace provides classes to create dynamic assemblies at runtime. It allows compilers and tools to emit MSIL, execute it and store it to a disk. Although Emit is a powerful tool, it is also extremely hard to use.

Let us take a look at the following example, which demonstrates the "normal" way of Emit programming:

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

namespace EmitDemo
{
    public interface IHello
    {
        void SayHello(string toWhom);
    }

    class Program
    {
        static void Main(string[] args)
        {
            AssemblyName asmName = new AssemblyName();

            asmName.Name = "HelloWorld";

            AssemblyBuilder asmBuilder =
                Thread.GetDomain().DefineDynamicAssembly
            (asmName, AssemblyBuilderAccess.RunAndSave);

            ModuleBuilder modBuilder  = asmBuilder.DefineDynamicModule
                            ("HelloWorld");

            TypeBuilder   typeBuilder = modBuilder.DefineType(
                                    "Hello",
                                    TypeAttributes.Public,
                                    typeof(object),
                                    new Type[] { typeof(IHello) });

            MethodBuilder methodBuilder = typeBuilder.DefineMethod("SayHello",
                         MethodAttributes.Private | MethodAttributes.Virtual,
                         typeof(void),
                         new Type[] { typeof(string) });

            typeBuilder.DefineMethodOverride(methodBuilder, 
                    typeof(IHello).GetMethod("SayHello"));

            ILGenerator il = methodBuilder.GetILGenerator();

            // string.Format("Hello, {0} World!", toWhom)
            //
            il.Emit(OpCodes.Ldstr, "Hello, {0} World!");
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Call, typeof(string).GetMethod
        ("Format", new Type[] { typeof(string), typeof(object) }));

            // Console.WriteLine("Hello, World!");
            //
            il.Emit(OpCodes.Call, typeof(Console).GetMethod
            ("WriteLine", new Type[] { typeof(string) }));
            il.Emit(OpCodes.Ret);

            Type   type  = typeBuilder.CreateType();

            IHello hello = (IHello)Activator.CreateInstance(type);

            hello.SayHello("Emit");
        }
    }
}
using System;

using BLToolkit.Reflection;
using BLToolkit.Reflection.Emit;

namespace EmitHelperDemo
{
    public interface IHello
    {
        void SayHello(string toWhom);
    }

    class Program
    {
        static void Main(string[] args)
        {
            EmitHelper emit = new AssemblyBuilderHelper("HelloWorld.dll")
                .DefineType  ("Hello", typeof(object), typeof(IHello))
                .DefineMethod(typeof(IHello).GetMethod("SayHello"))
                .Emitter;

            emit
                // string.Format("Hello, {0} World!", toWhom)
                //
                .ldstr   ("Hello, {0} World!")
                .ldarg_1
                .call    (typeof(string), "Format", typeof(string), 
                                            typeof(object))

                // Console.WriteLine("Hello, World!");
                //
                .call    (typeof(Console), "WriteLine", typeof(string))
                .ret()
                ;

            Type   type  = emit.Method.Type.Create();

            IHello hello = (IHello)TypeAccessor.CreateInstance(type);

            hello.SayHello("C#");
        }
    }
}



先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
### 回答1: "emit" is a function that is often used in event-driven programming to trigger or emit events. In JavaScript, the "emit" function is typically associated with an event emitter, which is an object that emits events and allows other objects to listen for those events. The "emit" function is used to trigger a specific event, and any listeners that are registered for that event will be notified and called. For example, if you have an event emitter object called "myEmitter", you can use the "emit" function to trigger an event like this: ``` myEmitter.emit('eventName', eventData); ``` This will trigger the "eventName" event and pass along the "eventData" as an argument. Any registered listeners for the "eventName" event will be notified and called with the provided data. If you are encountering an error message that says "emit is not a function", it may mean that the object you are trying to use the "emit" function on is not an event emitter or does not have the "emit" function implemented. Make sure that the object you are using has the appropriate functions and methods for event-driven programming. ### 回答2: defineEmits是Vue 3中的一个选项,用于声明组件所能触发的自定义事件。它是在组件的选项对象中使用,并且需要在组件中使用setup()函数才能生效。defineEmits的作用是将组件的自定义事件类型显式地列出,以便于静态分析工具的类型检测和提示。 在使用defineEmits时,我们需要将组件的自定义事件名称以数组的形式作为参数传递给defineEmits函数,例如: ``` import { defineEmits } from 'vue'; export default { setup() { const emit = defineEmits(['my-event']); // 触发自定义事件 const handleClick = () => { emit('my-event', payload); }; return { handleClick }; } } ``` 以上代码中,我们传递了一个名为'my-event'的自定义事件给defineEmits函数。然后使用emit函数触发该自定义事件,并传递一个参数payload。 根据你给出的错误提示 "emit is not a function",这可能是因为你没有正确地导入defineEmits函数,或者没有在setup函数中声明emit变量。 首先,要确保已经从Vue库中正确地导入defineEmits函数。导入语句应该类似于"import { defineEmits } from 'vue';"。 其次,在setup函数中,我们需要调用defineEmits函数并将其结果赋值给一个变量(通常命名为emit)。这样,我们才能使用emit变量来触发自定义事件。 总结一下,使用defineEmits可以明确列出组件的自定义事件类型,并且在组件中使用emit函数来触发这些事件。如果提示"emit is not a function",需要检查defineEmits函数的导入和在setup函数中的变量声明。 ### 回答3: defineEmits 是 Vue 3 中的一个选项,用于定义组件可以触发的自定义事件。它的作用是让父组件知道子组件可以触发哪些事件,方便父组件进行相关的处理。 在 Vue 3 中,通过 defineEmits 来定义子组件可以触发的事件列表,并且事件名称必须以小驼峰命名。 但需要注意的是,在使用 defineEmits 时,如果通过 emit 调用一个未定义的事件名称,就会出现 "emit is not a function" 的错误提示。这是因为 emit 函数在组件实例上是通过 emits 选项自动生成的,而 emits 选项又是由 defineEmits 定义的。 要解决这个问题,我们需要按照以下步骤进行操作: 1. 在子组件的选项中使用 defineEmits 定义可以触发的自定义事件,例如:defineEmits(['eventName'])。 2. 在子组件的模板中使用 emit 函数来触发自定义事件,例如:emit('eventName')。 3. 在父组件中引用子组件时,确保子组件中触发的自定义事件名称与父组件中监听的事件名称一致。 4. 如果还是出现 "emit is not a function" 的错误提示,可能是因为在调用 emit 时出现了拼写错误或其他语法错误,需要检查代码并逐一解决问题。 总之,defineEmits 提供了一种定义和使用子组件自定义事件的机制,但在使用 emit 函数时需要注意正确使用事件名称和避免出现语法错误,才能避免 "emit is not a function" 的错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值