System.Collections.Generic.KeyNotFoundException与单声

2019-10-18 10:43发布

我发现了一个很奇怪的问题,同时创建单元测试,只有在单声道运行时(Xamarin在Mac在内)的发生,但运行Visual Studio中的罚款。 我孤立它,只要我能,我达到了一个点,使用单声道运行时我无法判断它是单声道,起订量或城堡DinamicProxy一个错误,虽然它只有崩溃。

这是代码:

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. 与单声道运行时运行时,它不工作。 抛出一个异常(见下文)
  3. 如果我从一个接口更改声明一个抽象类,它的工作原理与单
  4. 如果我改变从返回类型List<T>到别的,它的工作原理与单
  5. 如果删除了泛型类型<U>它的工作原理与单
  6. 得到了在Windows,Mac和Linux相同的结果,使用命令行构建从http://mono-project.com
  7. 在Windows中,它失败,单声道2.10.9,但内Xamarin.Studio作品(4.0.10)
  8. 在Mac上,它内Xamarin.Studio 失败 (4.0.10)
  9. 我重新编译链接起订量的agains最新DynamicProxy释放(Castle.Core.3.2.0),没有改变的行为。

使用虽然我发现这个问题的时候起订量,但如果它涉及到它,甚至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
文章来源: System.Collections.Generic.KeyNotFoundException with Mono, but not Microsoft's .NET