Assembly::GetExecutingAssembly()->位置现在抛出 ArgumentException:路径中的非法字符

Assembly::GetExecutingAssembly()->Location now throws ArgumentException: Illegal characters in path

在 Visual Studio 2010(和 .Net 4)的 node.js 本机插件中,我可以使用

System::Reflection::Assembly::GetExecutingAssembly()->Location

获取 运行 C++/CLI 程序集的路径,但是在 Visual Studio 2015 项目(和 .Net 4.6)的 node.js 插件中,我得到一个异常:

System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.Security.Permissions.FileIOPermission.CheckIllegalCharacters(
      String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(
      FileIOPermissionAccess access, AccessControlActions control,
      String[] pathListOrig, Boolean checkForDuplicates,
      Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(
      FileIOPermissionAccess access, String path)
   at System.Reflection.RuntimeAssembly.get_Location()

知道如何获取 运行 程序集的路径吗?

编辑 2018-01-25 Visual Studio 2017 仍然没有解决,这次尝试 .Net 4.5.2

虽然 Location 属性 抛出,CodeBase 仍然可以使用,return 类似于:

file://?/C:/.../my-addon.node

这可以解释非法字符:'?'

一个new Uri(Application.CodeBase).LocalPath将return

/?/C:/.../my-addon.node

因此 GetExecutingAssembly()->Location 的解决方法可能是:

String^ GetExecutingAssemblyLocation()
{
  try
  {
    return Path::GetDirectoryName(Assembly::GetExecutingAssembly()->Location);
  }
  catch (Exception^ e)
  {
    String^ codeBase = Assembly::GetExecutingAssembly()->CodeBase;
    if (codeBase->StartsWith("file://?/"))
    {
      Uri^ codeBaseUri = gcnew Uri("file://" + codeBase->Substring(8));
      return codeBaseUri->LocalPath;
    }
    else
      throw;
  }
}

这将 return

C:\...\my-addon.node