2016-04-14 64 views
1

我正在構建一個控制檯應用程序,可以掃描MVC應用程序和簡單的asp.Net類(即.cs文件)。 我已經使用roslyn分析器爲簡單的C#代碼,它工作正常:)。 現在我想掃描剃鬚刀文件,但它沒有被roslyn編譯器掃描,因爲它與C#一樣。 Leme一個給你舉個例子....這是我的剃刀文件..如何從剃鬚刀文件(CsHtml)中提取C#代碼?

@using System.Text; 
@using Microsoft.ApplicationBlocks.Data; 
@using System.Configuration; 
@using System.Data; 
@using System.Data.SqlClient; 
    ViewBag.Title = "throwEx"; 
} 
<h2>throwEx</h2> 
<h3> 
    @{ 
     string name = ""; 
     string country = ""; 
     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 

     List<SqlParameter> parameters = new List<SqlParameter>(); 
     parameters.Add(new SqlParameter("@Name", name)); 
     parameters.Add(new SqlParameter("@Country", country)); 
     int customerId = Convert.ToInt32(SqlHelper.ExecuteScalar(constr, CommandType.Text, "query", parameters.ToArray())); 
    } 
</h3> 

在這裏,我想只掃描C#代碼..如何提取只有@ {*僅本部:)}。

+0

你得到什麼錯誤?您嘗試過哪些代碼進行「掃描」?你是否希望代碼通過目錄尋找並匹配文件類型,使用視圖作爲輸入參數....? – ironman

+0

不,我沒有得到任何具體的錯誤,事情是我的應用程序將掃描Web應用程序,並檢測任何違規行爲,如我希望開發人員不要使用內聯查詢,而是他們應該使用存儲過程..所以在這種情況下,如果他們已經寫了這個代碼它應該創建一個語音:)。它在正常的.cs頁面中工作正常,但在這裏它沒有檢測到這部分的代碼段,因爲它將它作爲簡單的文本文件:(....我只是想提取這個C#代碼並將其作爲銫文件的解析器:)但如何? :( –

+0

並感謝您的快速回復:) –

回答

0

您可能希望編寫一個控制檯應用程序,可以接受參數(文件名/其他)並使用流讀取器。 https://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      if (args.Length != 0) 
      { 
       string line = null; 
       string targetStart = "@{"; 
       string targetEnd = "}"; 
       string fileName = args[0]; 

       using (var reader = new StreamReader(fileName)) 
       { 
        line = reader.ReadLine(); 
        if (line.Contains(targetStart)) 
        { 
         while (reader.ReadLine() != targetEnd) 
         { 
          var readLine = reader.ReadLine(); 
          if (readLine != null) Console.WriteLine(readLine.ToString()); 
         } 
        } 
        Console.Read(); 
       } 
      } 

     } 
    } 
} 

**我測試了這個在.cshtml,它的工作

相關問題