Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

c# 9.0 - Roslyn uses MemberAccessExpressionSyntax instead of QualifiedNameSyntax

In the following code, I'd expect that both references to System.Action type to be represented as a QualifiedNameSyntax but the second one is represented as a MemberAccessExpressionSyntax.

Is that correct? If so, why can't it be a QualifiedNameSyntax?

class Foo
{
    public void M(object o)
    {
        var t = typeof(System.Action); // 1
        switch(o)
        {
            case System.Action: // 2
                break;
        }
    }
}
question from:https://stackoverflow.com/questions/66047461/roslyn-uses-memberaccessexpressionsyntax-instead-of-qualifiednamesyntax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Generally you're only going to get a QualifiedNameSyntax in a Roslyn syntax tree where the only legal thing there is a qualified name; in those cases we're running a restricted parser that will only understand qualified names. Anything else we're running our generic expression parser which will spit out whatever expression is there, and we'll figure out what it actually is during binding. Because consider another case like:

SomeEnum e;
switch (e)
{
     case SomeEnum.Blue: Console.WriteLine("Blue!"); return;
}

In that case the SomeEnum.Blue is absolutely an access to a member. But we don't actually know what "SomeEnum" is until binding, so we just always go with MemberAccessExpression.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...