添加对位于共享驱动器上的 .dll 的引用

Add reference to .dll located on shared drive

当引用位于共享驱动器上的 NET 动态 link 库时,出现 Python.NET 异常 FileNotFoundException。示例代码:

import sys
import clr

sys.path.append(PATH_TO_SHARED_NETWORK_DRIVE)
clr.AddReference('MY_DLL')

经过一番谷歌搜索后,我发现真正的问题是 .NET Framework 默认情况下不启用 CAS 策略。因此,要在共享驱动器上引用 dll,需要启用 loadFromRemoteSources 开关。示例代码:

from clr import System 
from System import Reflection 

Reflection.Assembly.LoadFrom(FULL_PATH_TO_MY_DLL.dll)

异常:

System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

问题是我不知道如何从 Python 启用 loadFromRemoteSources 开关。

显然设置此设置的唯一方法是通过 app.config 使用您提供的 link 中的内容:

<configuration>  
   <runtime>  
      <loadFromRemoteSources enabled="true"/>  
   </runtime>  
</configuration>  

首先,尝试在与 python.exe 相同的文件夹中创建一个包含上述内容的 python.exe.config 文件。如果这仍然无济于事,那么 python.net 的用处就会减少,因为那时您必须创建一个实际的 .NET 库并将其放在本地,以引用该远程 dll 并从那里代理您的调用。我觉得这个应该可以,但是我没试过。

你确定你真的需要引用远程主机上的 dll 吗?也许您最好重新定义问题?