How can I get the backing Field for a property in a Action

So from the class "Test" (), I can call .GetPropertyDeclarations(). Is there a way from the IPropertyDeclaration to get the IFieldDeclaration for that property (in the example below: "_name" for Property "Name")?

Example class:

 
public class Test
    {
        private string _name;
 
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
    }
1 comment
Comment actions Permalink

You can use IPropertyBodyHelper.GetBackingField to return you back an IField, and then use GetDeclarations to get any IFieldDeclarations. This will only work for simple properties with backing fields - it's essentially looking for properties that can be converted to auto-properties.

0

Please sign in to leave a comment.