CompilerParameters.ReferencedAssemblies — Add reference to System.Web.UI.WebControls

左心房为你撑大大i 提交于 2019-11-30 19:35:32

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:

var cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.Web.dll");
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());

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.

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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!