c# - 如何从执行程序集引用库?

标签 c# .net-assembly

我有一个应用程序引用比方说:DLL 1 和 DLL 2。

在应用程序中,最终用户可以为某些自定义功能编写一些代码。

在我的应用程序中,我使用以下代码编译他的代码:

// User's code
string code = @" ** User's code ** ";

// Create the provider
CSharpCodeProvider provider = new CSharpCodeProvider();

// Create the parameters
CompilerParameters parameters = new CompilerParameters();

// Compile the user's code 
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);

在编译之前,我想添加一些已经在我的应用程序中使用的引用,即 DLL 1 和 DLL 2(为了让用户使用我的一些功能)。

我尝试使用 GetExecutingAssembly,但它返回了主应用程序程序集,而且我找不到如何获取 DLL 1 和 DLL 2。

Assembly.GetExecutingAssembly

我不知道 DLL 的路径,所以下面的指令不起作用:

parameters.ReferencedAssemblies.Add("C:\DLL1.dll");

请问有人知道怎么解决吗?

最佳答案

您可以从该程序集中定义的类型获取程序集位置。因此,在您的 CompilerParameters 中,您可以像这样引用它们:

new CompilerParameters
{
    ReferencedAssemblies =
    {
        typeof(MyDll1.Type1).Assembly.Location,
        typeof(MyDll2.Type2).Assembly.Location
        //, etc
    }
    //, etc
}

关于c# - 如何从执行程序集引用库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401130/

相关文章:

c# - 在 WPF 中模拟 Enter 键

c# - 在文档准备好之前隐藏元素?

c# - Assembly.GetTypes() 返回类型的顺序是什么?

Powershell 无法找到类型 [System.Windows.Forms.KeyEventHandler]

c# - 将单独的列表绑定(bind)到一个列表框中

c# - Monotouch.Dialog:如何从 RadioGroup 中预选一个元素?

c# - 如何确定调用方法和类名?

c# - 汇编版本所需的格式是什么?

c# - 操纵 WriteableBitmap 像素

c# - 限制某些函数被调用的替代方法?