2

我正在努力创建对 private 的调用ImmutableDictionary.Add,这使我能够利用KeyCollisionBehavior更精细的控制(该Add方法仅在键和值不同时抛出,我需要它总是抛出)。

我可以通过基本反射到达我想要的位置,但是,调用InvokeonMethodInfoDynamicInvoke委托的开销很大(事实上,每次调用的时间几乎增加了三倍,这在我的场景中太重要了)。

我需要调用的函数的签名:

/// call property Origin, this returns a private MutationInput<,>
private MutationInput<TKey, TValue> Origin {get; }

/// call ImmutableDictionary<,>.Add, this takes MutationInput<,> and returns private MutationResult<,>
private static MutationResult<TKey, TValue> Add(TKey key, TValue value, KeyCollisionBehavior<TKey, TValue> behavior, MutationInput<TKey, TValue> origin);

/// then call MutationResult<,>.Finalize
internal ImmutableDictionary<TKey, TValue> Finalize(ImmutableDictionary<TKey, TValue> priorMap);

这里的挑战是我需要传递一个私有类型,并且私有类型是签名的一部分。

通常,在调用 之后CreateDelegate,您可以简单地将其转换为 a Func<X, Y, Z>,这会提供近乎直接的调用速度。Func<,>但是如果泛型类型是私有的和/或在编译时不知道,我不知道如何创建。使用object不起作用,会在演员表上产生运行时异常。

这是我目前拥有的代码的缩短版本(删除了很​​多 try/catch 和检查)。这有效:

/// Copy of enum type from Github source of ImmutableDictionary
type KeyCollisionBehavior =
    /// Sets the value for the given key, even if that overwrites an existing value.
    | SetValue = 0
    /// Skips the mutating operation if a key conflict is detected.
    | Skip = 1
    /// Throw an exception if the key already exists with a different value.
    | ThrowIfValueDifferent = 2
    /// Throw an exception if the key already exists regardless of its value.
    | ThrowAlways = 3

/// Simple wrapper DU to add type safety
type MutationInputWrapper = 
    /// Wraps the type ImmutableDictionary<K, V>.MutationInput, required as 4th param in the internal Add#4 method
    | MutationInput of obj

/// Simple wrapper to add type-safety
type MutationResultWrapper =
    /// Wraps the type ImmutableDictionary<K, V>.MutationResult, which is the result of an internal Add#4 operation
    | MutationResult of obj

/// Type abbreviation
type BclImmDict<'Key, 'Value> = System.Collections.Immutable.ImmutableDictionary<'Key, 'Value>

/// Private extensions to ImmutableDictionary
type ImmutableDictionary<'Key, 'Value>() =
    static let dicType = typeof<System.Collections.Immutable.ImmutableDictionary<'Key, 'Value>>
    static let addMethod = dicType.GetMethod("Add", BindingFlags.NonPublic ||| BindingFlags.Static)
    static let addMethodDelegate = 
        let parameters = addMethod.GetParameters() |> Array.map (fun p -> p.ParameterType)
        let funType = 
            typedefof<Func<_, _, _, _, _>>.MakeGenericType [|
                parameters.[0]
                parameters.[1]
                parameters.[2]
                parameters.[3]
                addMethod.ReturnType
            |]
        Delegate.CreateDelegate(funType, addMethod) // here one would normally cast to Func<X, Y...>

    static let mutationResultFinalizeMethod = 
        if not(isNull addMethod) && not(isNull(addMethod.ReturnParameter)) then
            /// Nested private type MutationResult, for simplicity taken from the return-param type of ImmutableDictionary.Add#4
            let mutationResultType = addMethod.ReturnParameter.ParameterType
            if not(isNull mutationResultType) then
                mutationResultType.GetMethod("Finalize", BindingFlags.NonPublic ||| BindingFlags.Instance ||| BindingFlags.DeclaredOnly)
            else
                null
        else
            null

    /// System.Collections.Immutable.ImmutableDictionary.get_Origin  // of valuetype ImmutableDictionary<,>.MutationInput<,>
    static let getOrigin = dicType.GetProperty("Origin", BindingFlags.NonPublic ||| BindingFlags.Instance)

    /// Calls private member ImmutableDictionary<,>.Add(key, value, behavior, origin), through reflection
    static member private refl_Add(key: 'Key, value: 'Value, behavior: KeyCollisionBehavior, MutationInput origin) =
        // use Invoke or DynamicInvoke makes little difference.
        //addMethod.Invoke(null, [|key; value; behavior; origin|])
        addMethodDelegate.DynamicInvoke([|box key; box value; box <| int behavior; origin|])
        |> MutationResult

    /// Gets the "origin" of an ImmutableDictionary, by calling the private property get_Origin
    static member private refl_GetOrigin(this: BclImmDict<'Key, 'Value>) =
        getOrigin.GetValue this
        |> MutationInput

    /// Finalizes the result by taking the (internal) MutationResult and returning a new non-mutational dictionary
    static member private refl_Finalize(MutationResult mutationResult, map: BclImmDict<'Key, 'Value>) =
        mutationResultFinalizeMethod.Invoke(mutationResult, [|map|])
        :?> BclImmDict<'Key, 'Value>

    /// Actual Add, with added control through CollisionBehavior
    static member InternalAddAndFinalize(key: 'Key, value: 'Value, behavior, thisMap) =
        let origin = ImmutableDictionary.refl_GetOrigin(thisMap)
        let mutationResult = ImmutableDictionary.refl_Add(key, value, behavior, origin)
        let finalizedMap = ImmutableDictionary.refl_Finalize(mutationResult, thisMap)
        finalizedMap

我意识到上面的代码是用 F# 编写的,但是如果你知道如何在 C# 中解决这个问题,我可以将你的答案翻译成我喜欢的目标语言。

4

3 回答 3

3

我认为你过于复杂了。从备注

如果给定的键/值对已经存在于字典中,则返回字典的现有实例。

Add因此,我们可以通过查看该方法是否返回相同的字典引用来判断给定的键/值是否已经存在于字典中。

在 C# 中:

public static ImmutableDictionary<TKey, TValue> AddAndThrowIfAlreadyPresent<TKey, TValue>(
    ImmutableDictionary<TKey, TValue> dict,
    TKey key,
    TValue value)
{
    // null checks, etc
    var newDict = dict.Add(key, value);
    if (newDict == dict)
        throw new ArgumentException($"An element with the same key and value already exists. Key: {key}");
    return newDict;
}
于 2020-01-21T17:35:12.380 回答
2

我认为其他评论者的观点是,在这种特殊情况下,这种策略可能不是最好的。但是,这里有一个普遍的问题,我会从表面上看:如何为一个方法创建一个委托,该方法的类型由于它们是私有的或内部的而无法通过名称访问?

由于您无法引用某些类型名称,因此您无法创建比 Invoke/DynamicInvoke 快得多的强类型委托。在这种情况下,想法是在运行时使用 System.Reflection.Emit.DynamicMethod 为包装器方法生成 IL,该方法调用具有不可访问类型的方法,但此包装器仅公开您有权访问的类型。DynamicMethod 可以由另一个程序集中的类型“拥有”,从而绕过一些可见性检查。困难在于您必须准确地告诉运行时为此包装方法发出什么 IL,因此很难在其中实现复杂的逻辑。在这种情况下,事情很简单,可以手动编写:获取一个属性(Origin)并调用两个方法(Add 和 Finalize)。

这是 C# 中的一个实现:

enum KeyCollisionBehavior
{
    SetValue = 0,
    Skip = 1,
    ThrowIfValueDifferent = 2,
    ThrowAlways = 3,
}

internal static class ImmutableDictionaryHelper<TKey, TValue>
{
    private static readonly MethodInfo OriginPropertyGetter = typeof(ImmutableDictionary<TKey, TValue>)
        .GetProperty("Origin", BindingFlags.Instance | BindingFlags.NonPublic).GetGetMethod(true);

    private static readonly MethodInfo AddMethod = typeof(ImmutableDictionary<TKey, TValue>)
        .GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name == "Add" && m.GetParameters().Length == 4).FirstOrDefault();

    private static readonly Type MutationResultType = AddMethod.ReturnType;

    private static readonly MethodInfo FinalizeMethod = MutationResultType
        .GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

    private static readonly Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> AddAndFinalize = CreateAddAndFinalize();

    private static Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> CreateAddAndFinalize()
    {
        var method = new DynamicMethod(
            nameof(AddAndFinalize),
            typeof(ImmutableDictionary<TKey, TValue>),
            new[] { typeof(TKey), typeof(TValue), typeof(KeyCollisionBehavior), typeof(ImmutableDictionary<TKey, TValue>) },
            typeof(ImmutableDictionary<TKey, TValue>));

        var ilGen = method.GetILGenerator();
        ilGen.DeclareLocal(OriginPropertyGetter.ReturnType);
        ilGen.DeclareLocal(AddMethod.ReturnType);

        // var origin = dictionary.Origin;
        ilGen.Emit(OpCodes.Ldarg_3);
        ilGen.Emit(OpCodes.Callvirt, OriginPropertyGetter);
        ilGen.Emit(OpCodes.Stloc_0);

        // var result = Add(key, value, behavior, origin)
        ilGen.Emit(OpCodes.Ldarg_0);
        ilGen.Emit(OpCodes.Ldarg_1);
        ilGen.Emit(OpCodes.Ldarg_2);
        ilGen.Emit(OpCodes.Ldloc_0);
        ilGen.Emit(OpCodes.Call, AddMethod);
        ilGen.Emit(OpCodes.Stloc_1);

        // var newDictionary = result.Finalize(dictionary);
        ilGen.Emit(OpCodes.Ldloca_S, 1);
        ilGen.Emit(OpCodes.Ldarg_3);
        ilGen.Emit(OpCodes.Call, FinalizeMethod);

        // return newDictionary;
        ilGen.Emit(OpCodes.Ret);

        var del = method.CreateDelegate(typeof(Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>>));
        var func = (Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>>)del;
        return func;
    }

    public static ImmutableDictionary<TKey, TValue> Add(ImmutableDictionary<TKey, TValue> source, TKey key, TValue value, KeyCollisionBehavior behavior)
    {
        if (source == null)
            throw new ArgumentNullException(nameof(source));

        return AddAndFinalize(key, value, behavior, source);
    }
}

需要指出的一个细节是 CLR 将枚举作为整数处理,因此我们可以创建自己的 KeyCollisionBehavior 枚举,该枚举与我们无法在没有任何显式转换的情况下访问的私有枚举兼容。

于 2020-01-21T21:25:21.353 回答
1

这是另一种方法,它与@Mike's answer做同样的事情,但使用编译表达式代替(这更容易阅读和编写):

enum KeyCollisionBehavior
{
    SetValue = 0,
    Skip = 1,
    ThrowIfValueDifferent = 2,
    ThrowAlways = 3,
}

internal static class ImmutableDictionaryHelper<TKey, TValue>
{
    private static readonly Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> AddAndFinalize = CreateAddAndFinalize();

    private static Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>> CreateAddAndFinalize()
    {
        var originPropertyGetter = typeof(ImmutableDictionary<TKey, TValue>)
            .GetProperty("Origin", BindingFlags.Instance | BindingFlags.NonPublic).GetGetMethod(true);
        var addMethod = typeof(ImmutableDictionary<TKey, TValue>)
            .GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name == "Add" && m.GetParameters().Length == 4).FirstOrDefault();
        var finalizeMethod = addMethod.ReturnType
            .GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        var key = Expression.Parameter(typeof(TKey), "key");
        var value = Expression.Parameter(typeof(TValue), "value");
        var behavior = Expression.Parameter(typeof(KeyCollisionBehavior), "behavior");
        var dictionary = Expression.Parameter(typeof(ImmutableDictionary<TKey, TValue>), "dictionary");

        // var convertedBehavior = (ImmutableDictionary<TKey, TValue>.KeyCollisionBehavior)behavior
        var convertedBehavior = Expression.Convert(behavior, addMethod.GetParameters()[2].ParameterType);
        // var origin = dictionary.Origin;
        var origin = Expression.Property(dictionary, originPropertyGetter);
        // var result = Add(key, value, behavior, origin)
        var result = Expression.Call(addMethod, key, value, convertedBehavior, origin);
        // var newDictionary = result.Finalize(dictionary);
        var newDictionary = Expression.Call(result, finalizeMethod, dictionary);

        var func = Expression.Lambda<Func<TKey, TValue, KeyCollisionBehavior, ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>>>(
            newDictionary, key, value, behavior, dictionary).Compile();
        return func;
    }

    public static ImmutableDictionary<TKey, TValue> Add(ImmutableDictionary<TKey, TValue> source, TKey key, TValue value, KeyCollisionBehavior behavior)
    {
        if (source == null)
            throw new ArgumentNullException(nameof(source));

        return AddAndFinalize(key, value, behavior, source);
    }
}
于 2020-01-23T09:18:56.713 回答