PropertyDeclaration TransformAutomaticProperties(PropertyDeclaration property)
        {
            PropertyDefinition cecilProperty = context.TypeSystem.GetCecil(property.GetSymbol() as IProperty) as PropertyDefinition;

            if (cecilProperty == null || cecilProperty.GetMethod == null)
            {
                return(null);
            }
            if (!cecilProperty.GetMethod.IsCompilerGenerated() && (cecilProperty.SetMethod?.IsCompilerGenerated() == false))
            {
                return(null);
            }
            IField fieldInfo = null;
            Match  m         = automaticPropertyPattern.Match(property);

            if (m.Success)
            {
                fieldInfo = m.Get <AstNode>("fieldReference").Single().GetSymbol() as IField;
            }
            else
            {
                Match m2 = automaticReadonlyPropertyPattern.Match(property);
                if (m2.Success)
                {
                    fieldInfo = m2.Get <AstNode>("fieldReference").Single().GetSymbol() as IField;
                }
            }
            if (fieldInfo == null)
            {
                return(null);
            }
            FieldDefinition field = context.TypeSystem.GetCecil(fieldInfo) as FieldDefinition;

            if (field.IsCompilerGenerated() && field.DeclaringType == cecilProperty.DeclaringType)
            {
                RemoveCompilerGeneratedAttribute(property.Getter.Attributes);
                RemoveCompilerGeneratedAttribute(property.Setter.Attributes);
                property.Getter.Body = null;
                property.Setter.Body = null;
            }
            // Add C# 7.3 attributes on backing field:
            var attributes = fieldInfo.Attributes
                             .Where(a => !attributeTypesToRemoveFromAutoProperties.Any(t => t == a.AttributeType.FullName))
                             .Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();

            if (attributes.Length > 0)
            {
                var section = new AttributeSection {
                    AttributeTarget = "field"
                };
                section.Attributes.AddRange(attributes);
                property.Attributes.Add(section);
            }
            // Since the property instance is not changed, we can continue in the visitor as usual, so return null
            return(null);
        }
예제 #2
0
 public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
 {
     currentMember = propertyDeclaration.GetSymbol() as IMember;
     if (currentMember == null)
     {
         return;
     }
     SetContext();
     base.VisitPropertyDeclaration(propertyDeclaration);
     currentMember = null;
 }
        PropertyDeclaration TransformAutomaticProperties(PropertyDeclaration propertyDeclaration)
        {
            IProperty property = propertyDeclaration.GetSymbol() as IProperty;

            if (!property.CanGet || (!property.Getter.IsCompilerGenerated() && (property.Setter?.IsCompilerGenerated() == false)))
            {
                return(null);
            }
            IField field = null;
            Match  m     = automaticPropertyPattern.Match(propertyDeclaration);

            if (m.Success)
            {
                field = m.Get <AstNode>("fieldReference").Single().GetSymbol() as IField;
            }
            else
            {
                Match m2 = automaticReadonlyPropertyPattern.Match(propertyDeclaration);
                if (m2.Success)
                {
                    field = m2.Get <AstNode>("fieldReference").Single().GetSymbol() as IField;
                }
            }
            if (field == null)
            {
                return(null);
            }
            if (field.IsCompilerGenerated() && field.DeclaringTypeDefinition == property.DeclaringTypeDefinition)
            {
                RemoveCompilerGeneratedAttribute(propertyDeclaration.Getter.Attributes);
                RemoveCompilerGeneratedAttribute(propertyDeclaration.Setter.Attributes);
                propertyDeclaration.Getter.Body = null;
                propertyDeclaration.Setter.Body = null;

                // Add C# 7.3 attributes on backing field:
                var attributes = field.GetAttributes()
                                 .Where(a => !attributeTypesToRemoveFromAutoProperties.Contains(a.AttributeType.FullName))
                                 .Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();
                if (attributes.Length > 0)
                {
                    var section = new AttributeSection {
                        AttributeTarget = "field"
                    };
                    section.Attributes.AddRange(attributes);
                    propertyDeclaration.Attributes.Add(section);
                }
            }
            // Since the property instance is not changed, we can continue in the visitor as usual, so return null
            return(null);
        }
        PropertyDeclaration TransformAutomaticProperties(PropertyDeclaration property)
        {
            PropertyDefinition cecilProperty = context.TypeSystem.GetCecil(property.GetSymbol() as IProperty) as PropertyDefinition;

            if (cecilProperty == null || cecilProperty.GetMethod == null)
            {
                return(null);
            }
            if (!cecilProperty.GetMethod.IsCompilerGenerated() && (cecilProperty.SetMethod?.IsCompilerGenerated() == false))
            {
                return(null);
            }
            IField fieldInfo = null;
            Match  m         = automaticPropertyPattern.Match(property);

            if (m.Success)
            {
                fieldInfo = m.Get <AstNode>("fieldReference").Single().GetSymbol() as IField;
            }
            else
            {
                Match m2 = automaticReadonlyPropertyPattern.Match(property);
                if (m2.Success)
                {
                    fieldInfo = m2.Get <AstNode>("fieldReference").Single().GetSymbol() as IField;
                }
            }
            if (fieldInfo == null)
            {
                return(null);
            }
            FieldDefinition field = context.TypeSystem.GetCecil(fieldInfo) as FieldDefinition;

            if (field.IsCompilerGenerated() && field.DeclaringType == cecilProperty.DeclaringType)
            {
                RemoveCompilerGeneratedAttribute(property.Getter.Attributes);
                RemoveCompilerGeneratedAttribute(property.Setter.Attributes);
                property.Getter.Body = null;
                property.Setter.Body = null;
            }
            // Since the property instance is not changed, we can continue in the visitor as usual, so return null
            return(null);
        }