Roslyn,CSharpCompilation详细信息

时间:2017-05-16 12:05:50

标签: c# roslyn

这是我的代码示例

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. 错误消息,警告.... .......
  5. 在此代码中没有任何编译错误,但如果有,我将获得有关错误的详细信息,例如,如果我将删除一些&#39; ; &#39 ;从输入字符串我将得到&#34; ;预期&#34; ,但它没有实现。我会得到详细信息,例如哪一​​行出现此错误?

    它有可能吗?请帮帮我。

    感谢advacie

1 个答案:

答案 0 :(得分:0)

我在Diagnostic.Location属性(GetLineSpan().StartLinePosition, GetLineSpan().EndLinePosition)中找到了一些帮助我的方法