使用Reflection.Emit设置属性值

时间:2015-04-12 16:13:24

标签: c# reflection reflection.emit

我正在构建一个动态代理来拦截我正在编写的库中的一些方法。我可以成功创建我的代理类型但是当我尝试实现属性设置器时,我收到以下错误。

  

System.InvalidProgramException

     

增加信息:

     

Common Language Runtime检测到无效程序。

我的发射器代码如下:

public void Emit(FieldInfo interceptorField, 
                 MethodInfo method, 
                 TypeBuilder typeBuilder)
{
    // Get the method parameters for any setters.
    ParameterInfo[] parameters = method.GetParameters();
    ParameterInfo parameter = parameters.FirstOrDefault();

    // Define attributes.
    const MethodAttributes MethodAttributes = 
             MethodAttributes.Public | MethodAttributes.HideBySig |
             MethodAttributes.Virtual;

    // Define the method.
    MethodBuilder methodBuilder = typeBuilder.DefineMethod(
        method.Name,
        MethodAttributes,
        CallingConventions.HasThis,
        method.ReturnType,
        parameters.Select(param => param.ParameterType).ToArray());

    ILGenerator il = methodBuilder.GetILGenerator();

    // Set the correct flags to signal the property is managed 
    // and implemented in intermediate language.
    methodBuilder.SetImplementationFlags(
        MethodImplAttributes.Managed | MethodImplAttributes.IL);

    // This is the equivalent to:
    // IInterceptor interceptor = ((IProxy)this).Interceptor;
    // if (interceptor == null)
    // {
    //    throw new NotImplementedException();
    // }
    il.Emit(OpCodes.Ldarg_0);
    il.Emit(OpCodes.Callvirt, GetInterceptor);
    Label skipThrow = il.DefineLabel();
    il.Emit(OpCodes.Dup);
    il.Emit(OpCodes.Ldnull);
    il.Emit(OpCodes.Bne_Un, skipThrow);
    il.Emit(OpCodes.Newobj, NotImplementedConstructor);
    il.Emit(OpCodes.Throw);
    il.MarkLabel(skipThrow);

    // This is equivalent to: 
    // For get
    // return interceptor.Intercept(MethodBase.GetCurrentMethod(), null);
    // For set
    // interceptor.Intercept(MethodBase.GetCurrentMethod(), value);
    il.Emit(OpCodes.Call, GetCurrentMethod);
    il.Emit(parameter == null ? OpCodes.Ldnull : OpCodes.Ldarg_1);
    il.Emit(OpCodes.Call, InterceptorMethod);

    if (method.ReturnType != typeof(void))
    {
        il.Emit(OpCodes.Ret);
    }
}

使用Telerik JustDecompile查看输出代码(一个名为Bat的字符串属性)时,我得到以下内容:

public override void set_Bat(string str)
{
    IInterceptor interceptor = ((IProxy)this).Interceptor;
    if (interceptor == null)
    {
        throw new NotImplementedException();
    }
    interceptor.Intercept(MethodBase.GetCurrentMethod(), str);
}

使用Reflector

public override void set_Bat(string str)
{
    IInterceptor interceptor = ((IProxy)this).Interceptor;
    if (interceptor == null)
    {
        throw new NotImplementedException();
    }
}

注意最后一行是如何丢失的。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

事实证明,代码存在一些问题。

首先,当汉斯帕斯特指出我在两种情况下都没有回来。

使用以下内容修复。

if (method.ReturnType == typeof(void))
{
    il.Emit(OpCodes.Pop);
}

il.Emit(OpCodes.Ret);

另外,我打电话给MethodBase.GetCurrentMethod()这是行不通的。我需要使用MethodBase.GetMethodFromHandle代替并发出

il.Emit(OpCodes.Ldtoken, method);
il.Emit(OpCodes.Call, GetMethodFromHandle);

确保MethodInfo上下文正确指向基本类型。

这一切都是:

public override void set_Bat(string value)
{
    IInterceptor interceptor = this.Interceptor;
    if (interceptor == null)
    {
        throw new NotImplementedException();
    }
    interceptor.Intercept(methodof(Bar.set_Bat), value);
}