ISanych ISanych - 1 month ago 5x
C# Question

Roslyn Explicit IInstanceReferenceExpression Kind instead of This

When I'm getting IInstanceReferenceExpression operation for

type Instance() => this;
instance kind is
, but I expect that kind would be
. Am I missing something or this is a bug in Roslyn?

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Semantics;
using System;
using System.Linq;

internal static class so39495857
private static void Main()
var tree = CSharpSyntaxTree.ParseText(@"
class c
c Instance() => this;
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);
var node = tree.GetRoot().DescendantNodes().OfType<ArrowExpressionClauseSyntax>().First();
var operation = model.GetOperation(node);
var block = (IBlockStatement)operation;
var @return = (IReturnStatement)block.Statements.First();
var @this = (IInstanceReferenceExpression)@return.ReturnedValue;

Project on github -


That's what Explicit means.

ThisClass is only for VB's MyClass keyword.