Beispiel #1
0
        internal string GenerateRewritingVisitor()
        {
            ClassDeclarationSyntax visitorClassDeclaration =
                SyntaxFactory.ClassDeclaration(_className)
                .AddModifiers(
                    SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                    SyntaxFactory.Token(SyntaxKind.AbstractKeyword))
                .AddMembers(
                    GenerateVisitMethod(),
                    GenerateVisitActualMethod(),
                    GenerateVisitNullCheckedMethod())
                .AddMembers(
                    GenerateVisitClassMethods());

            var usings = new HashSet <string> {
                "System", "System.Collections.Generic", "System.Linq"
            };

            string summaryComment = string.Format(
                CultureInfo.CurrentCulture,
                Resources.RewritingVisitorSummary,
                _schemaName);

            return(visitorClassDeclaration.Format(
                       _copyrightNotice,
                       usings,
                       _namespaceName,
                       summaryComment));
        }
        /// <summary>
        /// Generates a class that implements <see cref="System.Collections.Generic.IEqualityComparer{T}"/>
        /// for the specified class.
        /// </summary>
        /// <param name="className">
        /// The name of the class whose equality comparer class is to be generated.
        /// </param>
        /// <param name="propertyInfoDictionary">
        /// An object containing information about each property in the class specified by <paramref name="className"/>.
        /// </param>
        /// <returns>
        /// A string containing the text of the generated equality comparer class.
        /// </returns>
        internal string Generate(string className, PropertyInfoDictionary propertyInfoDictionary)
        {
            _className = className;
            _propertyInfoDictionary = propertyInfoDictionary;

            _classType = SyntaxFactory.ParseTypeName(_className);
            _localVariableNameGenerator.Reset();

            string comparerClassName = GetEqualityComparerClassName(_className);

            var comparerInterface = GetComparerBaseType(_className);

            ClassDeclarationSyntax classDeclaration =
                SyntaxFactory.ClassDeclaration(comparerClassName)
                .AddModifiers(
                    SyntaxFactory.Token(SyntaxKind.InternalKeyword),
                    SyntaxFactory.Token(SyntaxKind.SealedKeyword))
                .AddBaseListTypes(comparerInterface)
                .AddMembers(
                    GenerateInstanceProperty(),
                    GenerateEqualsMethod(),
                    GenerateGetHashCodeMethod());

            var usings = new HashSet <string>
            {
                "System",                       // For Object.
                "System.Collections.Generic"    // For IEqualityComparer<T>
            };

            IEnumerable <string> namespaceNames = _propertyInfoDictionary
                                                  .Values
                                                  .Select(propertyInfo => propertyInfo.NamespaceName)
                                                  .Where(namespaceName => !string.IsNullOrWhiteSpace(namespaceName));

            foreach (string namespaceName in namespaceNames)
            {
                usings.Add(namespaceName);
            }

            return(classDeclaration.Format(
                       _copyrightNotice,
                       usings,
                       _namespaceName,
                       MakeSummaryComment()));
        }