CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.

Home Page:https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Specified Property Name Overload to ObservableProperty Attribute

blueshift777 opened this issue · comments

commented

This is my proposed pull-request submission: Update the ObservableProperty attribute by adding an overload that allows the user to specify the property name that is generated from the private member variable.

Change to ObservablePropertyAttribute.cs:

    /// <summary>
    /// Constructor with specified property name
    /// </summary>
    /// <param name="specifiedPropertyName">Specified auto-generated property name</param>
    public ObservablePropertyAttribute(string specifiedPropertyName)
    {
        SpecifiedPropertyName = specifiedPropertyName;
    }

    /// <summary>
    /// Specified auto-generated property name
    /// </summary>
    public string SpecifiedPropertyName { get; private set; } = string.Empty;

Example of changes to ObservablePropertyGenerator.Execute.cs:

            string propertyName = fieldSymbol.Name;
            if (fieldSymbol?.GetAttributes().Any() == true)
            {
                ImmutableArray<AttributeData> attributeDataSet = fieldSymbol.GetAttributes();
                AttributeData? firstAttributeData = attributeDataSet.FirstOrDefault();
                if (firstAttributeData != null)
                {
                    ImmutableArray<TypedConstant> constructorArguments = firstAttributeData.ConstructorArguments;
                    if (constructorArguments.Any() == true)
                    {
                        TypedConstant? firstConstructorArgument = constructorArguments.FirstOrDefault();
                        if (firstConstructorArgument.HasValue)
                        {
                            string specifiedPropertyName = firstConstructorArgument.Value.Value!.ToString();
                            return specifiedPropertyName;
                        }
                    }
                }
            }

            string? propertyName = fieldSymbol?.Name;

Hello blueshift777, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.