1

经过 2 周的开发后,我第一次在 iphone 上尝试了我的应用程序,它在以下行崩溃(在模拟器中完美运行):

我有以下代码:

private readonly Dictionary<string, QueueItem queued = new Dictionary<string, QueueItem>();

private void Processqueue()
{
    KeyValuePair<string, QueueItem> valuePair = queued.FirstOrDefault();
    // Crashes with: System.TypeInitializationException has been thrown
    // "And exception was thrown by the type initializer for PredicateOf`1"
}

private class QueueItem
{
   public string Url { get; set; }
   public Action<string> ImageLoaded { get; set; }
   public bool Pending { get; set; }
}

希望有人知道该怎么做。

谢谢

4

3 回答 3

2

不知何故,AOT 编译器无法检测到这种情况,所以是的,您应该提交一份关于它的错误报告。同时,您可以通过以下方式解决此问题:

KeyValuePair<string, QueueItem> valuePair = queued.FirstOrDefault (delegate { return true; });

这将避免在做完全相同的事情(执行明智)时达到 PredicateOf 并且比循环自己短一点。

此外,您还得到了 TypeLoadException,因为原始异常发生在静态构造函数中(这总是导致 TLE,内部异常应该是您期望看到的异常)。

更新:这是作为bug #300提交的,并在 MonoTouch (4.2+) 的最新版本中修复

于 2011-07-17T15:21:38.167 回答
0

可能是因为静态编译器无法确定对字典中 FirstOrDefault 的调用将需要 PredicateOf 的编译。

于 2011-06-30T14:10:07.247 回答
0

AOTHint()向包含该方法的类添加一个静态构造函数和一个新的静态Processqueue()方法。在AOTHint()中,创建您遇到问题的特定泛型类的新实例,并调用遇到问题的方法。

static MyClass() {
    AOTHint();
}

static void AOTHint()
{
    // @fixes: ExecutionEngineException: Attempting to JIT compile method 'System.Collections.Generic.Dictionary`1.FirstOrDefault ()' while running with --aot-only.
    (new Dictionary<string, QueueItem>()).FirstOrDefault();
}

Note: I haven't tried running your specific code with the above in MonoTouch; this is an adapted solution from what I've used a number of times before for “Attempting to JIT compile method” issues.  If this doesn't work, you'll likely need to play with the types specified and called in AOTHint().

Do this for each class that has problem methods, for each of the problem types, and your worries will be gone in the time between now and whenever the Mono figures out how to make their generic compilation less-buggy.

This works by making sure the AOT compiler knows exactly what you need, at the minimal cost of wastefully creating a handful empty generic objects and calling a handful of LINQ methods on them at app launch time.

于 2014-08-21T05:18:03.450 回答