4

I am trying to use Roslyn to get the reference to the symbol (FieldSymbol) for backing field of the property, but AssociatedPropertyOrEvent is null. Here is the unit test that fails.

[Test]
public void Get_BackingField_for_Property()
{

    var sourceCode = @" public class Player
                    {
                        private Person _MyPerson;

                        public virtual Person MyPerson
                        {
                            get { return _MyPerson; }
                        }

                        public virtual void Set(Person person)
                        {
                            _MyPerson = person;
                        }
                    }";


    var syntaxTree = SyntaxTree.ParseText(sourceCode);


    var mscorlib = MetadataReference.CreateAssemblyReference(
                                     "mscorlib");


    var compilation = Compilation.Create("HelloWorld")
                    .AddReferences(mscorlib)
                    .AddSyntaxTrees(syntaxTree);

    //var  semanticModel = compilation.GetSemanticModel(syntaxTree);

    var classSymbol =
        compilation.GetTypeByMetadataName("Player");

    Assert.That(classSymbol,Is.Not.Null, "class");

    var propertySymbol = classSymbol.GetMembers().Where(x => x.Kind == SymbolKind.Property);

    Assert.That(propertySymbol, Is.Not.Null, "property");

    var backingField = classSymbol.GetMembers().Where(x=>x.Kind== SymbolKind.Field).Cast<FieldSymbol>().First();

    Assert.That(backingField.AssociatedPropertyOrEvent, Is.Not.Null,"backing field");

Update: For anybody trying to do the same INotifyPropertyChaged sample has code that gets backing field of the property. I modified it to fit my needs. Here is the code

internal static IFieldSymbol GetBackingField(this IPropertySymbol property, ISemanticModel semanticModel)
{
    var propertyDelcarationSyntax = (PropertyDeclarationSyntax)property.DeclaringSyntaxNodes.First();

    var getter = propertyDelcarationSyntax.AccessorList.Accessors.First(a => a.Kind == SyntaxKind.GetAccessorDeclaration);

    return GetBackingFieldFromGetter(getter, semanticModel);
}
private static IFieldSymbol GetBackingFieldFromGetter(AccessorDeclarationSyntax getter, ISemanticModel semanticModel)
{
    // The getter should have a body containing a single return of a backing field.

    if (getter.Body == null)
    {
        throw new Exception("Missing a getter body for property " + semanticModel.GetDeclaredSymbol(getter.Parent).Name);
    }

    var statements = getter.Body.Statements;
    if (statements.Count != 1)
    {
        throw new Exception("Getter body has more then one statement for property " + semanticModel.GetDeclaredSymbol(getter.Parent).Name);
    }

    var returnStatement = statements.Single() as ReturnStatementSyntax;
    if (returnStatement == null || returnStatement.Expression == null)
    {
        throw new Exception("Getter body is missing a return statement for property " + semanticModel.GetDeclaredSymbol(getter.Parent).Name);
    }

    return semanticModel.GetSymbolInfo(returnStatement.Expression).Symbol as IFieldSymbol;
}
4

2 回答 2

4

的文档AssociatedPropertyOrEvent说:

如果此字段用作自动生成的属性或类似字段的事件的支持变量,则返回该属性/事件。否则返回 null。

由于您的字段与任何自动属性无关,因此AssociatedPropertyOrEvent正确返回null.

通常,您要问的是不可能的,因为可能有许多属性可以访问同一字段。

于 2013-10-28T19:21:57.683 回答
0

您只能使用当前的 API 采取另一种方式。使用GetMembers获取所有这些,然后找到AssociatedPropertyOrEvent您想要的属性的字段。

于 2013-10-28T19:21:30.277 回答