roslyn生成html,c# - How to manipulate CSHTML with Roslyn - Stack Overflow

I'm trying to use Roslyn to do some mass refactoring on my code.

The idea is to remove a specific using and insert them directly in the code.

For example

using My.Awesome.Namespace;

...

var temp = MyType.Prop;

would become

var temp = My.Awesome.Namespace.MyType.Prop;

I already have a working solution for .cs files using MSBuildWorkspace to parse my solution, find the using reference and replace them in the file. But I can't find how to do the same on the cshtml files.

They do not appear in the Documents property of my project.

Any idea?

Here is the code I'm using to parse the solution

public void Process(string solutionPath, string projectName, string baseNamespace)

{

//Force import csharp projects

MSBuildLocator.RegisterDefaults();

var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOption

using (var msWorkspace = MSBuildWorkspace.Create())

{

var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;

var project = solution.Projects.FirstOrDefault(x => x.Name == projectName)

if (project == null)

throw new InvalidOperationException();

foreach (var document in project.Documents)

{

if (document.SourceCodeKind != SourceCodeKind.Regular)

continue;

Console.WriteLine("Fixing file " + document.Name);

// Remove using of baseNamespace from doc

var newDoc = RemoveUsing(document, baseNamespace);

solution = solution.WithDocumentSyntaxRoot(document.Id, newDoc);

}

msWorkspace.TryApplyChanges(solution);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值