Example #1
0
        /// <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);
        }
Example #2
0
 /// <summary>
 /// Gets a string representation of this object using the specified format string.
 /// </summary>
 /// <param name="format">Code element format string.</param>
 /// <returns>String representation.</returns>
 public virtual string ToString(string format)
 {
     return(ElementUtilities.Format(format, this));
 }