IPropertySymbolExtensions.cs 3.4 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
using System;
P
Pilchie 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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,
29
                property.RefKind,
30
                property.ExplicitInterfaceImplementations,
P
Pilchie 已提交
31 32 33 34 35 36
                property.Name,
                parameters,
                property.GetMethod,
                property.SetMethod,
                property.IsIndexer);
        }
37 38

        public static IPropertySymbol RemoveAttributeFromParameters(
39
            this IPropertySymbol property, INamedTypeSymbol[] attributesToRemove)
40
        {
41
            if (attributesToRemove == null)
42 43 44 45
            {
                return property;
            }

C
CyrusNajmabadi 已提交
46
            bool shouldRemoveAttribute(AttributeData a) =>
47 48
                attributesToRemove.Where(attr => attr != null).Any(attr => attr.Equals(a.AttributeClass));

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

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

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

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

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