gpt4 book ai didi

c# - Roslyn - 使用 CSharpCompilation 编译程序集以在 CSharpCompilation 编译的另一个程序中使用

转载 作者:行者123 更新时间:2023-12-04 09:37:07 25 4
gpt4 key购买 nike

我正在尝试使用 CSharpCompilation 编译程序集其中有一个简单的类,然后我可以在另一个也通过 CSharpCompilation 编译的程序中引用它.我有这个代码:
命名空间编译测试
{
类节目
{
静态无效主要(字符串 [] 参数)
{
HashSet 引用的Assemblies = new HashSet ()
{
typeof(object).Assembly,
Assembly.Load(new AssemblyName("Microsoft.CSharp")),
Assembly.Load(new AssemblyName("netstandard")),
Assembly.Load(new AssemblyName("System.Runtime")),
Assembly.Load(new AssemblyName("System.Linq")),
Assembly.Load(new AssemblyName("System.Linq.Expressions"))
};

字符串问候语 = @"
命名空间 TestLibraryAssembly
{
公共(public)静态类问候语
{
公共(public)静态字符串 GetGreeting(字符串名称)
{
返回“”你好,“”+名称+“”!“”;
}
}
}
";

CSharpCompilation 编译1 = CSharpCompilation.Create(
"TestLibraryAssembly",
新的 []
{
CSharpSyntaxTree.ParseText(greetingClass)
},
referencedAssemblies.Select(assembly => MetadataReference.CreateFromFile(assembly.Location)).ToList(),
新 CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
MemoryStream memoryStream1 = new MemoryStream();
EmitResult emitResult1 = 编译1.Emit(memoryStream1);
memoryStream1.Position = 0;
MetadataReference testLibraryReference = MetadataReference.CreateFromStream(memoryStream1);

字符串程序代码 = @"
使用 TestLibraryAssembly;

命名空间测试程序
{
公开类(class)序
{
公共(public)无效主要()
{
字符串问候 = Greeting.GetGreeting(""Name"");
}
}
}
";

CSharpCompilation 编译2 = CSharpCompilation.Create(
"测试程序",
新的 []
{
CSharpSyntaxTree.ParseText(programCode)
},
引用的程序集
.Select(assembly => MetadataReference.CreateFromFile(assembly.Location))
.Concat(new List { testLibraryReference }).ToList(),
新 CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
MemoryStream memoryStream2 = new MemoryStream();
EmitResult emitResult2 = 编译2.Emit(memoryStream2);
memoryStream2.Position = 0;

汇编程序Assembly = Assembly.Load(memoryStream2.ToArray());
类型 programType = programAssembly.GetType("TestProgram.Program");
MethodInfo 方法 = programType.GetMethod("Main");
对象实例 = Activator.CreateInstance(programType);
方法。调用(实例,空);
}
}
}

但是,当我运行它时,我收到此错误:

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

---> System.IO.FileNotFoundException: Could not load file or assembly 'TestLibraryAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

File name: 'TestLibraryAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'


我该如何解决这个错误?

最佳答案

您的示例中的问题是激活器( Activator.CreateInstance(programType); )如何尝试解决依赖关系。它将在磁盘上查找文件。
解决此问题的一种方法是将文件保存在磁盘上,然后使用 Activator.CreateInstanceFrom创建一个实例。
您可以在此处找到执行此操作的代码段:

using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Playground
{
public static class Program
{
private const string firstClass =
@"
namespace A
{
public class Foo
{
public int Bar() => 21;
}
}";

private const string secondClass =
@"using A;

namespace B
{
public class Test
{
public int GetValue() => new Foo().Bar();
}
}
";

public static void Main()
{
var firstAssemblyFileName = Path.Combine(Path.GetTempPath(), "A.dll");
var secondAssemblyFileName = Path.Combine(Path.GetTempPath(), "B.dll");

var compilation = CreateCompilation(CSharpSyntaxTree.ParseText(firstClass), "A");
var secondCompilation = CreateCompilation(CSharpSyntaxTree.ParseText(secondClass), "B")
.AddReferences(compilation.ToMetadataReference());

compilation.Emit(firstAssemblyFileName);
secondCompilation.Emit(secondAssemblyFileName);

dynamic testType = Activator.CreateInstanceFrom(secondAssemblyFileName, "B.Test").Unwrap();
var value = testType.GetValue();
}

private static CSharpCompilation CreateCompilation(SyntaxTree tree, string name) =>
CSharpCompilation
.Create(name, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddReferences(MetadataReference.CreateFromFile(typeof(string).Assembly.Location))
.AddSyntaxTrees(tree);
}
}

关于c# - Roslyn - 使用 CSharpCompilation 编译程序集以在 CSharpCompilation 编译的另一个程序中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62523946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com