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?
- Read the article How the Runtime Locates Assemblies
- Before compiling the code load all the assemblies found in the locations mentioned in point 1
- Load all the types in all the assemblies and keep a dictionary of namespace, type pair
- 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