Пример #1
0
        /* The following are the parts of the CodeDOM class hierarchy that may not have have type replacement methods written.
         *    o System.CodeDom.CodeAttributeArgument (CodeExpression)
         *    o System.CodeDom.CodeAttributeDeclaration
         *    o System.CodeDom.CodeNamespaceImportCollection --- System.Collections.IList, System.Collections.ICollection, System.Collections.IEnumerable
         *    o System.CodeDom.CodeObject
         + System.CodeDom.CodeComment
         + System.CodeDom.CodeCompileUnit
         # System.CodeDom.CodeSnippetCompileUnit
         + System.CodeDom.CodeNamespace
         + System.CodeDom.CodeNamespaceImport
         + System.CodeDom.CodeTypeMember
         # System.CodeDom.CodeMemberEvent
         # System.CodeDom.CodeMemberField
         # System.CodeDom.CodeMemberMethod
         * System.CodeDom.CodeConstructor
         * System.CodeDom.CodeEntryPointMethod
         * System.CodeDom.CodeTypeConstructor
         # System.CodeDom.CodeMemberProperty
         # System.CodeDom.CodeSnippetTypeMember
         # System.CodeDom.CodeTypeDeclaration
         * System.CodeDom.CodeTypeDelegate
         + System.CodeDom.CodeTypeReference
         +    o System.Collections.CollectionBase
         + System.CodeDom.CodeAttributeArgumentCollection
         + System.CodeDom.CodeAttributeDeclarationCollection
         + System.CodeDom.CodeCommentStatementCollection
         + System.CodeDom.CodeNamespaceCollection
         + System.CodeDom.CodeParameterDeclarationExpressionCollection
         + System.CodeDom.CodeTypeDeclarationCollection
         + System.CodeDom.CodeTypeMemberCollection
         */

        public void AddTemplateMembers(ModelClass modelClass, CodeTypeDeclaration classDeclaration)
        {
            if (TemplateCompileUnit != null)
            {
                CodeTypeMemberCollection methods    = new CodeTypeMemberCollection();
                CodeTypeMemberCollection properties = new CodeTypeMemberCollection();

                foreach (CodeTypeMember member in TemplateCompileUnit.Namespaces[0].Types[0].Members)
                {
                    if (member is CodeMemberMethod)
                    {
                        CodeMemberMethod method = ((CodeMemberMethod)member).Clone();


                        // If type is generic, replace all type parameters with the generated class name.
                        if (TemplateCompileUnit.Namespaces[0].Types[0].TypeParameters.Count > 0)
                        {
                            string typeParameter = TemplateCompileUnit.Namespaces[0].Types[0].TypeParameters[0].Name;

                            // If there's a generic parameter, we only need to add new if the parameter signature is the same.
                            if (modelClass.IsSubclass && !method.Parameters.ContainsType(typeParameter))
                            {
                                method.Attributes |= MemberAttributes.New;
                            }

                            method.ReplaceType(typeParameter, modelClass.Name);
                        }
                        else
                        {
                            // If there's no type argument, the signature will surely match and we need to add new or something similar.
                            if (modelClass.IsSubclass)
                            {
                                method.Attributes |= MemberAttributes.New;
                            }
                        }

                        methods.Add(method);
                    }
                    else if (member is CodeMemberProperty)
                    {
                        CodeMemberProperty property = ((CodeMemberProperty)member).Clone();

                        if (modelClass.IsSubclass)
                        {
                            property.Attributes |= MemberAttributes.New;
                        }

                        // If type is generic, replace all type parameters with the generated class name.
                        if (TemplateCompileUnit.Namespaces[0].Types[0].TypeParameters.Count > 0)
                        {
                            string typeParameter = TemplateCompileUnit.Namespaces[0].Types[0].TypeParameters[0].Name;

                            property.ReplaceType(typeParameter, modelClass.Name);
                        }

                        properties.Add(property);
                    }
                }

                AddRegionMarkers(methods, "Methods imported from Member Template File");
                classDeclaration.Members.AddRange(methods);

                AddRegionMarkers(properties, "Properties imported from Member Template File");
                classDeclaration.Members.AddRange(properties);
            }
        }