我有一组互不相交的动态编写的类,我想单独编译(并修复编译错误)。然后我想把它们组合到同一个程序集中。因为它们已经被编译了,所以我不想简单地将它们的语法树传递给汇编编译(以避免两次编译)。我试图将它们编译成netmodules,然后使用这些netmodule作为对程序集的引用,但得到的程序集没有所需的类型。
下面是我的测试代码(非常类似于Roslyn的CompilationEmitTests.MultipleNetmodulesWithPrivateImplementationDetails())
public static class Program
{
public static void Main()
{
var s1 = @"public class A {internal object o1 = new { hello = 1, world = 2 }; public static string M1() { return ""Hello, "";}}";
var s2 = @"public class B : A{internal object o2 = new { hello = 1, world = 2 };public static string M2(){ return ""world!"";}}";
var s3 = @"public class Program{public static void Main(){ System.Console.Write(A.M1()); System.Console.WriteLine(B.M2());}}";
var comp1 = CreateCompilationWithMscorlib("a1", s1, compilerOptions: TestOptions.ReleaseModule);
var ref1 = comp1.EmitToImageReference();
var comp2 = CreateCompilationWithMscorlib("a2", s2, compilerOptions: TestOptions.ReleaseModule, references: new[] { ref1 });
var ref2 = comp2.EmitToImageReference();
var comp3 = CreateCompilationWithMscorlib("a3", s3, compilerOptions: TestOptions.ReleaseExe.WithModuleName("C"), references: new[] { ref1, ref2 });
var ref3 = comp3.EmitToImageReference();
IEnumerable<byte> result = comp3.EmitToArray();
Assembly assembly = Assembly.Load(result.ToArray());
Module module = assembly.GetModule("C");
Type prog = module.GetType("Program");
object instance = Activator.CreateInstance(prog);
MethodInfo method = prog.GetMethod("Main");
method.Invoke(null, null);
}
public static ImmutableArray<byte> ToImmutable(this MemoryStream stream)
{
return ImmutableArray.Create<byte>(stream.ToArray());
}
internal static ImmutableArray<byte> EmitToArray
(
this Compilation compilation,
EmitOptions options = null,
Stream pdbStream = null
)
{
var stream = new MemoryStream();
if (pdbStream == null && compilation.Options.OptimizationLevel == OptimizationLevel.Debug)
{
pdbStream = new MemoryStream();
}
var emitResult = compilation.Emit(
peStream: stream,
pdbStream: pdbStream,
xmlDocumentationStream: null,
win32Resources: null,
manifestResources: null,
options: options);
return stream.ToImmutable();
}
public static MetadataReference EmitToImageReference(
this Compilation comp
)
{
var image = comp.EmitToArray();
if (comp.Options.OutputKind == OutputKind.NetModule)
{
return ModuleMetadata.CreateFromImage(image).GetReference(display: comp.AssemblyName);
}
else
{
return AssemblyMetadata.CreateFromImage(image).GetReference(display: comp.AssemblyName);
}
}
private static CSharpCompilation CreateCompilationWithMscorlib(string assemblyName, string code, CSharpCompilationOptions compilerOptions = null, IEnumerable<MetadataReference> references = null)
{
SourceText sourceText = SourceText.From(code, Encoding.UTF8);
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(sourceText, null, "");
MetadataReference mscoreLibReference = AssemblyMetadata.CreateFromFile(typeof(string).Assembly.Location).GetReference();
IEnumerable<MetadataReference> allReferences = new MetadataReference[] { mscoreLibReference };
if (references != null)
{
allReferences = allReferences.Concat(references);
}
CSharpCompilation compilation = CSharpCompilation.Create
(
assemblyName,
new[] {syntaxTree},
options : compilerOptions,
references: allReferences
);
return compilation;
}
}Main()在静态方法Invoke()抛出异常,因为它找不到对A和B类的引用。
我做错了什么?
发布于 2016-05-15 22:45:49
这是用户0xd4d在Github上发布的相同问题的答案。我测试了它的工作情况:
必须先调用LoadModule,然后才能访问网络模块
assembly.LoadModule("a1.netmodule", a1_netmodule_bytes);
assembly.LoadModule("a2.netmodule", a2_netmodule_bytes);如果您已将它们全部保存到磁盘,则不需要执行此操作。
我仍然不明白他说如果我把它保存到磁盘,我就不需要这样做,这是什么意思。
https://stackoverflow.com/questions/37213781
复制相似问题