C# CompilerParameters.ReferencedAssemblies -- 添加对 System.Web.UI.WebControls 的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/793610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CompilerParameters.ReferencedAssemblies -- Add reference to System.Web.UI.WebControls
提问by cllpse
I am compiling classes at run-time using the CodeDomProvider
class. This works fine for classes only using the System
namespace:
我正在使用类在运行时编译CodeDomProvider
类。这适用于仅使用System
命名空间的类:
using System;
public class Test
{
public String HelloWorld()
{
return "Hello World!";
}
}
If I try to compile a class using System.Web.UI.WebControls
though, I get this error:
如果我尝试使用System.Web.UI.WebControls
虽然编译一个类,我会收到此错误:
{error CS0006: Metadata file 'System.Web.UI.WebControls' could not be found} System.CodeDom.Compiler.CompilerError
{错误 CS0006:找不到元数据文件“System.Web.UI.WebControls”} System.CodeDom.Compiler.CompilerError
Here's a snippet of my code:
这是我的代码片段:
var cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.Web.UI.WebControls");
How do I reference the System.Web.UI.WebControls
namespace?
如何引用System.Web.UI.WebControls
命名空间?
采纳答案by Tim Robinson
You reference assemblies, not namespaces. You should use MSDN to look up the name of the assembly that contains the classes you need to use: in this case it's going to be:
您引用程序集,而不是命名空间。您应该使用 MSDN 查找包含您需要使用的类的程序集的名称:在这种情况下,它将是:
var cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.Web.dll");
回答by Dan Nuffer
You can loop through all the currently loaded assemblies:
您可以遍历所有当前加载的程序集:
var assemblies = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => a.Location);
cp.ReferencedAssemblies.AddRange(assemblies.ToArray());
回答by jwize
This proved to be a little less brute force in my case. I was building an addin and there were 730 assemblies loaded in the current domain so there was major lag involved.
在我的情况下,这被证明不是蛮力。我正在构建一个插件,当前域中加载了 730 个程序集,因此存在重大延迟。
var assemblies = someType.Assembly.GetReferencedAssemblies().ToList();
var assemblyLocations =
assemblies.Select(a =>
Assembly.ReflectionOnlyLoad(a.FullName).Location).ToList();
assemblyLocations.Add(someType.Assembly.Location);
cp.ReferencedAssemblies.AddRange(assemblyLocations.ToArray());