即使在部署后也从不同的文件夹引用 dll
Referencing dll's from a different folder even after deployment
如何引用应用程序文件夹外的 dll 并利用其方法和接口动态加载它?有办法吗?
注意:即使在部署应用程序后也必须从该路径进行引用。
我会尝试使用 Assembly.LoadFrom(string)
。此重载获取程序集的路径并允许您通过反射使用它。
你可以这样得到程序集:
var sampleAssembly = Assembly.LoadFrom("c:\Sample.Assembly.dll");
var method = sampleAssembly.GetTypes()[0].GetMethod("Method1");
然后使用 MethodInfo.Invoke()
调用该方法
有很多方法可以做到这一点。有关运行时如何定位程序集的详细说明,请参见 How runtime locates assemblies。
您可以通过 app.config
中的 codeBase
元素指定确切位置:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<codeBase version="2.0.0.0"
href="http://www.litwareinc.com/myAssembly.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
如果您的程序集位于应用程序的子文件夹中,那么您可以使用 probing
元素:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>
如果所有这些都失败了,您可以随时编写事件处理程序并将其附加到 AppDomain.AssemblyResolve to explicitly load assemblies. There is even example on SO。
如何引用应用程序文件夹外的 dll 并利用其方法和接口动态加载它?有办法吗?
注意:即使在部署应用程序后也必须从该路径进行引用。
我会尝试使用 Assembly.LoadFrom(string)
。此重载获取程序集的路径并允许您通过反射使用它。
你可以这样得到程序集:
var sampleAssembly = Assembly.LoadFrom("c:\Sample.Assembly.dll");
var method = sampleAssembly.GetTypes()[0].GetMethod("Method1");
然后使用 MethodInfo.Invoke()
有很多方法可以做到这一点。有关运行时如何定位程序集的详细说明,请参见 How runtime locates assemblies。
您可以通过 app.config
中的 codeBase
元素指定确切位置:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<codeBase version="2.0.0.0"
href="http://www.litwareinc.com/myAssembly.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
如果您的程序集位于应用程序的子文件夹中,那么您可以使用 probing
元素:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>
如果所有这些都失败了,您可以随时编写事件处理程序并将其附加到 AppDomain.AssemblyResolve to explicitly load assemblies. There is even example on SO。