-
Hi Roslyn team, Is the output of using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Operations;
using Xunit;
namespace Analyzers.Test
{
public class ReproTest
{
[Fact]
public void Test()
{
const string programText =
@"using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace HelloWorld
{
public class MyClass
{
public const string ErrorInvalidOption = ""Something"";
public static string SomeMethod()
{
return ErrorInvalidOption; // ""fieldReferenceOperation"" below
}
}
}
";
SyntaxTree tree = CSharpSyntaxTree.ParseText(programText);
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
CSharpCompilation compilation = CSharpCompilation.Create("HelloWorld")
.AddReferences(MetadataReference.CreateFromFile(typeof(string).Assembly.Location))
.AddSyntaxTrees(tree);
var methodDeclarations = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList();
SemanticModel model = compilation.GetSemanticModel(tree);
MethodDeclarationSyntax methodDeclaration = methodDeclarations[0];
// IMethodSymbol methodSymbol = model.GetDeclaredSymbol(methodDeclaration)!;
List<IOperation> descendants = model.GetOperation(methodDeclaration.Body!).Descendants().ToList();
IFieldReferenceOperation fieldReferenceOperation = (descendants[1] as IFieldReferenceOperation)!;
Debug.WriteLine(fieldReferenceOperation.Field.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)); // "ErrorInvalidOption"
Debug.WriteLine(fieldReferenceOperation.Field.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)); // "HelloWorld.MyClass.ErrorInvalidOption"
}
}
} Roslyn 3.8.0 EDIT: It looks to me that SymbolDisplayFormat.FullyQualifiedFormat is specified without setting Therefore my field is specified without a containing type because the IF statement here is not true: |
Beta Was this translation helpful? Give feedback.
Answered by
MartyIX
Jan 5, 2021
Replies: 1 comment
-
I have filed issue #50259. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
MartyIX
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have filed issue #50259.