Skip to main content
 首页 » 编程设计

c#之为动态编译的程序集加载符号

2024年02月05日14artech

我正在为我的应用程序添加脚本功能。 Init 方法加载脚本,编译它并调用脚本 Init 方法。我希望能够将 Visual Studio 调试器附加到我的应用程序,并在 IDE 中以通常的方式在脚本中添加断点。

Visual Studio 说:加载“i0uu5bcn.vhy”。无法找到或打开 PDB 文件。

断点可以像这样使用:System.Diagnostics.Debugger.Break();

public static string[] Init(string scriptPath, params object[] parameters) 
        { 
            List<string> errors = new List<string>(); 
 
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(scriptPath), null, scriptPath); 
 
            SyntaxTree encoded = CSharpSyntaxTree.Create(syntaxTree.GetRoot() as CSharpSyntaxNode, null, scriptPath, System.Text.Encoding.UTF8); 
 
            string assemblyName = Path.GetRandomFileName(); 
 
            MetadataReference[] references = new MetadataReference[] 
            { 
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location), 
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location) 
            }; 
 
            CSharpCompilation compilation = CSharpCompilation.Create( 
                assemblyName, 
                syntaxTrees: new[] { encoded }, 
                references: references, 
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); 
 
            Assembly finalAssembly = null; 
 
            using (var symbols = new MemoryStream()) 
            { 
                using (var ms = new MemoryStream()) 
                { 
                    EmitResult result = compilation.Emit(ms, symbols); 
 
                    if (!result.Success) 
                    { 
                        IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic => 
                            diagnostic.IsWarningAsError || 
                            diagnostic.Severity == DiagnosticSeverity.Error); 
 
                        foreach (Diagnostic diagnostic in failures) 
                        { 
                            errors.Add($"{diagnostic.Id}: {diagnostic.GetMessage()}"); 
                        } 
                    } 
                    else 
                    { 
                        ms.Seek(0, SeekOrigin.Begin); 
                        symbols.Seek(0, SeekOrigin.Begin); 
 
                        finalAssembly = Assembly.Load(ms.ToArray(), symbols.ToArray()); 
 
                        Type type = finalAssembly.DefinedTypes.First().AsType(); 
                        object obj = Activator.CreateInstance(type); 
                        type.InvokeMember("Init", 
                            BindingFlags.Default | BindingFlags.InvokeMethod, 
                            null, 
                            obj, 
                            parameters); 
                    } 
                } 
            } 
 
            return errors.ToArray(); 
        } 

请您参考如下方法:

再次确保该文件实际上是 UTF8 编码的。编码用于获取生成 PDB 时使用的散列函数的输入字节。请注意,Encoding.UTF8 假定文件具有 BOM .

您可以使用 SourceLink 从生成的 PDB 中转储校验和(先将其保存到磁盘...) :

sourcelink checksums -p <pdbfile> 

您可以将转储中脚本文件的值与您在代码中计算的源文件的哈希值进行比较。除非这两者不匹配,否则 Visual Studio 的调试器将无法正确获取脚本文件。

另请注意,如果您使用 SourceText.From(Stream, Encoding),它会尝试 detect the correct encoding for you .

因此,如果我的预感是正确的,像这样获取 SourceTree 应该也可以解决您的问题:

SyntaxTree encoded; 
using(var stream = File.OpenRead(scriptPath)) 
{ 
     SourceText text = SourceText.From(stream, Encoding.UTF8); 
     encoded = CSharpSyntaxTree.ParseText(text, null, scriptPath); 
}