【问题标题】:C# Change using namespace reference dll path dynamically IN .NET LIBRARY PROJECTC# 在 .NET LIBRARY PROJECT 中动态更改使用命名空间引用 dll 路径
【发布时间】:2018-07-31 23:17:25
【问题描述】:

我有一个类库 .net 项目(没有配置,没有 exe 文件),我有这样的东西;

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Web.UI.WebControls;
using eBAControls; *<-- I want this dll to be loaded from a path in runtime.*

出于未来的原因,我必须创建copyLocal false,因此我需要能够在运行时引用 dll。与当前 dll 位于同一文件夹中的其他第 3 方 dll 不会导致任何问题,但 eBAControls dll 与当前 dll 不在同一文件夹中,因此会出现错误:Could not load file or assembly 'eBAControls。 ..

由于某些编码和性能原因,我也不能为此使用反射,因此需要将其添加到引用中...

我想要的就是

using eBAControls = @"somepath"; <- that is set dynamically for ex while one of my methods are being executed etc...

【问题讨论】:

  • 你别无选择,只能使用某种反射(动态程序集加载/类型解析/等)

标签: c# asp.net .net-assembly


【解决方案1】:

如果您在AppDomain.AssemblyResolve 上放置一个事件处理程序,您可以从任何您喜欢的地方加载程序集。见Resolving Assembly Loads

这是一篇包含要点的博文。关键是时机。您必须在引用程序集的第一个方法被 JITted 之前附加事件。 https://blogs.msdn.microsoft.com/dbrowne/2014/06/25/how-to-load-an-assembly-in-a-ssis-script-task-that-isnt-in-the-gac/

【讨论】:

  • 你能给我看一些示例代码吗?别忘了这是一个图书馆项目
  • 谢谢,我的课是静态的,我把它改成了正常的。现在可以了。
猜你喜欢
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多