使用Mono的System.Collections.Generic.KeyNotFoundException,但不是Microsoft的.NET

时间:2013-08-03 15:25:15

标签: c# generics mono moq castle-dynamicproxy

我在创建仅在Mono运行时(包括Mac上的Xamarin)时才发生的单元测试时发现了一个非常奇怪的问题,但在Visual Studio中运行正常。我尽可能地隔离了它,并且我达到了一个点,我无法判断它是否是Mono,Moq或Castle DinamicProxy的错误,尽管它只在使用Mono运行时崩溃。

这是代码:

using System;
using System.Collections.Generic;
using Moq;

namespace ConsoleApplication1
{
    public interface ISomething<T>
    {
        List<T> DoSomething<U>(/*Func<T, U> expression*/);
    }

    public class Foo { }

    public class Program
    {
        public static void Main(string[] args) {
            var mock = new Mock<ISomething<Foo>>();
            Console.WriteLine(mock.Object);
        }
    }
}

我知道这是:

  1. 它在Visual Studio 2012中编译并运行。输出为“Castle.Proxies.ISomething`1Proxy”
  2. 使用Mono运行时运行时不起作用。抛出异常(见下文)
  3. 如果我将声明从接口更改为抽象类,则它适用于Mono
  4. 如果我将返回类型从List<T>更改为其他任何内容,则可以使用Mono
  5. 如果我删除通用类型<U>,则可以使用mono
  6. 使用http://mono-project.com
  7. 命令行构建在Windows,Mac和Linux下获得相同的结果
  8. 在Windows中,它使用Mono 2.10.9失败,但在Xamarin.Studio(4.0.10)中工作
  9. 在Mac中,在Xamarin.Studio(4.0.10)
  10. 中失败
  11. 我重新编译了Moq链接最新的DynamicProxy版本( Castle.Core.3.2.0 ),没有改变行为。
  12. 虽然我在使用Moq时发现了这个问题,但我无法判断它是否与它有关,甚至与DynamicProxy有关。部分例外如下,完整版可在http://pastie.org/8203093

    找到
    mono ConsoleApplication1.exe
    
    [ERROR] FATAL UNHANDLED EXCEPTION: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
      at System.Collections.Generic.Dictionary`2[System.String,System.Reflection.Emit.GenericTypeParameterBuilder].get_Item (System.String key) [0x00000] in <filename unknown>:0
      at Castle.DynamicProxy.Internal.TypeUtil.CloseGenericParametersIfAny (Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter emitter, System.Type[] arguments) [0x00000] in <filename unknown>:0
      at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget (System.Type interfaceToProxy, System.Type[] additionalInterfacesToProxy, Castle.DynamicProxy.IInterceptor[] interceptors) [0x00000] in <filename unknown>:0
      at Moq.Proxy.CastleProxyFactory.CreateProxy[ISomething`1] (ICallInterceptor interceptor, System.Type[] interfaces, System.Object[] arguments) [0x00000] in <filename unknown>:0
      [....]
      at Moq.Mock`1[ConsoleApplication1.ISomething`1[ConsoleApplication1.Foo]].<InitializeInstance>b__0 () [0x00000] in <filename unknown>:0
      at Moq.PexProtector.Invoke (System.Action action) [0x00000] in <filename unknown>:0
      at Moq.Mock`1[ConsoleApplication1.ISomething`1[ConsoleApplication1.Foo]].InitializeInstance () [0x00000] in <filename unknown>:0
      at Moq.Mock`1[ConsoleApplication1.ISomething`1[ConsoleApplication1.Foo]].OnGetObject () [0x00000] in <filename unknown>:0
      at Moq.Mock.GetObject () [0x00000] in <filename unknown>:0
      at Moq.Mock.get_Object () [0x00000] in <filename unknown>:0
      at Moq.Mock`1[ConsoleApplication1.ISomething`1[ConsoleApplication1.Foo]].get_Object () [0x00000] in <filename unknown>:0
      at ConsoleApplication1.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
    

0 个答案:

没有答案