Roslyn,CSharp编译详细信息

问题描述 投票:0回答:1

这是我的代码示例

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoslynTest
{ 
class Program
        {
            static void Main(string[] args)
            {
                myClass obj = new myClass();
                obj.Exec();
            }
        }
    
        public class myClass
        {
    
            private string _path;
            private readonly IEnumerable<MetadataReference> DefaultReferences =
                new[]
                {
    
                    MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\mscorlib.dll"),
                    MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll"),
                    MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Core.dll")
                };
    
            private readonly CSharpCompilationOptions DefaultCompilationOptions =
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                        .WithOverflowChecks(true).WithOptimizationLevel(OptimizationLevel.Release)
                        .WithUsings(new[]
                {
                    "System",
                    "System.Collections.Generic"
                });
    
            private SyntaxTree Parse(string text, string filename = "", CSharpParseOptions options = null)
            {
                var stringText = SourceText.From(text, Encoding.UTF8);
                return SyntaxFactory.ParseSyntaxTree(stringText, options, filename);
            }
    
            private string source = "public class Test1 { public int Amount {get; set;}   public void CalcAmo() { Amount = Amount + 500;} public void CalcAmo1() { Amount = Amount + 1000;  } } ";
    
            public void Exec()
            {
                var parsedSyntaxTree = Parse(source, "", CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6));
                var compilation = CSharpCompilation.Create(
                                    "Compilation",
                                    syntaxTrees: new SyntaxTree[] { parsedSyntaxTree },
                                    references: DefaultReferences,
                                    options: DefaultCompilationOptions);
                _path = "C:\\custom.dll";
                var result = compilation.Emit(_path);
                if (!result.Success)
                {
                    foreach (var item in result.Diagnostics)
                    {
                        Console.WriteLine(item.ToString());
                    }
                    }
                Console.ReadLine();
            }
        }
}

我通过输入字符串创建编译,然后从诊断中获取编译错误,但这还不够

我想了解有关编译错误的一切。

  1. 我的班级哪一行有错误? (课堂上排队)
  2. 哪个方法/属性有错误?
  3. 方法/属性的哪一行有错误? (方法中的线)
  4. 错误消息、警告...

在此代码中没有任何编译错误,但如果有,我会获取有关错误的详细信息,例如,如果我从输入字符串中删除一些“;”,我将得到“;预期” ,但这还不够。我会得到详细信息,例如这个错误在哪一行?

有可能吗?请帮助我。

提前致谢

c# roslyn
1个回答
0
投票

我在 Diagnostic.Location 属性中找到了一些方法

(GetLineSpan().StartLinePosition,  GetLineSpan().EndLinePosition)
这对我有帮助

© www.soinside.com 2019 - 2024. All rights reserved.