Pregunta

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?

¿Fue útil?

Solución

  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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top