我想使用Roslyn来分析Razor View中一块C#代码的上下文中的语义信息.
有没有办法(在Visual Studio 2015中,甚至在单元测试中)获得SemanticModel代表此代码的代码?
使用RazorTemplateEngine.GenerateCode和从CSharpCodeProvider.GenerateCodeFromCompileUnit(或者VBCodeProvider如果您希望中间源为VB.NET)从Razor视图文件中提取代表视图的代码.然后,您可以使用Roslyn来解析代码.
有使用罗斯林与Razor视图文件的例子在这里.
请注意,GenerateCode有一点需要注意:
此类型/成员支持.NET Framework基础结构,不应在代码中直接使用.
Razor文件包含一个C#投影缓冲区,其中包含生成的C#代码(包括您自己不编写的部分).这个缓冲区具有完整的Roslyn服务,正是您所需要的.
你需要遍历TextView的BufferGraph并找到CSharp缓冲区; 然后你可以得到它的Document语义模型.
如果您从光标位置开始,则只需将该位置映射到CSharp缓冲区即可.
请注意,TextView包含多个 CSharp缓冲区是完全合法的.(虽然Razor编辑器永远不会这样做)
如果您不在TextView中工作,则需要自己完成所有这些工作; 你需要通过Razor编译器运行Razor源来获取生成的C#源,然后用Roslyn编译它以获得语义模型.
以防万一其他人陷入困境,我有一个迷你示例应用程序可能会有所帮助。
我有一个这样的 CMS 类:
public partial class CMS
{
public static string SomeKey
{
get { return (string) ResourceProvider.GetResource("some_key"); }
}
// ... and many more ...
}
Run Code Online (Sandbox Code Playgroud)
...我想找出在我的报告解决方案中使用了哪些...输入 Roslyn!
以下应用程序将打印出使用和未使用引用的计数:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Razor;
namespace TranslationSniffer
{
class Program
{
static void Main(string[] args)
{
new Program().Go().Wait();
}
public async Task Go()
{
// Roslyn!
var ws = MSBuildWorkspace.Create();
// Store the translation keys...
List<string> used = new List<string>();
List<string> delete = new List<string>();
string solutionRoot = @"C:\_Code\PathToProject\";
string sln = solutionRoot + "MySolution.sln";
// Load the solution, and find all the cshtml Razor views...
var solution = await ws.OpenSolutionAsync(sln);
var mainProj = solution.Projects.Where(x => x.Name == "ConsumerWeb").Single();
FileInfo[] cshtmls = new DirectoryInfo(solutionRoot).GetFiles("*.cshtml", SearchOption.AllDirectories);
// Go through each Razor View - generate the equivalent CS and add to the project for compilation.
var host = new RazorEngineHost(RazorCodeLanguage.Languages["cshtml"]);
var razor = new RazorTemplateEngine(host);
var cs = new CSharpCodeProvider();
var csOptions = new CodeGeneratorOptions();
foreach (var cshtml in cshtmls)
{
using (StreamReader re = new StreamReader(cshtml.FullName))
{
try
{
// Let Razor do it's thang...
var compileUnit = razor.GenerateCode(re).GeneratedCode;
// Pull the code into a stringbuilder, and append to the main project:
StringBuilder sb = new StringBuilder();
using (StringWriter rw = new StringWriter(sb))
{
cs.GenerateCodeFromCompileUnit(compileUnit, rw, csOptions);
}
// Get the new immutable project
var doc = mainProj.AddDocument(cshtml.Name + ".cs", sb.ToString());
mainProj = doc.Project;
}
catch(Exception ex)
{
Console.WriteLine("Compile fail for: {0}", cshtml.Name);
// throw;
}
continue;
}
}
// We now have a new immutable solution, as we have changed the project instance...
solution = mainProj.Solution;
// Pull out our application translation list (its in a static class called 'CMS'):
var mainCompile = await mainProj.GetCompilationAsync();
var mainModel = mainCompile.GetTypeByMetadataName("Resources.CMS");
var translations = mainModel.GetMembers().Where(x => x.Kind == SymbolKind.Property).ToList();
foreach (var translation in translations)
{
var references = await SymbolFinder.FindReferencesAsync(translation, solution) ;
if (!references.First().Locations.Any())
{
Console.WriteLine("{0} translation is not used!", translation.Name);
delete.Add(translation.Name);
}
else
{
Console.WriteLine("{0} :in: {1}", translation.Name, references.First().Locations.First().Document.Name);
used.Add(translation.Name);
}
}
Console.WriteLine();
Console.WriteLine("Used references {0}. Unused references: {1}", used.Count, delete.Count);
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)