1

我似乎无法弄清楚为什么我会收到运行以下代码的 InvalidCastException:

var item = new KeyValuePair<string, string>("key", "value");

Action<KeyValuePair<string, string>> kvrAction = 
    kvr =>Console.WriteLine(kvr.Value);

var result = kvrAction.BeginInvoke(item, null, null);
kvrAction.EndInvoke(result);

异常信息:

Test method Utilities.Tests.IEnumerableExtensionTests.ProveDelegateAsyncInvokeFailsForKeyValuePair threw exception:  System.Runtime.Remoting.RemotingException: The argument type '[key, value]' cannot be converted into parameter type 'System.Collections.Generic.KeyValuePair`2[System.String,System.String]'.
--->  System.InvalidCastException: Object must implement IConvertible..

任何帮助将不胜感激 =) 这段代码似乎适用于我扔给它的任何东西,除了 KeyValuePair<>。

更新:似乎任何结构都存在这种情况。我没有注意到 KeyValuePair<> 是一个结构,因此仅使用类进行测试。我仍然不明白为什么会这样。

更新 2:西蒙的回答帮助确认了这种行为是意外的,但是实现自定义类型不适用于我想要做的事情。我正在尝试在 IEnumerable<> 上实现一个扩展方法,以便为每个项目异步执行委托。我注意到针对通用 Dictionary 对象运行测试时出错。

    public static IEnumerable<T> ForEachAsync<T>(this IEnumerable<T> input, Action<T> act)
    {
        foreach (var item in input)
        {
            act.BeginInvoke(item, new AsyncCallback(EndAsyncCall<T>), null);
        }

        return input;
    }

    private static void EndAsyncCall<T>(IAsyncResult result)
    {
        AsyncResult r = (AsyncResult)result;
        if (!r.EndInvokeCalled)
        {
            var d = (Action<T>)((r).AsyncDelegate);
            d.EndInvoke(result);
        }
    }

我宁愿不使用对 T 的约束来限制该方法以确保仅使用类,因此我已按如下方式重构该方法以解决 BeginInvoke 的问题,但我之前没有直接使用 TreadPool 并希望确保我我没有错过任何重要的东西。

    public static IEnumerable<T> ForEachAsync<T>(this IEnumerable<T> input, Action<T> act)
    {
        foreach (var item in input)
            ThreadPool.QueueUserWorkItem(obj => act((T)obj), item);

        return input;
    }
4

1 回答 1

1

奇怪,似乎是.NET(C#?)中的某种错误,将参数编组到工作线程。

如果在传递的结构上实现 IConvertable:

struct MyPair<TKey, TValue> : IConvertable
{
    public readonly TKey Key;
    public readonly TValue Value;

    public MyPair(TKey key, TValue value)
    {
        Key = key;
        Value = value;
    }

    // I just used the smart-tag on IConvertable to get all these...
    // public X ToX(IFormatProvider provider) { throw new InvalidCastException(); }

    ...

    public object ToType(Type conversionType, IFormatProvider provider)
    {
        if (typeof(MyPair<TKey, TValue>).GUID == conversionType.GUID)
            return this;
        throw new InvalidCastException();
    }
}

它运行良好。传递的conversionType 没有通过.Equal()、IsAssignableFrom() 或除GUID 比较之外的任何其他我尝试过的方法,这可能与它首先要求IConvertable 的原因有关。

编辑:一个简单的解决方法是使用闭包来传递参数:

var data = new Dictionary<string, string> {
    { "Hello", "World" },
    { "How are", "You?" },
    { "Goodbye", "World!" }
};
foreach (var pair in data)
{
    var copy = pair; // define a different variable for each worker
    Action worker = () => Console.WriteLine("Item {0}, {1}", copy.Key, copy.Value);
    worker.BeginInvoke(null, null);
}

当然,如果您需要结果,则需要将 IAsyncResults 存储在另一个方向上,这可能与参数有相同的问题。作为替代方案,您可以在它们完成后将它们添加到集合中,但是锁定有点奇怪:

var data = new Dictionary<string, string> {
    { "Hello", "World" },
    { "How are", "You?" },
    { "Goodbye", "World!" }
};

var results = new List<KeyValuePair<string, string>>();
var pending = 0;
var done = new ManualResetEvent(false);

var workers = new List<Action>();
foreach (var pair in data)
{
    ++pending;
    var copy = pair; // define a different variable for each worker
    workers.Add(delegate()
    {
        Console.WriteLine("Item {0}, {1}", copy.Key, copy.Value);
        lock (results)
            results.Add(new KeyValuePair<string, string>("New " + copy.Key, "New " + copy.Value));
        if (0 == Interlocked.Decrement(ref pending))
            done.Set();
    });
}

foreach (var worker in workers)
    worker.BeginInvoke(null, null);

done.WaitOne();

foreach (var pair in results)
    Console.WriteLine("Result {0}, {1}", pair.Key, pair.Value);
于 2009-02-27T06:27:29.740 回答