Ejemplo n.º 1
0
        public static void CreateUser()
        {
            // create namespace
            NamespaceDeclaration ns = new NamespaceDeclaration("MyFirstNamespace");

            // adding imports

            // creating User class
            ClassDeclaration user = ns.AddClass("User");

            // put some comments
            user.Doc.Summary.AddText("System.Extentions User class");
            user.Doc.Remarks.Add("para");
            user.Doc.Remarks.Into();
            user.Doc.Remarks.AddText("This class was generated by System.Extentions");
            user.Doc.Remarks.OutOf();

            // adding private fields
            FieldDeclaration name     = user.AddField(typeof(string), "name");
            FieldDeclaration lastName = user.AddField(typeof(string), "lastName");

            // adding properties for the fields
            PropertyDeclaration pname     = user.AddProperty(name, true, true, true);
            PropertyDeclaration plastName = user.AddProperty(lastName, true, true, true);

            Output(ns);
        }
Ejemplo n.º 2
0
        public static void CreateUser()
        {
            // create namespace
            var ns = new NamespaceDeclaration("MyFirstNamespace");

            // adding imports

            // creating User class
            var user = ns.AddClass("User");

            // put some comments
            user.Doc.Summary.AddText("Refly User class");
            user.Doc.Remarks.Add("para");
            user.Doc.Remarks.Into();
            user.Doc.Remarks.AddText("This class was generated by Refly");
            user.Doc.Remarks.OutOf();

            // adding private fields
            var name     = user.AddField(typeof(string), "name");
            var lastName = user.AddField(typeof(string), "lastName");

            // adding properties for the fields
            var pname     = user.AddProperty(name, true, true, true);
            var plastName = user.AddProperty(lastName, true, true, true);

            Output(ns);
        }
Ejemplo n.º 3
0
 public void VerifyDefaultCtorWithComment()
 {
     NamespaceDeclaration nsdecl = new NamespaceDeclaration("My.Stuff");
     nsdecl.AddClass("SomeClass")
         .AddConstructor("Initializes a new instance of SomeClass");
     new CodeBuilder().GenerateCode(Console.Out, nsdecl);
 }
Ejemplo n.º 4
0
 public void VerifyDefaultCtor()
 {
     NamespaceDeclaration nsdecl = new NamespaceDeclaration("My.NS");
     nsdecl.AddClass("MyClass").AddConstructor();
     CodeBuilder builder = new CodeBuilder();
     builder.GenerateCode(Console.Out, nsdecl);
 }
Ejemplo n.º 5
0
        public static void Refly()
        {
            // creating the Refly.Demo namespace
            NamespaceDeclaration demo = new NamespaceDeclaration("Refly.Demo");

            // create the user class
            ClassDeclaration user = demo.AddClass("User");

            // add name field
            FieldDeclaration name = user.AddField(typeof(string), "name");

            // add constructor
            ConstructorDeclaration cstr = user.AddConstructor();
            // add name parameter
            ParameterDeclaration pname = cstr.Signature.Parameters.Add(typeof(string), "name", true);

            // this.name = name;
            cstr.Body.AddAssign(
                Expr.This.Field(name),
                Expr.Arg(pname)
                );

            // add property
            user.AddProperty(name, true, false, false);
        }
Ejemplo n.º 6
0
        public void VerifySingleArg()
        {
            NamespaceDeclaration nsdecl = new NamespaceDeclaration("My.NS");
            nsdecl.AddClass("MyClass")
                .AddConstructor(typeof(int), "id", "_id");

            CodeBuilder builder = new CodeBuilder();
            builder.GenerateCode(Console.Out, nsdecl);
        }
Ejemplo n.º 7
0
 public void VerifyMultipleArgs()
 {
     NamespaceDeclaration nsdecl = new NamespaceDeclaration("My.Stuff");
     nsdecl.AddClass("SomeClass")
             .AddConstructor("Initializes a new instance of SomeClass",
                 new ConstructorArg(typeof(int), "id", "_id", "ID of the person"),
                 new ConstructorArg(typeof(string), "name", "_name", "Name of the person")
             );
     new CodeBuilder().GenerateCode(Console.Out, nsdecl);
 }
        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);
        }
        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);
        }