When operations were introduced at Roslyn, one of the goals was to provide reduced operations (I think it was in the design review video), which, as I understand it, should provide explicit operations for implicit compiler actions at higher levels. I see that the directory is located in Roslyn, but the classes are internal there. Can I now omit operations or not have a public API?
In the example below, the operation already removes some implicit parts - adding a return statement for the body of the expression and displaying a symbol for the overloaded statement. But pre and post increments differ only in appearance.
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Semantics; using System.Linq; namespace so39468373 { internal static class Program { private static void Main() { var tree = CSharpSyntaxTree.ParseText(@" public class c { public static c operator ++(co) { return o; } static c pre(co) => ++o; static c post(co) => o++; public static void Main() {} }"); var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); var compilation = CSharpCompilation.Create(null, new[] { tree }, new[] { mscorlib }); var model = compilation.GetSemanticModel(tree); foreach (var node in tree.GetRoot().DescendantNodes().OfType<ArrowExpressionClauseSyntax>()) { var operation = model.GetOperation(node); var block = (IBlockStatement)operation; var statement = (IReturnStatement)block.Statements.First(); var increment = (IIncrementExpression)statement.ReturnedValue;
Code on github - https://github.com/isanych/so-39468373
c # roslyn
ISanych Sep 13 '16 at 10:59 2016-09-13 10:59
source share