static void ConvertCustomAttributes(AstNode attributedNode, ICustomAttributeProvider customAttributeProvider, AttributeTarget target = AttributeTarget.None) { if (customAttributeProvider.HasCustomAttributes) { var attributes = new List<ICSharpCode.NRefactory.CSharp.Attribute>(); foreach (var customAttribute in customAttributeProvider.CustomAttributes) { var attribute = new ICSharpCode.NRefactory.CSharp.Attribute(); attribute.AddAnnotation(customAttribute); attribute.Type = ConvertType(customAttribute.AttributeType); attributes.Add(attribute); SimpleType st = attribute.Type as SimpleType; if (st != null && st.Identifier.EndsWith("Attribute", StringComparison.Ordinal)) { st.Identifier = st.Identifier.Substring(0, st.Identifier.Length - "Attribute".Length); } if(customAttribute.HasConstructorArguments) { foreach (var parameter in customAttribute.ConstructorArguments) { Expression parameterValue = ConvertArgumentValue(parameter); attribute.Arguments.Add(parameterValue); } } if (customAttribute.HasProperties) { TypeDefinition resolvedAttributeType = customAttribute.AttributeType.Resolve(); foreach (var propertyNamedArg in customAttribute.Properties) { var propertyReference = resolvedAttributeType != null ? resolvedAttributeType.Properties.FirstOrDefault(pr => pr.Name == propertyNamedArg.Name) : null; var propertyName = new IdentifierExpression(propertyNamedArg.Name).WithAnnotation(propertyReference); var argumentValue = ConvertArgumentValue(propertyNamedArg.Argument); attribute.Arguments.Add(new AssignmentExpression(propertyName, argumentValue)); } } if (customAttribute.HasFields) { TypeDefinition resolvedAttributeType = customAttribute.AttributeType.Resolve(); foreach (var fieldNamedArg in customAttribute.Fields) { var fieldReference = resolvedAttributeType != null ? resolvedAttributeType.Fields.FirstOrDefault(f => f.Name == fieldNamedArg.Name) : null; var fieldName = new IdentifierExpression(fieldNamedArg.Name).WithAnnotation(fieldReference); var argumentValue = ConvertArgumentValue(fieldNamedArg.Argument); attribute.Arguments.Add(new AssignmentExpression(fieldName, argumentValue)); } } } if (target == AttributeTarget.Module || target == AttributeTarget.Assembly) { // use separate section for each attribute foreach (var attribute in attributes) { var section = new AttributeSection(); section.AttributeTarget = target; section.Attributes.Add(attribute); attributedNode.AddChild(section, AttributedNode.AttributeRole); } } else { // use single section for all attributes var section = new AttributeSection(); section.AttributeTarget = target; section.Attributes.AddRange(attributes); attributedNode.AddChild(section, AttributedNode.AttributeRole); } } }
static void ConvertCustomAttributes(AstNode attributedNode, ICustomAttributeProvider customAttributeProvider, string attributeTarget = null) { if (customAttributeProvider.HasCustomAttributes) { var attributes = new List<ICSharpCode.NRefactory.CSharp.Attribute>(); foreach (var customAttribute in customAttributeProvider.CustomAttributes) { if (customAttribute.AttributeType.Name == "ExtensionAttribute" && customAttribute.AttributeType.Namespace == "System.Runtime.CompilerServices") { // don't show the ExtensionAttribute (it's converted to the 'this' modifier) continue; } if (customAttribute.AttributeType.Name == "ParamArrayAttribute" && customAttribute.AttributeType.Namespace == "System") { // don't show the ParamArrayAttribute (it's converted to the 'params' modifier) continue; } var attribute = new ICSharpCode.NRefactory.CSharp.Attribute(); attribute.AddAnnotation(customAttribute); attribute.Type = ConvertType(customAttribute.AttributeType); attributes.Add(attribute); SimpleType st = attribute.Type as SimpleType; if (st != null && st.Identifier.EndsWith("Attribute", StringComparison.Ordinal)) { st.Identifier = st.Identifier.Substring(0, st.Identifier.Length - "Attribute".Length); } if(customAttribute.HasConstructorArguments) { foreach (var parameter in customAttribute.ConstructorArguments) { Expression parameterValue = ConvertArgumentValue(parameter); attribute.Arguments.Add(parameterValue); } } if (customAttribute.HasProperties) { TypeDefinition resolvedAttributeType = customAttribute.AttributeType.Resolve(); foreach (var propertyNamedArg in customAttribute.Properties) { var propertyReference = resolvedAttributeType != null ? resolvedAttributeType.Properties.FirstOrDefault(pr => pr.Name == propertyNamedArg.Name) : null; var propertyName = new IdentifierExpression(propertyNamedArg.Name).WithAnnotation(propertyReference); var argumentValue = ConvertArgumentValue(propertyNamedArg.Argument); attribute.Arguments.Add(new AssignmentExpression(propertyName, argumentValue)); } } if (customAttribute.HasFields) { TypeDefinition resolvedAttributeType = customAttribute.AttributeType.Resolve(); foreach (var fieldNamedArg in customAttribute.Fields) { var fieldReference = resolvedAttributeType != null ? resolvedAttributeType.Fields.FirstOrDefault(f => f.Name == fieldNamedArg.Name) : null; var fieldName = new IdentifierExpression(fieldNamedArg.Name).WithAnnotation(fieldReference); var argumentValue = ConvertArgumentValue(fieldNamedArg.Argument); attribute.Arguments.Add(new AssignmentExpression(fieldName, argumentValue)); } } } if (attributeTarget == "module" || attributeTarget == "assembly") { // use separate section for each attribute foreach (var attribute in attributes) { var section = new AttributeSection(); section.AttributeTarget = attributeTarget; section.Attributes.Add(attribute); attributedNode.AddChild(section, AttributedNode.AttributeRole); } } else if (attributes.Count > 0) { // use single section for all attributes var section = new AttributeSection(); section.AttributeTarget = attributeTarget; section.Attributes.AddRange(attributes); attributedNode.AddChild(section, AttributedNode.AttributeRole); } } }