This project is read-only.
1
Vote

Query pattern when Select is a delegate

description

The program below will print
System.Func<System.Func<C, C>, C>.Invoke(System.Func<C, C>)
and nowhere is it indicated that the invoked member is the field C.Select, and similar for all possible query pattern methods. I understand that this is esoteric and that there probably are more high-priority tasks for you to do, but I still feel obliged to point out when I found some information Roslyn does not supply. I don't know the best way to supply the information, the GetSymbol() call for the select node could return the IFieldInfo for the Select delegate, but this might break extensions that str not aware of this pitfall and assume that only methods are invoked.
class Program {
    static void Main(string[] args) {
        var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class C {
    public Func<Func<C, C>, C> Select;

    public C(int[] arr) {
        var e = from i in this select i;
    }
}");

        var compilation = CSharpCompilation.Create("Test", new[] { syntaxTree }, new[] { MetadataReference.CreateFromAssembly(typeof(object).Assembly), MetadataReference.CreateFromAssembly(typeof(Enumerable).Assembly) }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
        foreach (var d in compilation.GetDiagnostics())
            Console.WriteLine(d.GetMessage());

        var semanticModel = compilation.GetSemanticModel(syntaxTree);

        var root = syntaxTree.GetRoot();
        var expr = root.DescendantNodes().OfType<ConstructorDeclarationSyntax>().First().Body.Statements.OfType<LocalDeclarationStatementSyntax>().Single().Declaration.Variables[0].Initializer.Value;
        var qe = (QueryExpressionSyntax)expr;

        var infoSymbol = semanticModel.GetSymbolInfo(qe.Body.SelectOrGroup).Symbol;

        Console.WriteLine(infoSymbol);
    }
}

comments