2017-05-16 43 views
0

這是我的代碼示例羅斯林,CSharpCompilation詳細信息

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. 錯誤消息,警告.... .......

在此代碼有沒有任何編譯錯誤,但如果它會有我會得到有關錯誤的詳細信息,例如,如果我將刪除一些';'from input string i will get「; expected」,但它不夠。我會得到詳細信息,例如哪一​​行出現此錯誤?

它是可行的嗎?請幫幫我。

感謝在advacie

+0

現在我沒有得到任何錯誤,但我希望得到錯誤的細節info4mation如果將存在,這是我如何編譯,在實際項目中,我將有很多方法和屬性的大類,它將編譯運行時,並且如果出現錯誤,我需要獲取錯誤詳細信息 – Vecchiasignora

+0

我想從錯誤中獲取錯誤運行時編譯通過字符串,嘗試/ catch?)))它是運行時編譯,我想要編譯錯誤) – Vecchiasignora

+2

你看看在項目中的屬性? – SLaks

回答

0

我發現在Diagnostic.Location財產(GetLineSpan().StartLinePosition, GetLineSpan().EndLinePosition)一些方法已經幫助我