AppDomain.CurrentDomain.RelativeSearchPath在单元测试中为空

Ant*_*tov 3 c# unit-testing relative-path

我在单独的类库项目中有电子邮件模板(和电子邮件).我正在获取视图的路径(复制到ouput directory = true),如下所示:

var basePath = AppDomain.CurrentDomain.RelativeSearchPath;
var path = Path.Combine(basePath, "Views", viewname + ".cshtml");
Run Code Online (Sandbox Code Playgroud)

当我尝试在单元测试项目中运行此代码时,basePath为null.但它不应该为null!

谁能解释我 -

  1. 为什么basepath为null?
  2. 怎么解决?

Roh*_*ats 6

根据MSDN:

专用程序集部署在与应用程序相同的目录结构中.如果RelativeSearchPath属性指定的路径不在AppDomainSetup.ApplicationBase下,则会被忽略.

此属性返回使用AppDomainSetup.PrivateBinPath设置的值.

您应该首先设置AppDomainSetup.PrivateBinPath.