我对Moq完全陌生,现在正尝试为它创建一个模拟System.Reflection.Assembly类。我正在使用此代码:

var mockAssembly = new Mock<Assembly>();
mockAssembly.Setup( x => x.GetTypes() ).Returns( new Type[] {
    typeof( Type1 ),
    typeof( Type2 )
} );

但是当我运行测试时,我得到了下一个异常:
System.ArgumentException : The type System.Reflection.Assembly
implements ISerializable, but failed to provide a deserialization
constructor
Stack Trace:
   at
Castle.DynamicProxy.Generators.BaseProxyGenerator.VerifyIfBaseImplementsGet­ObjectData(Type
baseType)
   at
Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[]
interfaces, ProxyGenerationOptions options)
   at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type
classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
   at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type
classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, Object[] constructorArguments,
IInterceptor[] interceptors)
   at Moq.Proxy.CastleProxyFactory.CreateProxy[T](ICallInterceptor
interceptor, Type[] interfaces, Object[] arguments)
   at Moq.Mock`1.<InitializeInstance>b__0()
   at Moq.PexProtector.Invoke(Action action)
   at Moq.Mock`1.InitializeInstance()
   at Moq.Mock`1.OnGetObject()
   at Moq.Mock`1.get_Object()

您能推荐给我模拟ISerializable类的正确方法吗
(例如System.Reflection.Assembly)和Moq。

提前致谢!

最佳答案

问题不在于ISerializable接口(interface)。您可以模拟ISerializable类。

注意异常消息:



问题是,Assembly不提供反序列化构造函数。

关于.net - 如何用Moq模拟ISerializable类?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2696236/

10-11 07:31