首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么在使用NET40-NoCastle时会出现'Moq.Proxy.CastleProxyFactory‘类型的初始化器异常?

为什么在使用NET40-NoCastle时会出现'Moq.Proxy.CastleProxyFactory‘类型的初始化器异常?
EN

Stack Overflow用户
提问于 2010-10-28 21:11:02
回答 1查看 7.2K关注 0票数 17

因此,我从Moq主页上几乎逐字复制了sample code,并得到了一个城堡代理异常。

以下是我的代码(作为一个简单示例的控制台应用程序)

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;

namespace MoqTestConsole
{
    public interface ILoveThisFramework
    {
        bool DownloadExists(string s);
    }

    class Program
    {
        static void Main(string[] args)
        {
            Mock<ILoveThisFramework> mock = new Mock<ILoveThisFramework>();

            // WOW! No record/replay weirdness?! :)
            mock.Setup(framework => framework.DownloadExists("2.0.0.0")).Returns(true);

            // Hand mock.Object as a collaborator and exercise it, 
            // like calling methods on it...
            ILoveThisFramework lovable = mock.Object;
            bool download = lovable.DownloadExists("2.0.0.0");

            // Verify that the given method was indeed called with the expected value
            mock.Verify(framework => framework.DownloadExists("2.0.0.0"));
        }
    }
}

一切都可以很好地编译,但是当它调用mock.Object时会抛出以下异常。

代码语言:javascript
复制
System.TypeInitializationException was unhandled
  Message=The type initializer for 'Moq.Mock`1' threw an exception.
  Source=Moq
  TypeName=Moq.Mock`1
  StackTrace:
       at Moq.Mock`1.<InitializeInstance>b__0() in d:\Code\moq\src\Source\Mock.Generic.cs:line 138
       at Moq.PexProtector.Invoke(Action action) in d:\Code\moq\src\Source\PexProtector.cs:line 56
       at Moq.Mock`1.InitializeInstance() in d:\Code\moq\src\Source\Mock.Generic.cs:line 136
       at Moq.Mock`1.OnGetObject() in d:\Code\moq\src\Source\Mock.Generic.cs:line 153
       at Moq.Mock.GetObject() in d:\Code\moq\src\Source\Mock.cs:line 152
       at Moq.Mock.get_Object() in d:\Code\moq\src\Source\Mock.cs:line 147
       at Moq.Mock`1.get_Object() in d:\Code\moq\src\Source\Mock.Generic.cs:line 131
       at MoqTestConsole.Program.Main(String[] args) in C:\Projects\Test\MoqTestConsole\MoqTestConsole\Program.cs:line 25
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.TypeInitializationException
       Message=The type initializer for 'Moq.Proxy.CastleProxyFactory' threw an exception.
       Source=Moq
       TypeName=Moq.Proxy.CastleProxyFactory
       StackTrace:
            at Moq.Proxy.CastleProxyFactory..ctor()
            at Moq.Mock`1..cctor() in d:\Code\moq\src\Source\Mock.Generic.cs:line 54
       InnerException: System.IO.FileNotFoundException
            Message=Could not load file or assembly 'Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The system cannot find the file specified.
            Source=Moq
            FileName=Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc
            FusionLog==== Pre-bind state information ===
LOG: User = jsi-dev-001\jmacintyre
LOG: DisplayName = Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc
 (Fully-specified)
LOG: Appbase = file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core.DLL.
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core/Castle.Core.DLL.
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core.EXE.
LOG: Attempting download of new URL file:///C:/Projects/Test/MoqTestConsole/MoqTestConsole/bin/Debug/Castle.Core/Castle.Core.EXE.

            StackTrace:
                 at Moq.Proxy.CastleProxyFactory..cctor()
            InnerException: 

所以它看起来像是一个缺少的Castle代理组件,但我引用的是NET40-NoCastle目录中的二进制文件。

最新版本的Moq (Moq.4.0.10827)

而且我是Moq的新手,所以我可能正在做一些非常密集的事情。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4043230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档