【发布时间】:2011-10-24 03:53:43
【问题描述】:
我收到来自 Castle Dynamic Proxy 的参数异常,同时使用 Moq 创建一个对象模拟,该对象使用具有接口约束的通用方法实现嵌套通用接口。
例外情况是:System.ArgumentException:无法将父级设置为接口。
在创建模拟后直接访问模拟的 Object 属性时发生。 (为了便于阅读,调用栈在底部)
代码简单,自描述:
public interface A<T>
{
void Method<Z>() where Z : T;
}
public interface B
{
}
[Test]
public void MockNestedGenericInterfaceTest()
{
Mock<A<B>> mock = new Mock<A<B>>();
var o = mock.Object; //argument exception here
}
如果 where Z : T 子句被删除,测试不会产生异常。
我做了一些研究,发现了一张票 here。我正在使用最新版本的 Moq 和 Castle。
是否有解决此问题的方法?我看到它工作的唯一方法是手动模拟实现。 Rhino mocks 也不适合我。
谢谢。
调用栈:
at System.Reflection.Emit.TypeBuilder.SetParent(Type parent)
在 Castle.DynamicProxy.Generators.Emitters.GenericUtil.CopyGenericArguments(MethodInfo methodToCopyGenericsFrom, Dictionary2 name2GenericType, ApplyGenArgs genericParameterGenerator)
at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.CopyGenericParametersFromMethod(MethodInfo methodToCopyGenericsFrom)
at Castle.DynamicProxy.Generators.InvocationTypeGenerator.Generate(ClassEmitter class, ProxyGenerationOptions options, INamingScope namingScope)
at Castle.DynamicProxy.Contributors.InterfaceProxyWithoutTargetContributor.GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGenerationOptions options)
at Castle.DynamicProxy.Contributors.InterfaceProxyWithoutTargetContributor.GetMethodGenerator(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
at Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementMethod(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
at Castle.DynamicProxy.Contributors.CompositeTypeContributor.Generate(ClassEmitter class, ProxyGenerationOptions options)
at Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope)
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
at Moq.Proxy.CastleProxyFactory.CreateProxy(ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
at Moq.Mock1.b__0()
在 Moq.Mock1.InitializeInstance()
at Moq.Mock1.OnGetObject()
在 Moq.Mock`1.get_Object()
【问题讨论】:
标签: moq aop rhino-mocks castle castle-dynamicproxy