Question

I am working on a c# codedom project which provides users to dynamically compile the c# code. I am getting error when adding assembly dll of wpf (it is working fine for winforms). It is saying that "Can not find #### in assembly. Are you missing some reference" when I try to add the reference like "System.Windows.Media". But when I am adding the reference by its dll path like "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Printing.dll" then it is saying that "File C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Printing.dll not found" but when I place the System.Printing.dll to the application executable folder, it is working fine.

Following is the code I am using to add the reference to compiler option:

CompilerParameters oParameters;
:
:
:
string lcAssemblyDll="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Printing.dll";
oParameters.ReferencedAssemblies.Add(lcAssemblyDll);

I am not able to understand the problem. Also is there any other approach to add the wpf assemblies?

Thanks

Was it helpful?

Solution

Well, I can't provide a thorough answer off the top of my head, but first of all you need to realize that a compilation reference is not the same as being able to resolve an assembly during application execution. So if that error was given during application execution I can imagine it throwing typeloader exceptions.

Second you are probably getting "Can not find #### in assembly. Are you missing some reference", because base types of classes you are dependent upon reside in assemblies to which the System.Windows.Media is referring.

You could try to solve this adding references to assemblies that get loaded during Assembly.ReflectionOnlyLoad of the assemblies you want referred. If you add an event handler for AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve, you can add code that makes sure those assemblies may be loaded and then simply add references to the CodeCompileUnit.

I have run into one problem with this though and that was that some references get optimized away during compilation, so I implemented a nasty hack that simply adds a container class to the CodeCompileUnit which initializies the first constructable type found in each of those assemblies.

Hope that helps a bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top