How to add reference to a dynamic assembly for com

2019-05-19 00:44发布

问题:

In my AppDomain there are few dynamic assembly, when I try codeDom.CompileAssemblyFromSource to Compile another new assembly, I can't figure out a way to add those dynamic assemble to ReferencedAssemblies.

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
         compilerParameters.ReferencedAssemblies.Add(assembly.Location);
}

Failed, as dynamic assembly doesn't have Location.

Thanks in advance.

PS: I'm actually trying to use ASP.Net MVC 3's new Razor template engine in IronPython.

回答1:

Not test, try use assembly.FullName instead of assembly.Location.



回答2:

I was having similar issue and this blog post: http://geekswithblogs.net/gyoung/archive/2006/04/27/76533.aspx convinced me that there is no way to do this. However this is relatively old post and if there is something new in .net 4 which allow this would be great to know about it.

EDIT:

I can confirm that this is not possible and with .net 4. As CSharpCodeGenerator class is using csc.exe to compile your code it uses the following code to add the referenced assemblies as parameters to the compiler:

foreach (string current in options.ReferencedAssemblies)
{
    stringBuilder.Append("/R:");
    stringBuilder.Append("\"");
    stringBuilder.Append(current);
    stringBuilder.Append("\"");
    stringBuilder.Append(" ");
}

BTW: There are another posts in SO for the same problem:

Supply Assembly to CompilerParameters ReferencedAssemblies from memory and not disk?

In C#, how do you reference types from one in-memory assembly inside another?