如何以编程方式向项目添加程序集引用? [英] How do I programmatically add an assembly reference to a project?

查看:59
本文介绍了如何以编程方式向项目添加程序集引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Roslyn 来解析解决方案文件,并以编程方式向解决方案中的每个项目添加自定义程序集引用.

I have been trying to use Roslyn to parse a solution file and programmatically add a custom assembly reference to each project in the solution.

我尝试使用以下代码片段来做同样的事情:

I tried using the following code snippet to do the same:

//The name of the DLL is customLib.dll
var reference = MetadataReference.CreateAssemblyReference("customLib");
project = project.AddMetadataReference(reference);

但是,它在创建 MetadataReference 时遇到 FileNotFoundException.

However, it encounters a FileNotFoundException while creating the MetadataReference.

所以,我的问题是:如何指定 Roslyn 需要检查指定 dll 的路径?

谢谢.

推荐答案

MetadataReference.CreateAssemblyReference 用于获取对 GAC 中程序集的引用.试试这个:

MetadataReference.CreateAssemblyReference is used to get references to assemblies in the GAC. Try this:

project = project.AddMetadataReference(new MetadataFileReference("<path>"));

这篇关于如何以编程方式向项目添加程序集引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆