IPropertySymbolExtensions.cs 3.5 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2

3 4
#nullable enable

5
using System;
P
Pilchie 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeGeneration;

namespace Microsoft.CodeAnalysis.Shared.Extensions
{
    internal static class IPropertySymbolExtensions
    {
        public static IPropertySymbol RenameParameters(this IPropertySymbol property, IList<string> parameterNames)
        {
            var parameterList = property.Parameters;
            if (parameterList.Select(p => p.Name).SequenceEqual(parameterNames))
            {
                return property;
            }

            var parameters = parameterList.RenameParameters(parameterNames);

            return CodeGenerationSymbolFactory.CreatePropertySymbol(
                property.ContainingType,
                property.GetAttributes(),
                property.DeclaredAccessibility,
                property.GetSymbolModifiers(),
                property.Type,
31
                property.RefKind,
32
                property.ExplicitInterfaceImplementations,
P
Pilchie 已提交
33 34 35 36 37 38
                property.Name,
                parameters,
                property.GetMethod,
                property.SetMethod,
                property.IsIndexer);
        }
39

40 41
        public static IPropertySymbol RemoveInaccessibleAttributesAndAttributesOfTypes(
            this IPropertySymbol property, ISymbol accessibleWithin, params INamedTypeSymbol[] attributesToRemove)
42
        {
C
CyrusNajmabadi 已提交
43
            bool shouldRemoveAttribute(AttributeData a) =>
44
                attributesToRemove.Any(attr => attr.Equals(a.AttributeClass)) || !a.AttributeClass.IsAccessibleWithin(accessibleWithin);
45

46
            var someParameterHasAttribute = property.Parameters
47
                .Any(p => p.GetAttributes().Any(shouldRemoveAttribute));
48 49 50 51 52 53 54 55 56 57 58
            if (!someParameterHasAttribute)
            {
                return property;
            }

            return CodeGenerationSymbolFactory.CreatePropertySymbol(
                property.ContainingType,
                property.GetAttributes(),
                property.DeclaredAccessibility,
                property.GetSymbolModifiers(),
                property.Type,
59
                property.RefKind,
60
                property.ExplicitInterfaceImplementations,
61
                property.Name,
C
CyrusNajmabadi 已提交
62
                property.Parameters.SelectAsArray(p =>
63
                    CodeGenerationSymbolFactory.CreateParameterSymbol(
C
CyrusNajmabadi 已提交
64
                        p.GetAttributes().WhereAsArray(a => !shouldRemoveAttribute(a)),
65
                        p.RefKind, p.IsParams, p.GetTypeWithAnnotatedNullability(), p.Name, p.IsOptional,
C
CyrusNajmabadi 已提交
66
                        p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)),
67 68 69 70
                property.GetMethod,
                property.SetMethod,
                property.IsIndexer);
        }
J
Jonathon Marolf 已提交
71 72

        public static bool IsWritableInConstructor(this IPropertySymbol property)
J
Jonathon Marolf 已提交
73
            => (property.SetMethod != null || ContainsBackingField(property));
J
Jonathon Marolf 已提交
74

75
        public static IFieldSymbol? GetBackingFieldIfAny(this IPropertySymbol property)
J
Jonathon Marolf 已提交
76 77 78
            => property.ContainingType.GetMembers()
                .OfType<IFieldSymbol>()
                .FirstOrDefault(f => property.Equals(f.AssociatedSymbol));
J
Jonathon Marolf 已提交
79

J
Jonathon Marolf 已提交
80 81
        private static bool ContainsBackingField(IPropertySymbol property)
            => property.GetBackingFieldIfAny() != null;
P
Pilchie 已提交
82
    }
83
}