public ClassDeclaration AddClass(NamespaceDeclaration ns)
        {
            ClassDeclaration col = ns.AddClass(this.DictionaryName);

            // set base class as CollectionBase
            col.Parent = new TypeTypeDeclaration(typeof(DictionaryBase));

            // default constructor
            col.AddConstructor();

            // add indexer
            if (this.ItemGet || this.ItemSet)
            {
                IndexerDeclaration index = col.AddIndexer(
                    this.ValueType
                    );
                ParameterDeclaration pindex = index.Signature.Parameters.Add(KeyType, "key", false);

                // get body
                if (this.ItemGet)
                {
                    index.Get.Return(
                        (Expr.This.Prop("Dictionary").Item(Expr.Arg(pindex)).Cast(this.ValueType)
                        )
                        );
                }
                // set body
                if (this.ItemSet)
                {
                    index.Set.Add(
                        Stm.Assign(
                            Expr.This.Prop("Dictionary").Item(Expr.Arg(pindex)),
                            Expr.Value
                            )
                        );
                }
            }

            // add method
            if (this.Add)
            {
                MethodDeclaration    add    = col.AddMethod("Add");
                ParameterDeclaration pKey   = add.Signature.Parameters.Add(this.KeyType, "key", true);
                ParameterDeclaration pValue = add.Signature.Parameters.Add(this.ValueType, "value", true);
                add.Body.Add(
                    Expr.This.Prop("Dictionary").Method("Add").Invoke(pKey, pValue)
                    );
            }

            // contains method
            if (this.Contains)
            {
                MethodDeclaration contains = col.AddMethod("Contains");
                contains.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool));
                ParameterDeclaration pKey = contains.Signature.Parameters.Add(this.KeyType, "key", true);
                contains.Body.Return(
                    Expr.This.Prop("Dictionary").Method("Contains").Invoke(pKey)
                    );
            }

            // remove method
            if (this.Remove)
            {
                MethodDeclaration    remove = col.AddMethod("Remove");
                ParameterDeclaration pKey   = remove.Signature.Parameters.Add(this.KeyType, "key", true);

                remove.Body.Add(
                    Expr.This.Prop("Dictionary").Method("Remove").Invoke(pKey)
                    );
            }

            return(col);
        }
Esempio n. 2
0
        private void AddArrayField(ClassDeclaration c, FieldInfo f)
        {
            // create a collection
            ClassDeclaration col = c.AddClass(conformer.ToSingular(f.Name) + "Collection");

            col.Parent = new TypeTypeDeclaration(typeof(System.Collections.CollectionBase));

            // add serializable attribute
            col.CustomAttributes.Add(typeof(SerializableAttribute));

            // default constructor
            col.AddConstructor();
            // default indexer
            IndexerDeclaration index = col.AddIndexer(
                typeof(Object)
                );
            ParameterDeclaration pindex = index.Signature.Parameters.Add(typeof(int), "index", false);

            // getter
            index.Get.Return(
                Expr.This.Prop("List").Item(Expr.Arg(pindex))
                );
            index.Set.AddAssign(
                Expr.This.Prop("List").Item(Expr.Arg(pindex)),
                Expr.Value
                );

            // add object method
            MethodDeclaration    addObject  = col.AddMethod("Add");
            ParameterDeclaration paraObject = addObject.Signature.Parameters.Add(new TypeTypeDeclaration(typeof(Object)), "o", true);

            addObject.Body.Add(
                Expr.This.Prop("List").Method("Add").Invoke(paraObject)
                );

            // if typed array add methods for type
            if (f.FieldType.GetElementType() != typeof(Object))
            {
                AddCollectionMethods(
                    col,
                    MapType(f.FieldType.GetElementType()),
                    this.conformer.ToCapitalized(f.FieldType.GetElementType().Name),
                    "o"
                    );
            }

            foreach (XmlElementAttribute ea in f.GetCustomAttributes(typeof(XmlElementAttribute), true))
            {
                string name  = this.conformer.ToCapitalized(ea.ElementName);
                string pname = this.conformer.ToCamel(name);

                ITypeDeclaration mappedType = null;
                if (ea.Type != null)
                {
                    mappedType = MapType(ea.Type);
                }

                if (mappedType == null || mappedType == f.FieldType.GetElementType())
                {
                    continue;
                }

                AddCollectionMethods(col, mappedType, name, pname);
            }

            // add field
            FieldDeclaration fd = c.AddField(col, f.Name);

            fd.InitExpression = Expr.New(col);
            PropertyDeclaration p = c.AddProperty(fd, f.Name, true, true, false);

            // setting attributes
            // attach xml text
            if (TypeHelper.HasCustomAttribute(f, typeof(XmlTextAttribute)))
            {
                AttributeDeclaration attr = p.CustomAttributes.Add(typeof(XmlTextAttribute));

                attr.Arguments.Add("Type", Expr.TypeOf(typeof(string)));

                // adding to string to collection
                MethodDeclaration tostring = col.AddMethod("ToString");
                tostring.Signature.ReturnType = new TypeTypeDeclaration(typeof(String));
                tostring.Attributes           = MemberAttributes.Public | MemberAttributes.Override;

                VariableDeclarationStatement sw = Stm.Var(typeof(StringWriter), "sw");
                sw.InitExpression = Expr.New(typeof(StringWriter));
                tostring.Body.Add(sw);
                ForEachStatement fe = Stm.ForEach(
                    typeof(string), "s", Expr.This.Prop("List"), false);

                fe.Body.Add(
                    Expr.Var(sw).Method("Write").Invoke(fe.Local)
                    );

                tostring.Body.Add(fe);
                tostring.Body.Return(Expr.Var(sw).Method("ToString").Invoke());
            }
            else if (TypeHelper.HasCustomAttribute(f, typeof(XmlArrayItemAttribute)))
            {
                // add xml array attribute
                AttributeDeclaration attr = p.CustomAttributes.Add(typeof(XmlArrayAttribute));
                attr.Arguments.Add("ElementName", Expr.Prim(f.Name));

                // add array item attribute
                XmlArrayItemAttribute arrayItem =
                    (XmlArrayItemAttribute)TypeHelper.GetFirstCustomAttribute(f, typeof(XmlArrayItemAttribute));

                attr = p.CustomAttributes.Add(typeof(XmlArrayItemAttribute));
                attr.Arguments.Add("ElementName", Expr.Prim(arrayItem.ElementName));
                //MMI:attr.Arguments.Add("Type",Expr.Prim(MapType(f.FieldType.GetElementType()).Name));
                attr.Arguments.Add("Type", Expr.TypeOf(MapType(f.FieldType.GetElementType())));

                if (arrayItem.Type != null)
                {
                    attr.Arguments.Add("DataType", Expr.Prim(arrayItem.DataType));
                }
                attr.Arguments.Add("IsNullable", Expr.Prim(arrayItem.IsNullable));
                if (this.Config.KeepNamespaces)
                {
                    attr.Arguments.Add("Namespace", Expr.Prim(arrayItem.Namespace));
                }
            }
            else
            {
                AttachXmlElementAttributes(p, f);
            }
        }
        public ClassDeclaration AddClass(NamespaceDeclaration ns)
        {
            ClassDeclaration col = ns.AddClass(this.CollectionName);

            // set base class as CollectionBase
            col.Parent = new TypeTypeDeclaration(typeof(CollectionBase));

            // default constructor
            col.AddConstructor();

            // add indexer
            if (this.ItemGet || this.ItemSet)
            {
                IndexerDeclaration index = col.AddIndexer(
                    this.Type
                    );
                ParameterDeclaration pindex = index.Signature.Parameters.Add(typeof(int), "index", false);

                // get body
                if (this.ItemGet)
                {
                    index.Get.Return(
                        (Expr.This.Prop("List").Item(Expr.Arg(pindex)).Cast(this.Type)
                        )
                        );
                }
                // set body
                if (this.ItemSet)
                {
                    index.Set.Add(
                        Stm.Assign(
                            Expr.This.Prop("List").Item(Expr.Arg(pindex)),
                            Expr.Value
                            )
                        );
                }
            }

            string pname = ns.Conformer.ToCamel(this.Type.Name);

            // add method
            if (this.Add)
            {
                MethodDeclaration    add  = col.AddMethod("Add");
                ParameterDeclaration para = add.Signature.Parameters.Add(this.Type, pname, true);
                add.Body.Add(
                    Expr.This.Prop("List").Method("Add").Invoke(para)
                    );
            }

            if (this.AddRange)
            {
                MethodDeclaration    add  = col.AddMethod("AddRange");
                ParameterDeclaration para = add.Signature.Parameters.Add(col, pname, true);

                ForEachStatement fe = Stm.ForEach(
                    this.Type,
                    "item",
                    Expr.Arg(para),
                    false
                    );
                fe.Body.Add(
                    Expr.This.Prop("List").Method("Add").Invoke(fe.Local)
                    );

                add.Body.Add(fe);
            }

            // contains method
            if (this.Contains)
            {
                MethodDeclaration contains = col.AddMethod("Contains");
                contains.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool));
                ParameterDeclaration para = contains.Signature.Parameters.Add(this.Type, pname, true);
                contains.Body.Return(
                    Expr.This.Prop("List").Method("Contains").Invoke(para)
                    );
            }

            // remove method
            if (this.Remove)
            {
                MethodDeclaration    remove = col.AddMethod("Remove");
                ParameterDeclaration para   = remove.Signature.Parameters.Add(this.Type, pname, true);

                remove.Doc.Summary.AddText("Removes the first occurrence of a specific ParameterDeclaration from this ParameterDeclarationCollection.");

                remove.Body.Add(
                    Expr.This.Prop("List").Method("Remove").Invoke(para)
                    );
            }

            // insert
            if (this.Insert)
            {
                MethodDeclaration    insert = col.AddMethod("Insert");
                ParameterDeclaration index  = insert.Signature.Parameters.Add(typeof(int), "index", true);
                ParameterDeclaration para   = insert.Signature.Parameters.Add(this.Type, pname, true);
                insert.Body.Add(
                    Expr.This.Prop("List").Method("Insert").Invoke(index, para)
                    );
            }

            // indexof
            if (this.IndexOf)
            {
                MethodDeclaration    indexof = col.AddMethod("IndexOf");
                ParameterDeclaration para    = indexof.Signature.Parameters.Add(this.Type, pname, true);
                indexof.Signature.ReturnType = new TypeTypeDeclaration(typeof(int));
                indexof.Body.Return(
                    Expr.This.Prop("List").Method("IndexOf").Invoke(para)
                    );
            }

            if (this.Enumerator)
            {
                // create subclass
                ClassDeclaration en = col.AddClass("Enumerator");
                // add wrapped field
                FieldDeclaration wrapped = en.AddField(
                    typeof(IEnumerator), "wrapped"
                    );
                // add IEnumerator
                en.Interfaces.Add(typeof(IEnumerator));

                // add constructor
                ConstructorDeclaration cs         = en.AddConstructor();
                ParameterDeclaration   collection = cs.Signature.Parameters.Add(col, "collection", true);
                cs.Body.Add(
                    Stm.Assign(
                        Expr.This.Field(wrapped),
                        Expr.Arg(collection).Cast(typeof(CollectionBase)).Method("GetEnumerator").Invoke()
                        )
                    );

                // add current
                PropertyDeclaration current = en.AddProperty(this.Type, "Current");
                current.Get.Return(
                    (Expr.This.Field(wrapped).Prop("Current")).Cast(this.Type)
                    );

                // add explicit interface implementation
                PropertyDeclaration currentEn = en.AddProperty(typeof(Object), "Current");
                currentEn.Get.Return(Expr.This.Prop(current));
                currentEn.PrivateImplementationType = wrapped.Type;

                // add reset
                MethodDeclaration reset = en.AddMethod("Reset");
                reset.ImplementationTypes.Add(wrapped.Type);
                reset.Body.Add(Expr.This.Field(wrapped).Method("Reset").Invoke());

                // add movenext
                MethodDeclaration movenext = en.AddMethod("MoveNext");
                movenext.ImplementationTypes.Add(wrapped.Type);
                movenext.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool));
                movenext.Body.Return(Expr.This.Field(wrapped).Method("MoveNext").Invoke());

                // add get enuemrator
                MethodDeclaration geten = col.AddMethod("GetEnumerator");
                geten.Attributes |= MemberAttributes.New;
                geten.ImplementationTypes.Add(new TypeTypeDeclaration(typeof(IEnumerable)));
                geten.Signature.ReturnType = en;
                geten.Body.Return(Expr.New(en, Expr.This));
            }

            return(col);
        }