我正在尝试构建一个动态的Property Accessor。想要像真正调用Property一样快的东西。不想走反射路线,因为它非常慢。所以我选择使用DynamicAssembly并使用ILGenerator注入IL。以下是似乎与ILGenerator相关的代码
        Label nulllabel = getIL.DefineLabel();
        Label returnlabel = getIL.DefineLabel();
        //_type = targetGetMethod.ReturnType;
        if (methods.Count > 0)
        {
            getIL.DeclareLocal(typeof(object));
            getIL.DeclareLocal(typeof(bool));
            getIL.Emit(OpCodes.Ldarg_1); //Load the first argument
            //(target object)
            //Cast to the source type
            getIL.Emit(OpCodes.Castclass, this.mTargetType);
            //Get the property value
            foreach (var methodInfo in methods)
            {
                getIL.EmitCall(OpCodes.Call, methodInfo, null);
                if (methodInfo.ReturnType.IsValueType)
                {
                    getIL.Emit(OpCodes.Box, methodInfo.ReturnType);
                    //Box if necessary
                }
            }
            getIL.Emit(OpCodes.Stloc_0); //Store it
            getIL.Emit(OpCodes.Br_S,returnlabel);
            getIL.MarkLabel(nulllabel);
            getIL.Emit(OpCodes.Ldnull);
            getIL.Emit(OpCodes.Stloc_0);
            getIL.MarkLabel(returnlabel);
            getIL.Emit(OpCodes.Ldloc_0);
        }
        else
        {
            getIL.ThrowException(typeof(MissingMethodException));
        }
        getIL.Emit(OpCodes.Ret);
因此,在上面获取第一个参数,它是包含属性的对象。方法集合包含嵌套属性(如果有)。对于每个属性,我使用EmitCall将该值放在堆栈上,然后尝试装箱。这就像一个魅力。
唯一的问题是,如果您具有Order.Instrument.Symbol.Name之类的属性,并假定Instrument对象为null。然后,代码将引发空对象异常。
所以我做了这个,我引入了空检查
            foreach (var methodInfo in methods) …