C# CompileAssemblyFromSource, add referenced assemblies it needs?

佐手、 提交于 2019-12-30 08:51:08

问题


I have the CompileAssemblyFromSource working for code that only references assemblies that my program (that compiles it) uses. It works beautifully.

However, if I need to compile code that has a "using blah;" statement, it won't be able to find blah and say it is missing a resource.

So if it is "System.Windows.Forms", and I say "compilerparams.ReferencedAssemblies.Add("System.Windows.Forms.dll")", it works.

So I can parse the code file and get all the "using" parameters, but how do I know that those are supposed to be "*.dll" adding, or they are namespaces elsewhere or whatnot? Is there a way for .NET to take in "System.Windows.Forms" and spit back out "System.Windows.Forms.dll" because that's what it needs and so on?


回答1:


  1. Read the article How the Runtime Locates Assemblies
  2. Before compiling the code load all the assemblies found in the locations mentioned in point 1
  3. Load all the types in all the assemblies and keep a dictionary of namespace, type pair
  4. When a using is encountered in the source try adding reference of assemblies from the dictionary in 3


来源:https://stackoverflow.com/questions/7605466/c-sharp-compileassemblyfromsource-add-referenced-assemblies-it-needs

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