/// <summary> /// Gets the Attributes attribute. /// </summary> /// <param name="codeElement">The code element.</param> /// <returns>Attributes as a comma-separated list.</returns> private static string GetAttributesAttribute(ICodeElement codeElement) { StringBuilder attributesBuilder = new StringBuilder(); AttributedElement attributedElement = codeElement as AttributedElement; if (attributedElement != null) { for (int attributeIndex = 0; attributeIndex < attributedElement.Attributes.Count; attributeIndex++) { IAttributeElement attribute = attributedElement.Attributes[attributeIndex]; attributesBuilder.Append(attribute.Name); foreach (ICodeElement attributeChild in attribute.Children) { IAttributeElement childAttributeElement = attributeChild as IAttributeElement; if (childAttributeElement != null) { attributesBuilder.Append(", "); attributesBuilder.Append(childAttributeElement.Name); } } if (attributeIndex < attributedElement.Attributes.Count - 1) { attributesBuilder.Append(", "); } } } return(attributesBuilder.ToString()); }
/// <summary> /// Creates a clone of the instance and copies any state. /// </summary> /// <returns>A clone of the instance.</returns> protected override sealed CodeElement DoClone() { AttributedElement clone = DoAttributedClone(); // // Copy state // clone._access = _access; lock (_attributesLock) { for (int attributeIndex = 0; attributeIndex < Attributes.Count; attributeIndex++) { IAttributeElement attribute = Attributes[attributeIndex]; IAttributeElement attributeClone = attribute.Clone() as IAttributeElement; clone.AddAttribute(attributeClone); } } return(clone); }
/// <summary> /// Gets the string representation of a code element attribute. /// </summary> /// <param name="attributeType">Type of the attribute.</param> /// <param name="codeElement">The code element.</param> /// <returns>The code element attribute as text.</returns> public static string GetAttribute(ElementAttributeType attributeType, ICodeElement codeElement) { string attributeString = null; MemberElement memberElement; TypeElement typeElement; if (codeElement != null) { switch (attributeType) { case ElementAttributeType.Name: attributeString = codeElement.Name; break; case ElementAttributeType.Access: AttributedElement attributedElement = codeElement as AttributedElement; if (attributedElement != null) { attributeString = EnumUtilities.ToString(attributedElement.Access); } break; case ElementAttributeType.ElementType: attributeString = EnumUtilities.ToString(codeElement.ElementType); break; case ElementAttributeType.Type: attributeString = GetTypeAttribute(codeElement); break; case ElementAttributeType.Attributes: attributeString = GetAttributesAttribute(codeElement); break; case ElementAttributeType.Modifier: memberElement = codeElement as MemberElement; if (memberElement != null) { attributeString = EnumUtilities.ToString(memberElement.MemberModifiers); } else { typeElement = codeElement as TypeElement; if (typeElement != null) { attributeString = EnumUtilities.ToString(typeElement.TypeModifiers); } } break; default: attributeString = string.Empty; break; } } if (attributeString == null) { attributeString = string.Empty; } return(attributeString); }
/// <summary> /// Writes a collection of element attributes. /// </summary> /// <param name="element">The element.</param> private void WriteAttributes(AttributedElement element) { foreach (IAttributeElement attribute in element.Attributes) { attribute.Accept(this); } }
/// <summary> /// Creates a comparison delegate based on the configuration. /// </summary> /// <param name="compareAttribute">The compare attribute.</param> /// <returns> /// Comparision delegate for two code elements. /// </returns> public Comparison <ICodeElement> CreateComparison(ElementAttributeType compareAttribute) { Comparison <ICodeElement> comparison = delegate(ICodeElement x, ICodeElement y) { int compareValue = 0; if (x == null && y != null) { compareValue = -1; } else if (x != null && y == null) { compareValue = 1; } else { switch (compareAttribute) { case ElementAttributeType.Access: AttributedElement attributedX = x as AttributedElement; AttributedElement attributedY = y as AttributedElement; if (attributedX != null && attributedY != null) { compareValue = attributedX.Access.CompareTo(attributedY.Access); } break; case ElementAttributeType.Modifier: MemberElement memberX = x as MemberElement; MemberElement memberY = y as MemberElement; if (memberX != null && memberY != null) { compareValue = memberX.MemberModifiers.CompareTo(memberY.MemberModifiers); } break; case ElementAttributeType.ElementType: compareValue = x.ElementType.CompareTo(y.ElementType); break; default: if (compareAttribute == ElementAttributeType.Type && x is TypeElement && y is TypeElement) { compareValue = ((TypeElement)x).Type.CompareTo(((TypeElement)y).Type); } else if (compareAttribute == ElementAttributeType.Type && x is UsingElement && y is UsingElement) { compareValue = ((UsingElement)x).Type.CompareTo(((UsingElement)y).Type); } else { string attributeX = ElementUtilities.GetAttribute(compareAttribute, x); string attributeY = ElementUtilities.GetAttribute(compareAttribute, y); compareValue = StringComparer.OrdinalIgnoreCase.Compare(attributeX, attributeY); } break; } } return(compareValue); }; return(comparison); }