创建DynamicMethod以在任何类型上设置只读字段

时间:2017-04-26 23:56:17

标签: c# cil reflection.emit dynamicmethod

我的目标是在运行时创建一个委托,可以将任何引用类型中的任何字段(包括readonly)设置为用户指定的值。不幸的是,当包含类型的程序集指定VerificationException属性时,我的当前实现会在运行时抛出[AllowPartiallyTrustedCallers]

AssemblyOne

[assembly: AllowPartiallyTrustedCallers]
public class TypeOne
{
    public TypeOne(TypeTwo typeTwoField)
    {
        this.TypeTwoField = typeTwoField;
    }

    public TypeTwo TypeTwoField { get; }
}

AssemblyTwo

[assembly: AllowPartiallyTrustedCallers]
public class TypeTwo
{
    public TypeTwo(int i)
    {
        this.Int = i;
    }

    public int Int { get; }
}

Main

using System;
using System.Reflection;
using System.Reflection.Emit;
using AssemblyOne;
using AssemblyTwo;

namespace Main
{
    class Program
    {
        public class MyType
        {
            public MyType(TypeOne typeOneField)
            {
                this.TypeOneField = typeOneField;
            }

            public TypeOne TypeOneField { get; }
        }

        static void Main(string[] args)
        {
            var fieldInfo = typeof(TypeOne)
                .GetTypeInfo()
                .GetField(
                    "<TypeTwoField>k__BackingField",
                    BindingFlags.Instance | BindingFlags.NonPublic |
                    BindingFlags.Public);
            var setter = (Action<TypeOne, TypeTwo>) GetReferenceSetter(fieldInfo);
            var myType = new MyType(new TypeOne(new TypeTwo(1)));

            // Throws VerificationException
            setter(myType.TypeOneField, new TypeTwo(2));
        }

        public static Delegate GetReferenceSetter(FieldInfo field)
        {
            var delegateType = typeof(Action<,>)
                .MakeGenericType(field.DeclaringType, field.FieldType);

            var method = new DynamicMethod(
                field.Name + "Set",
                null,
                new[] {field.DeclaringType, field.FieldType},
                field.DeclaringType,
                skipVisibility: true);

            var emitter = method.GetILGenerator();
            emitter.Emit(OpCodes.Ldarg_0);
            emitter.Emit(OpCodes.Ldarg_1);
            emitter.Emit(OpCodes.Stfld, field);
            emitter.Emit(OpCodes.Ret);

            return method.CreateDelegate(delegateType);
        }
    }
}

因此MyType有一个TypeOne,只有TypeTwo。在这种情况下,DynamicMethod会在运行时抛出VerificationException

是否可以创建一个适用于您抛出的任何声明类型+字段类型的委托?如果是这样,怎么样?

我意识到readonly字段不应该在构建后设置,但是这样做的目的是用于反序列化&amp;深拷贝。

2 个答案:

答案 0 :(得分:1)

动态方法在修改其安全性方面非常有限。我怀疑使用 AssemblyBuilder 可能会绕过安全检查,但我没有尝试过。

相反,可能能够实现此目的的是使用其他访问字段的方法,在 TypedReference 类型中。

public static unsafe void SetValue<T>(object inst, FieldInfo fi, T val)
{
    var mi = typeof(TypedReference).GetMethod("InternalMakeTypedReference", BindingFlags.NonPublic | BindingFlags.Static);
    var sig = MethodSignature.FromMethodInfo(mi);
    var del = ReflectionTools.NewCustomDelegateType(sig.ReturnType, sig.ParameterTypes);
    var inv = mi.CreateDelegate(del);
    TypedReference tr;
    var ptr = Pointer.Box(&tr, typeof(void*));
    inv.DynamicInvoke(ptr, inst, new[]{fi.FieldHandle.Value}, fi.FieldType);
    __refvalue(tr, T) = val;
}

FromMethodInfo NewCustomDelegateType 来自 SharpUtils ,需要调用 InternalMakeTypedReference ,这是能够获得对任何领域的引用。它的签名如下:

private unsafe static extern void InternalMakeTypedReference(void* result, object target, IntPtr[] flds, RuntimeType lastFieldType);

您可以将库方法替换为您自己的库方法,只需构建适当的委托类型( Action 因指针而无法使用)。由于内部 RuntimeType ,您无法直接创建委托类型。令人惊讶的是, DynamicInvoke 适用于委托,并且创建了类型引用。

然而,要使用这个伏都教,我不得不降低装配中的安全检查,所以我不确定它是否也适用于装配系统:

[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1, SkipVerificationInFullTrust=true)]

另请注意,此代码使用了大量未记录的功能,并且可能随时停止工作。使用风险自负。

答案 1 :(得分:0)

我有一些退出代码可能对你有所帮助。

/// <summary>
    /// create a getter delegate for a static field.
    /// </summary>
    /// <typeparam name="TField"></typeparam>
    /// <param name="staticType">the type that contains the field. </param>
    /// <param name="fieldName">the field that you want to get the value.</param>
    /// <returns>the getter delegate.</returns>
    public static Func<TField> CreateStaticFieldGetter<TField>(Type staticType, string fieldName)
    {
        var fieldInfo = staticType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static);
        var body = Expression.Field(null, fieldInfo);
        Expression<Func<TField>> lambda = Expression.Lambda<Func<TField>>(body);
        return lambda.Compile();
    }


    public static Action<TField> CreateStaticFieldSetter<TField>(Type staticType, string fieldName)
    {
        ParameterExpression p1 = Expression.Parameter(typeof(TField), "p1");
        var fieldInfo = staticType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static);
        var leftExpr = Expression.Field(null, fieldInfo);
        var body = Expression.Assign(leftExpr, p1);
        Expression<Action<TField>> lambda = Expression.Lambda<Action<TField>>(body, p1);
        return lambda.Compile();
    }



    public static Func<TTarget, TField> CreateInstanceFieldGetter<TTarget, TField>(string fieldName)
    {
        ParameterExpression p1 = Expression.Parameter(typeof(TTarget), "p1");
        var fieldInfo = typeof(TTarget).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
        var body = Expression.Field(Expression.Convert(p1, typeof(TTarget)), fieldInfo);
        Expression<Func<TTarget, TField>> lambda = Expression.Lambda<Func<TTarget, TField>>(body, p1);
        return lambda.Compile();
    }



    public static Action<TTarget, TProperty> CreateInstanceFieldSetter<TTarget, TProperty>(string filedName)
    {
        ParameterExpression p1 = Expression.Parameter(typeof(TTarget), "p1");
        ParameterExpression p2 = Expression.Parameter(typeof(TProperty), "p2");

        var member = typeof(TTarget).GetField(filedName, BindingFlags.NonPublic | BindingFlags.Instance);
        var m1 = Expression.MakeMemberAccess(Expression.Convert(p1, typeof(TTarget)), member);
        BinaryExpression body = Expression.Assign(m1, p2);

        Expression<Action<TTarget, TProperty>> lambda = Expression.Lambda<Action<TTarget, TProperty>>(body, p1, p2);
        return lambda.Compile();
    }