标签: roslyn

如何使用Roslyn阅读XML文档注释

我希望能够在使用Roslyn解析C#源代码时阅读XML文档注释.

/// <summary>
/// Documentation...
/// </summary>
Run Code Online (Sandbox Code Playgroud)

我尝试在ParseOptions中设置ParseDocumentationComments,但它似乎没有效果?

var parseOptions = ParseOptions.Default.WithParseDocumentationComments(true);
SyntaxTree unit = SyntaxTree.ParseFile(file, parseOptions);
Run Code Online (Sandbox Code Playgroud)

c# roslyn

22
推荐指数
2
解决办法
5728
查看次数

使用Roslyn解析/转换/生成代码:我的目标是太高还是太低?

(我要做的是通过从vs生成的设置文件生成接口和包装类来解决Application.Settings/MVVM问题.)

我想做的是:

  • 从文件中解析类声明
  • 仅基于类的(非静态)属性生成接口声明
  • 生成一个实现此接口的包装类,在构造函数中获取原始类的实例,并将所有属性"管道"到实例.
  • 生成另一个直接实现接口的类.

我的问题是双重的:

  • 我吠叫错了树吗?我会更好地使用Code-Dom,T4,Regex(!)来实现这个目标,还是部分原因?(我不介意一些额外的工作,因为这主要是一种学习经历.)
  • 如果罗斯林是要走的路,我应该关注哪一点?我有点天真地希望有一些方法可以走树并吐出我想要的东西,但是我无法理解是否/如何使用SyntaxRewriter来实现它,或者是否要使用流畅的结构,多次查询源我需要的位.

如果你想评论MVVM方面,你可以,但这不是问题的主旨:)

c# code-generation roslyn

22
推荐指数
4
解决办法
9424
查看次数

Roslyn没有引用System.Runtime

我正在开发一个项目,我们正在使用Roslyn为我们编译一些模板.现在,当我正在编译模板时,我收到了多个错误CompileResult.Diagnostics.

错误是:

(21,6): error CS0012: The type 'System.Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(21,6): error CS0012: The type 'System.Type' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Run Code Online (Sandbox Code Playgroud)

看到这些错误,我认为我没有System.Runtime正确添加对程序集的引用.但是,在检查加载的组件后,这似乎是有序的.

private IEnumerable<MetadataReference> GetGlobalReferences()
{
    var assemblies = new [] 
        {
            typeof(System.Object).Assembly,                         //mscorlib
            typeof(System.Composition.ExportAttribute).Assembly,    //System.Composition (MEF)
            typeof(System.CodeDom.Compiler.CodeCompiler).Assembly,  //System.CodeDom.Compiler
        };

    var refs = …
Run Code Online (Sandbox Code Playgroud)

c# roslyn

22
推荐指数
3
解决办法
5888
查看次数

在TFS 2012上构建c#6.0

如何在TFS 2012上使用C#6.0功能构建Visual Studio 2015解决方案,而无需在构建代理上安装Visual Studio 2015(使用Microsoft Build Tools 2015 RC)

我已经安装了MSBuild工具,但我仍然有例外.如何告诉我的构建模板使用MSBuild 14(仅适用于一个项目)

为什么我的TFS 2012编译异步并等待(c#5.0)而BuildAgent只安装了Visual Studio 2012而没有任何问题?

我尝试将BuildProcessTemplate的ToolPath更改为MSBuild/14.0 /但是我遇到了构建错误:

C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.CSharp.targets(316):"csc2.exe"已退出,代码为-532462766.

安装Microsoft.Net.Compilers 1.0.0-rc2编译器会出现相同的错误.

如果我使用命令行编译项目,我会得到完全相同的错误/尽管在使用具有完全相同参数的命令行时我的开发机器上没有错误.

这是我在命令行中获得的异常:

C:\Program Files (x86)\MSBuild\14.0\bin\csc2.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentro.....
     Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program.
        at System.Collections.Immutable.SecurePooledObject`1.Use[TCaller](TCaller& caller)
        at System.Collections.Immutable.SortedInt32KeyNode`1.Enumerator.PushLeft(SortedInt32KeyNode`1 node)
        at System.Collections.Immutable.SortedInt32KeyNode`1.Enumerator..ctor(SortedInt32KeyNode`1 root)
        at System.Collections.Immutable.ImmutableDictionary`2.Enumerator..ctor(SortedInt32KeyNode`1 root, Builder builder)
        at Microsoft.CodeAnalysis.RuleSet.GetDiagnosticOptionsFromRulesetFile(Dictionary`2 diagnosticOptions, String resolvedPath, IList`1 diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        at Microsoft.CodeAnalysis.CSharp.CSharpCommandLineParser.Parse(IEnumerable`1 args, String baseDirectory, String additionalReferencePaths)
        at Microsoft.CodeAnalysis.CSharp.CSharpCommandLineParser.CommonParse(IEnumerable`1 args, String baseDirectory, String …
Run Code Online (Sandbox Code Playgroud)

c# msbuild tfs roslyn tfs2012

22
推荐指数
2
解决办法
7344
查看次数

使用Roslyn删除C#中的外来分号 - (替换w空琐事)

我已经想出了如何打开解决方案然后遍历项目然后迭代文档.我一直在寻找如何查找C#类,枚举,结构和接口,这些可能在声明结尾处有一个无关的分号(C++风格).我想删除它们并将.cs文件保存回磁盘.在我现在的公司写了大约25个解决方案,我会反对.注意:我们这样做的原因是为了推进更好的编码标准.(我想学习如何使用Roslyn进行这些'简单'调整)

示例(更新):

class Program
{
    static void Main(string[] args)
    {
        string solutionFile = @"S:\source\dotnet\SimpleApp\SimpleApp.sln";
        IWorkspace workspace = Workspace.LoadSolution(solutionFile);
        var proj = workspace.CurrentSolution.Projects.First();
        var doc = proj.Documents.First();
        var root = (CompilationUnitSyntax)doc.GetSyntaxRoot();
        var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
        foreach (var decl in classes)
        {
            ProcessClass(decl);
        }
        Console.ReadKey();

    }

    private static SyntaxNode ProcessClass(ClassDeclarationSyntax node)
    {
        ClassDeclarationSyntax newNode;
        if (node.HasTrailingTrivia)
        {
            foreach (var t in node.GetTrailingTrivia())
            {
                var es = new SyntaxTrivia();
                es.Kind = SyntaxKind.EmptyStatement;
                // kind is readonly - what is the right …
Run Code Online (Sandbox Code Playgroud)

.net c# roslyn

21
推荐指数
2
解决办法
2091
查看次数

如何编译C#字符串插值?

我知道插值是语法糖string.Format(),但它是否有任何特殊的行为/识别何时与字符串格式化方法一起使用?

如果我有一个方法:

void Print(string format, params object[] parameters)
Run Code Online (Sandbox Code Playgroud)

以下使用插值调用它:

Print($"{foo} {bar}");
Run Code Online (Sandbox Code Playgroud)

以下哪个调用行最相当于字符串插值的编译结果?

Print(string.Format("{0} {1}", new[] { foo, bar }));
Print("{0} {1}", new[] { foo, bar });
Run Code Online (Sandbox Code Playgroud)

问题背后的推理:诸如NLog之类的记录框架通常会推迟字符串格式化,直到确定将实际写入日志消息为止.一般来说,我更喜欢字符串插值语法,但我需要知道它是否会产生额外的性能损失.

c# string-formatting roslyn c#-6.0

21
推荐指数
1
解决办法
1821
查看次数

Roslyn编译器优化了函数调用乘法与零

昨天我在C#代码中发现了这种奇怪的行为:

Stack<long> s = new Stack<long>();

s.Push(1);           // stack contains [1]
s.Push(2);           // stack contains [1|2]
s.Push(3);           // stack contains [1|2|3]

s.Push(s.Pop() * 0); // stack should contain [1|2|0]

Console.WriteLine(string.Join("|", s.Reverse()));
Run Code Online (Sandbox Code Playgroud)

我假设程序会打印,1|2|0但实际上是打印的1|2|3|0.

查看生成的IL代码(通过ILSpy),您可以看到它s.Pop() * 0被优化为0:

// ...
IL_0022: ldloc.0
IL_0023: ldc.i4.0
IL_0024: conv.i8
IL_0025: callvirt instance void class   [System]System.Collections.Generic.Stack`1<int64>::Push(!0)
// ...
Run Code Online (Sandbox Code Playgroud)

ILSpy反编译:

Stack<long> s = new Stack<long>();
s.Push(1L);
s.Push(2L);
s.Push(3L);
s.Push(0L); // <- the offending line
Console.WriteLine(string.Join<long>("|", s.Reverse<long>()));
Run Code Online (Sandbox Code Playgroud)

首先,我在Windows 7下使用Visual Studio …

c# optimization roslyn visual-studio-2015

21
推荐指数
1
解决办法
590
查看次数

使用Roslyn - 编译器作为服务有什么好处

什么是关于Roslyn项目的CaaS(编译器即服务)?

使用Roslyn功能如何通过当前的C#4.0编译器提高C#应用程序的性能?

Roslyn-CTP中的已知限制/问题是什么?

compiler-as-a-service roslyn

20
推荐指数
3
解决办法
8981
查看次数

为什么以下代码编译没有错误?

我正在搞乱我的C#项目,我很惊讶看到这段代码编译:

var a = new Action<string>(ref b => b = "hello");
Run Code Online (Sandbox Code Playgroud)

反过来翻转,就像Action<string> a = ref b => b = "hello";在'ref'处给出语法错误一样.

果然,将片段复制粘贴到http://dotnetfiddle.net会产生编译错误:

A ref or out argument must be an assignable variable
Run Code Online (Sandbox Code Playgroud)

我在编译器中遇到过错误吗?(我正在使用VS 2015和C#6.)


编辑:刚刚切换到.NET Fiddle上的Roslyn编译器,它仍然会发出相同的错误.

c# delegates compiler-errors ref roslyn

20
推荐指数
1
解决办法
2614
查看次数

对.net核心项目使用<DebugType> Full </ DebugType>和<DebugType> Portable </ DebugType>有什么区别?

要生成打开封面报告,我必须将debugType设置为Full.我在构建服务器上生成报告,因为如果覆盖范围没有达到某个阈值,我必须使构建失败.构建在发布模式下生成.在我的csproj文件中保持debugType Full有什么后果?它会降低生产性能吗?

.net opencover roslyn .net-core

20
推荐指数
2
解决办法
8372
查看次数