示例#1
0
        /// <summary>
        /// Generates the <see cref="CodeTypeDeclaration"/> that represents the <see cref="ClassType"/>.
        /// </summary>
        /// <returns>A new instance of a <see cref="CodeTypeDeclaration"/> if successful.-null- otherwise.</returns>
        public override CodeTypeDeclaration GenerateCodeDom(ICodeDOMTranslationOptions options)
        {
            if (options.BuildTrail != null)
            {
                options.BuildTrail.Push(this);
            }
            CodeTypeDeclaration result = base.GenerateCodeDom(options);

            if (result == null)
            {
                if (options.BuildTrail != null)
                {
                    options.BuildTrail.Pop();
                }
                return(result);
            }
            if (this.IsStatic)
            {
                result.TypeAttributes |= TypeAttributes.Abstract;// | TypeAttributes.Sealed;
                result.Attributes     |= MemberAttributes.Static | MemberAttributes.Final;
            }
            result.Attributes |= AccessLevelAttributes(this.AccessLevel);
            if (this.BaseType != null && (!(this.BaseType.Equals(typeof(object).GetTypeReference()))))
            {
                result.BaseTypes.Add(this.BaseType.GenerateCodeDom(options));
            }
            ITypeReference[] impls     = this.ImplementsList.ToArray();
            bool[]           duplicate = new bool[impls.Length];
            List <string>    names     = new List <string>();

            for (int i = 0; i < impls.Length; i++)
            {
                string currentName = impls[i].TypeInstance.GetTypeName(options);
                if (impls[i].TypeParameters.Count > 0 && impls[i].TypeInstance.IsGeneric)
                {
                    currentName += string.Format("`{0}", impls[i].TypeParameters.Count);
                }
                duplicate[i] = names.Contains(currentName);
                if (duplicate[i])
                {
                    duplicate[names.IndexOf(currentName)] = true;
                }
                names.Add(currentName);
            }
            bool autoResolve = options.AutoResolveReferences;

            for (int i = 0; i < impls.Length; i++)
            {
                ITypeReference typeRef = impls[i];
                if (duplicate[i])
                {
                    if (autoResolve)
                    {
                        options.AutoResolveReferences = false;
                    }
                }
                result.BaseTypes.Add(typeRef.GenerateCodeDom(options));
                if (duplicate[i] && autoResolve)
                {
                    options.AutoResolveReferences = autoResolve;
                }
            }
            result.IsClass = true;
            if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Class))
            {
                result.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, String.Format("Begin {0}", result.Name)));
                result.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
            }
            if (options.BuildTrail != null)
            {
                options.BuildTrail.Pop();
            }
            return(result);
        }
示例#2
0
            internal static void InsertPartialDeclarationTypes <TItem, TCodeDOM, TMembers>(string itemName, TItem item, TMembers membersCollection, ICodeDOMTranslationOptions options)
                where TItem :
            class,
            ISegmentableDeclarationTarget <TItem>,
            ITypeParent
                where TCodeDOM :
            CodeObject
                where TMembers :
            CollectionBase,
            IList
            {
                if (options.AllowPartials)
                {
                    IDictionary <AutoRegionAreas, int> startRanges  = new Dictionary <AutoRegionAreas, int>();
                    IDictionary <AutoRegionAreas, int> rangeLengths = new Dictionary <AutoRegionAreas, int>();

                    startRanges[AutoRegionAreas.Classes] = membersCollection.Count;
                    foreach (IClassType subTypeClass in item.Classes.Values)
                    {
                        if (subTypeClass.ParentTarget == item)
                        {
                            membersCollection.Add(subTypeClass.GenerateCodeDom(options));
                        }
                        foreach (IClassType partial in subTypeClass.Partials)
                        {
                            if (partial.ParentTarget == item)
                            {
                                membersCollection.Add(partial.GenerateCodeDom(options));
                            }
                        }
                    }
                    rangeLengths[AutoRegionAreas.Classes] = membersCollection.Count - startRanges[AutoRegionAreas.Classes];

                    startRanges[AutoRegionAreas.Delegates] = membersCollection.Count;
                    foreach (IDelegateType subTypeDelegate in item.Delegates.Values)
                    {
                        if (subTypeDelegate.ParentTarget == item)
                        {
                            membersCollection.Add(subTypeDelegate.GenerateCodeDom(options));
                        }
                    }
                    rangeLengths[AutoRegionAreas.Delegates] = membersCollection.Count - startRanges[AutoRegionAreas.Delegates];

                    startRanges[AutoRegionAreas.Enumerators] = membersCollection.Count;
                    foreach (IEnumeratorType subTypeDelegate in item.Enumerators.Values)
                    {
                        if (subTypeDelegate.ParentTarget == item)
                        {
                            membersCollection.Add(subTypeDelegate.GenerateCodeDom(options));
                        }
                    }
                    rangeLengths[AutoRegionAreas.Enumerators] = membersCollection.Count - startRanges[AutoRegionAreas.Enumerators];

                    startRanges[AutoRegionAreas.Interfaces] = membersCollection.Count;
                    foreach (IInterfaceType subTypeInterface in item.Interfaces.Values)
                    {
                        if (subTypeInterface.ParentTarget == item)
                        {
                            membersCollection.Add(subTypeInterface.GenerateCodeDom(options));
                        }
                        foreach (IInterfaceType partial in subTypeInterface.Partials)
                        {
                            if (partial.ParentTarget == item)
                            {
                                membersCollection.Add(partial.GenerateCodeDom(options));
                            }
                        }
                    }
                    rangeLengths[AutoRegionAreas.Interfaces] = membersCollection.Count - startRanges[AutoRegionAreas.Interfaces];

                    startRanges[AutoRegionAreas.Structures] = membersCollection.Count;
                    foreach (IStructType subTypeStruct in item.Structures.Values)
                    {
                        if (subTypeStruct.ParentTarget == item)
                        {
                            membersCollection.Add(subTypeStruct.GenerateCodeDom(options));
                        }
                        foreach (IStructType partial in subTypeStruct.Partials)
                        {
                            if (partial.ParentTarget == item)
                            {
                                membersCollection.Add(partial.GenerateCodeDom(options));
                            }
                        }
                    }
                    rangeLengths[AutoRegionAreas.Structures] = membersCollection.Count - startRanges[AutoRegionAreas.Structures];

                    if (options.AllowRegions)
                    {
                        if (options.AutoRegionsFor(AutoRegionAreas.NestedTypes) && membersCollection.Count > 0)
                        {
                            ((CodeTypeDeclaration)membersCollection[0]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_NestedTypes)));
                            ((CodeTypeDeclaration)membersCollection[membersCollection.Count - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Classes) && (rangeLengths[AutoRegionAreas.Classes] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Classes]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Classes)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Classes] + rangeLengths[AutoRegionAreas.Classes] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Delegates) && (rangeLengths[AutoRegionAreas.Delegates] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Delegates]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Delegates)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Delegates] + rangeLengths[AutoRegionAreas.Delegates] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Enumerators) && (rangeLengths[AutoRegionAreas.Enumerators] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Enumerators]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Enumerators)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Enumerators] + rangeLengths[AutoRegionAreas.Enumerators] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Interfaces) && (rangeLengths[AutoRegionAreas.Interfaces] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Interfaces]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Interfaces)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Interfaces] + rangeLengths[AutoRegionAreas.Interfaces] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Structures) && (rangeLengths[AutoRegionAreas.Structures] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Structures]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Structures)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Structures] + rangeLengths[AutoRegionAreas.Structures] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                    }
                }
                else if (item.IsRoot)
                {
                    //ToDo: Insert code to properly manage root members who have
                    //partial instances inserted in the type groups.
                    IDictionary <AutoRegionAreas, int> startRanges  = new Dictionary <AutoRegionAreas, int>();
                    IDictionary <AutoRegionAreas, int> rangeLengths = new Dictionary <AutoRegionAreas, int>();

                    startRanges[AutoRegionAreas.Classes] = membersCollection.Count;
                    foreach (CodeTypeDeclaration declClass in item.Classes.GenerateCodeDom(options))
                    {
                        membersCollection.Add(declClass);
                    }
                    rangeLengths[AutoRegionAreas.Classes] = membersCollection.Count - startRanges[AutoRegionAreas.Classes];

                    startRanges[AutoRegionAreas.Delegates] = membersCollection.Count;
                    foreach (CodeTypeDelegate declDelegate in item.Delegates.GenerateCodeDom(options))
                    {
                        membersCollection.Add(declDelegate);
                    }
                    rangeLengths[AutoRegionAreas.Delegates] = membersCollection.Count - startRanges[AutoRegionAreas.Delegates];

                    startRanges[AutoRegionAreas.Enumerators] = membersCollection.Count;
                    foreach (CodeTypeDeclaration declEnum in item.Enumerators.GenerateCodeDom(options))
                    {
                        membersCollection.Add(declEnum);
                    }
                    rangeLengths[AutoRegionAreas.Enumerators] = membersCollection.Count - startRanges[AutoRegionAreas.Enumerators];

                    startRanges[AutoRegionAreas.Interfaces] = membersCollection.Count;
                    foreach (CodeTypeDeclaration declInterface in item.Interfaces.GenerateCodeDom(options))
                    {
                        membersCollection.Add(declInterface);
                    }
                    rangeLengths[AutoRegionAreas.Interfaces] = membersCollection.Count - startRanges[AutoRegionAreas.Interfaces];

                    startRanges[AutoRegionAreas.Structures] = membersCollection.Count;
                    foreach (CodeTypeDeclaration declStruct in item.Structures.GenerateCodeDom(options))
                    {
                        membersCollection.Add(declStruct);
                    }
                    rangeLengths[AutoRegionAreas.Structures] = membersCollection.Count - startRanges[AutoRegionAreas.Structures];

                    //Check the nested type insert first.
                    if (options.AllowRegions)
                    {
                        if (options.AutoRegionsFor(AutoRegionAreas.NestedTypes) && membersCollection.Count > 0)
                        {
                            ((CodeTypeDeclaration)membersCollection[0]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_NestedTypes)));
                            ((CodeTypeDeclaration)membersCollection[membersCollection.Count - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Classes) && (rangeLengths[AutoRegionAreas.Classes] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Classes]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Classes)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Classes] + rangeLengths[AutoRegionAreas.Classes] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Delegates) && (rangeLengths[AutoRegionAreas.Delegates] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Delegates]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Delegates)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Delegates] + rangeLengths[AutoRegionAreas.Delegates] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Enumerators) && (rangeLengths[AutoRegionAreas.Enumerators] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Enumerators]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Enumerators)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Enumerators] + rangeLengths[AutoRegionAreas.Enumerators] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Interfaces) && (rangeLengths[AutoRegionAreas.Interfaces] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Interfaces]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Interfaces)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Interfaces] + rangeLengths[AutoRegionAreas.Interfaces] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                        if (options.AutoRegionsFor(AutoRegionAreas.Structures) && (rangeLengths[AutoRegionAreas.Structures] > 0))
                        {
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Structures]]).StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format(MaintenanceResources.AutoRegions_BasePattern, itemName, MaintenanceResources.AutoRegions_Structures)));
                            ((CodeTypeDeclaration)membersCollection[startRanges[AutoRegionAreas.Structures] + rangeLengths[AutoRegionAreas.Structures] - 1]).EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                        }
                    }
                }
            }
示例#3
0
        /// <summary>
        /// Generates the <see cref="CodeTypeDeclaration"/> that represents the <see cref="SegmentableParameteredMemberTypeParentType"/>.
        /// </summary>
        /// <returns>A new instance of a <see cref="CodeTypeDeclaration"/> if successful.-null- otherwise.</returns>
        public override TDom GenerateCodeDom(ICodeDOMTranslationOptions options)
        {
            TDom result = base.GenerateCodeDom(options);

            _OIL._Core.InsertPartialDeclarationTypes <TItem, TDom, CodeTypeMemberCollection>(result.Name, (TItem)(object)this, result.Members, options);
            if (options.AllowPartials)
            {
                ///*
                foreach (ISnippetMember subTypeSnippet in this.SnippetMembers.Values)
                {
                    if (subTypeSnippet.ParentTarget == this)
                    {
                        result.Members.Add(subTypeSnippet.GenerateCodeDom(options));
                    }
                }

                int regionMemberIndex = result.Members.Count;
                foreach (IConstructorMember memberConstructor in this.Constructors.Values)
                {
                    if (memberConstructor.ParentTarget == this)
                    {
                        result.Members.Add(memberConstructor.GenerateCodeDom(options));
                    }
                }
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Constructors) && this.Constructors.GetCountForTarget(this) > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Constructors", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
                regionMemberIndex = result.Members.Count;
                foreach (IFieldMember memberField in this.Fields.Values)
                {
                    if (memberField.ParentTarget == this)
                    {
                        result.Members.Add(memberField.GenerateCodeDom(options));
                    }
                }
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Fields) && this.Fields.GetCountForTarget(this) > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Data Members", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
                regionMemberIndex = result.Members.Count;
                foreach (IMethodMember memberMethod in this.Methods.Values)
                {
                    if (memberMethod.ParentTarget == this)
                    {
                        result.Members.Add(memberMethod.GenerateCodeDom(options));
                    }
                }
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Methods) && this.Methods.GetCountForTarget(this) > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Methods", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
                regionMemberIndex = result.Members.Count;
                foreach (IPropertyMember memberProperty in this.Properties.Values)
                {
                    if (memberProperty.ParentTarget == this)
                    {
                        result.Members.Add(memberProperty.GenerateCodeDom(options));
                    }
                }
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Properties) && this.Properties.GetCountForTarget(this) > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Properties", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
                //*/

                /*
                 * result.Members.AddRange(this.Classes.GenerateCodeDom(options));
                 * result.Members.AddRange(this.Structures.GenerateCodeDom(options));
                 * result.Members.AddRange(this.Methods.GenerateCodeDom(options));
                 * //*/
            }
            else if (this.IsRoot)
            {
                foreach (ISnippetMember subTypeSnippet in this.SnippetMembers.Values)
                {
                    if (subTypeSnippet.ParentTarget == this)
                    {
                        result.Members.Add(subTypeSnippet.GenerateCodeDom(options));
                    }
                }
                int regionMemberIndex = result.Members.Count;
                result.Members.AddRange(this.Constructors.GenerateCodeDom(options));
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Constructors) && this.Constructors.Count > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Constructors", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
                regionMemberIndex = result.Members.Count;
                result.Members.AddRange(this.Fields.GenerateCodeDom(options));
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Fields) && this.Fields.Count > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Data Members", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
                regionMemberIndex = result.Members.Count;
                result.Members.AddRange(this.Methods.GenerateCodeDom(options));
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Methods) && this.Methods.Count > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Methods", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
                regionMemberIndex = result.Members.Count;
                result.Members.AddRange(this.Properties.GenerateCodeDom(options));
                if (options.AllowRegions && options.AutoRegionsFor(AutoRegionAreas.Properties) && this.Properties.Count > 0)
                {
                    result.Members[regionMemberIndex].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, string.Format("{0} Properties", result.Name)));
                    result.Members[result.Members.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, ""));
                }
            }
            else
            {
                return(null);
            }

            return(result);
        }