Пример #1
0
        // example based on the MSDN Properties Sample (person.cs)
        public static void GenPerson(AssemblyGen ag)
        {
            var st  = ag.StaticFactory;
            var exp = ag.ExpressionFactory;

            TypeGen Person = ag.Class("Person");
            {
                FieldGen myName = Person.Private.Field(typeof(string), "myName", "N/A");
                FieldGen myAge  = Person.Private.Field(typeof(int), "myAge", 0);

                // Declare a Name property of type string:
                PropertyGen Name = Person.Public.SimpleProperty(myName, "Name");

                // Declare an Age property of type int:
                PropertyGen Age = Person.Public.SimpleProperty(myAge, "Age");

                CodeGen g = Person.Public.Override.Method(typeof(string), "ToString");
                {
                    g.Return("Name = " + Name + ", Age = " + Age);
                }

                g = Person.Public.Static.Method(typeof(void), "Main");
                {
                    g.WriteLine("Simple Properties");

                    // Create a new Person object:
                    var person = g.Local(exp.New(Person));

                    // Print out the name and the age associated with the person:
                    g.WriteLine("Person details - {0}", person);

                    // Set some values on the person object:
                    ITypeMapper typeMapper = ag.TypeMapper;
                    g.Assign(person.Property("Name"), "Joe");
                    ITypeMapper typeMapper1 = ag.TypeMapper;
                    g.Assign(person.Property("Age"), 99);
                    g.WriteLine("Person details - {0}", person);

                    // Increment the Age property:
                    ITypeMapper typeMapper2 = ag.TypeMapper;
                    g.AssignAdd(person.Property("Age"), 1);
                    g.WriteLine("Person details - {0}", person);
                }
            }
        }
Пример #2
0
        // example based on the MSDN Properties Sample (person.cs)
        public static void GenPerson(AssemblyGen ag)
        {
            TypeGen Person = ag.Class("Person");
            {
                FieldGen myName = Person.Private.Field <string>("myName", "N/A");
                FieldGen myAge  = Person.Private.Field <int>("myAge", 0);

                // Declare a Name property of type string:
                PropertyGen Name = Person.Public.SimpleProperty(myName, "Name");

                // Declare an Age property of type int:
                PropertyGen Age = Person.Public.SimpleProperty(myAge, "Age");

                CodeGen g = Person.Public.Override.Method <string>("ToString");
                {
                    g.Return("Name = " + Name + ", Age = " + Age);
                }

                g = Person.Public.Static.Void("Main");
                {
                    g.WriteLine("Simple Properties");

                    // Create a new Person object:
                    Operand person = g.Local(Exp.New(Person));

                    // Print out the name and the age associated with the person:
                    g.WriteLine("Person details - {0}", person);

                    // Set some values on the person object:
                    g.Assign(person.Property("Name"), "Joe");
                    g.Assign(person.Property("Age"), 99);
                    g.WriteLine("Person details - {0}", person);

                    // Increment the Age property:
                    g.AssignAdd(person.Property("Age"), 1);
                    g.WriteLine("Person details - {0}", person);
                }
            }
        }
Пример #3
0
        // example based on the MSDN Delegates Sample (bookstore.cs)
        public static void GenBookstore(AssemblyGen ag)
        {
            var st  = ag.StaticFactory;
            var exp = ag.ExpressionFactory;

            ITypeMapper m = ag.TypeMapper;
            TypeGen     book, processBookDelegate, BookDBLocal;

            // A set of classes for handling a bookstore:
            using (ag.Namespace("Bookstore"))
            {
                // Describes a book in the book list:
                book = ag.Public.Struct("Book");
                {
                    FieldGen title     = book.Public.Field(typeof(string), "Title");                       // Title of the book.
                    FieldGen author    = book.Public.Field(typeof(string), "Author");                      // Author of the book.
                    FieldGen price     = book.Public.Field(typeof(decimal), "Price");                      // Price of the book.
                    FieldGen paperback = book.Public.Field(typeof(bool), "Paperback");                     // Is it paperback?

                    CodeGen g = book.Public.Constructor()
                                .Parameter(typeof(string), "title")
                                .Parameter(typeof(string), "author")
                                .Parameter(typeof(decimal), "price")
                                .Parameter(typeof(bool), "paperBack");
                    {
                        g.Assign(title, g.Arg("title"));
                        g.Assign(author, g.Arg("author"));
                        g.Assign(price, g.Arg("price"));
                        g.Assign(paperback, g.Arg("paperBack"));
                    }
                }

                // Declare a delegate type for processing a book:
                processBookDelegate = ag.Public.Delegate(typeof(void), "ProcessBookDelegate").Parameter(book, "book");

                // Maintains a book database.
                BookDBLocal = ag.Public.Class("BookDB");
                {
                    // List of all books in the database:
                    FieldGen list = BookDBLocal.Field(typeof(ArrayList), "list", exp.New(typeof(ArrayList)));

                    // Add a book to the database:
                    CodeGen g = BookDBLocal.Public.Method(typeof(void), "AddBook")
                                .Parameter(typeof(string), "title")
                                .Parameter(typeof(string), "author")
                                .Parameter(typeof(decimal), "price")
                                .Parameter(typeof(bool), "paperBack")
                    ;
                    {
                        g.Invoke(list, "Add", exp.New(book, g.Arg("title"), g.Arg("author"), g.Arg("price"), g.Arg("paperBack")));
                    }

                    // Call a passed-in delegate on each paperback book to process it:
                    g = BookDBLocal.Public.Method(typeof(void), "ProcessPaperbackBooks").Parameter(processBookDelegate, "processBook");
                    {
                        var b = g.ForEach(book, list);
                        {
                            g.If(b.Field("Paperback"));
                            {
                                g.InvokeDelegate(g.Arg("processBook"), b);
                            }
                            g.End();
                        }
                        g.End();
                    }
                }
            }

            // Using the Bookstore classes:
            using (ag.Namespace("BookTestClient"))
            {
                // Class to total and average prices of books:
                TypeGen priceTotaller = ag.Class("PriceTotaller");
                {
                    FieldGen countBooks = priceTotaller.Field(typeof(int), "countBooks", 0);
                    FieldGen priceBooks = priceTotaller.Field(typeof(decimal), "priceBooks", 0.0m);

                    CodeGen g = priceTotaller.Internal.Method(typeof(void), "AddBookToTotal").Parameter(book, "book");
                    {
                        g.AssignAdd(countBooks, 1);
                        g.AssignAdd(priceBooks, g.Arg("book").Field("Price"));
                    }

                    g = priceTotaller.Internal.Method(typeof(decimal), "AveragePrice");
                    {
                        g.Return(priceBooks / countBooks);
                    }
                }

                // Class to test the book database:
                TypeGen test = ag.Class("Test");
                {
                    // Print the title of the book.
                    CodeGen g = test.Static.Method(typeof(void), "PrintTitle").Parameter(book, "book");
                    {
                        g.WriteLine("   {0}", g.Arg("book").Field("Title"));
                    }

                    // Initialize the book database with some test books:
                    g = test.Static.Method(typeof(void), "AddBooks").Parameter(BookDBLocal, "bookDB");
                    {
                        var bookDb = g.Arg("bookDB");

                        g.Invoke(bookDb, "AddBook", "The C Programming Language",
                                 "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true);
                        g.Invoke(bookDb, "AddBook", "The Unicode Standard 2.0",
                                 "The Unicode Consortium", 39.95m, true);
                        g.Invoke(bookDb, "AddBook", "The MS-DOS Encyclopedia",
                                 "Ray Duncan", 129.95m, false);
                        g.Invoke(bookDb, "AddBook", "Dogbert's Clues for the Clueless",
                                 "Scott Adams", 12.00m, true);
                    }

                    // Execution starts here.
                    g = test.Public.Static.Method(typeof(void), "Main");
                    {
                        var bookDb = g.Local(exp.New(BookDBLocal));

                        // Initialize the database with some books:
                        g.Invoke(test, "AddBooks", bookDb);

                        // Print all the titles of paperbacks:
                        g.WriteLine("Paperback Book Titles:");
                        // Create a new delegate object associated with the static
                        // method Test.PrintTitle:
                        g.Invoke(bookDb, "ProcessPaperbackBooks", (Operand)exp.NewDelegate(processBookDelegate, test, "PrintTitle"));

                        // Get the average price of a paperback by using
                        // a PriceTotaller object:
                        var totaller = g.Local(exp.New(priceTotaller));
                        // Create a new delegate object associated with the nonstatic
                        // method AddBookToTotal on the object totaller:
                        g.Invoke(bookDb, "ProcessPaperbackBooks", (Operand)exp.NewDelegate(processBookDelegate, totaller, "AddBookToTotal"));
                        g.WriteLine("Average Paperback Book Price: ${0:#.##}",
                                    totaller.Invoke("AveragePrice"));
                    }
                }
            }
        }
Пример #4
0
        // example based on the MSDN Delegates Sample (bookstore.cs)
        public static void GenBookstore(AssemblyGen ag)
        {
            TypeGen Book, ProcessBookDelegate, BookDB;

            // A set of classes for handling a bookstore:
            using (ag.Namespace("Bookstore"))
            {
                // Describes a book in the book list:
                Book = ag.Public.Struct("Book");
                {
                    FieldGen Title     = Book.Public.Field <string>("Title");                      // Title of the book.
                    FieldGen Author    = Book.Public.Field <string>("Author");                     // Author of the book.
                    FieldGen Price     = Book.Public.Field <decimal>("Price");                     // Price of the book.
                    FieldGen Paperback = Book.Public.Field <bool>("Paperback");                    // Is it paperback?

                    CodeGen g = Book.Public.Constructor()
                                .Parameter <string>("title")
                                .Parameter <string>("author")
                                .Parameter <decimal>("price")
                                .Parameter <bool>("paperBack");
                    {
                        g.Assign(Title, g.Arg("title"));
                        g.Assign(Author, g.Arg("author"));
                        g.Assign(Price, g.Arg("price"));
                        g.Assign(Paperback, g.Arg("paperBack"));
                    }
                }

                // Declare a delegate type for processing a book:
                ProcessBookDelegate = ag.Public.DelegateVoid("ProcessBookDelegate").Parameter(Book, "book");

                // Maintains a book database.
                BookDB = ag.Public.Class("BookDB");
                {
                    // List of all books in the database:
                    FieldGen list = BookDB.Field <ArrayList>("list", Exp.New <ArrayList>());

                    // Add a book to the database:
                    CodeGen g = BookDB.Public.Void("AddBook")
                                .Parameter <string>("title")
                                .Parameter <string>("author")
                                .Parameter <decimal>("price")
                                .Parameter <bool>("paperBack");
                    {
                        g.Invoke(list, "Add", Exp.New(Book, g.Arg("title"), g.Arg("author"), g.Arg("price"), g.Arg("paperBack")));
                    }

                    // Call a passed-in delegate on each paperback book to process it:
                    g = BookDB.Public.Void("ProcessPaperbackBooks").Parameter(ProcessBookDelegate, "processBook");
                    {
                        Operand b = g.ForEach(Book, list);
                        {
                            g.If(b.Field("Paperback"));
                            {
                                g.InvokeDelegate(g.Arg("processBook"), b);
                            }
                            g.End();
                        }
                        g.End();
                    }
                }
            }

            // Using the Bookstore classes:
            using (ag.Namespace("BookTestClient"))
            {
                // Class to total and average prices of books:
                TypeGen PriceTotaller = ag.Class("PriceTotaller");
                {
                    FieldGen countBooks = PriceTotaller.Field <int>("countBooks", 0);
                    FieldGen priceBooks = PriceTotaller.Field <decimal>("priceBooks", 0.0m);

                    CodeGen g = PriceTotaller.Internal.Void("AddBookToTotal").Parameter(Book, "book");
                    {
                        g.AssignAdd(countBooks, 1);
                        g.AssignAdd(priceBooks, g.Arg("book").Field("Price"));
                    }

                    g = PriceTotaller.Internal.Method <decimal>("AveragePrice");
                    {
                        g.Return(priceBooks / countBooks);
                    }
                }

                // Class to test the book database:
                TypeGen Test = ag.Class("Test");
                {
                    // Print the title of the book.
                    CodeGen g = Test.Static.Void("PrintTitle").Parameter(Book, "book");
                    {
                        g.WriteLine("   {0}", g.Arg("book").Field("Title"));
                    }

                    // Initialize the book database with some test books:
                    g = Test.Static.Void("AddBooks").Parameter(BookDB, "bookDB");
                    {
                        Operand bookDB = g.Arg("bookDB");

                        g.Invoke(bookDB, "AddBook", "The C Programming Language",
                                 "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true);
                        g.Invoke(bookDB, "AddBook", "The Unicode Standard 2.0",
                                 "The Unicode Consortium", 39.95m, true);
                        g.Invoke(bookDB, "AddBook", "The MS-DOS Encyclopedia",
                                 "Ray Duncan", 129.95m, false);
                        g.Invoke(bookDB, "AddBook", "Dogbert's Clues for the Clueless",
                                 "Scott Adams", 12.00m, true);
                    }

                    // Execution starts here.
                    g = Test.Static.Void("Main");
                    {
                        Operand bookDB = g.Local(Exp.New(BookDB));

                        // Initialize the database with some books:
                        g.Invoke(Test, "AddBooks", bookDB);

                        // Print all the titles of paperbacks:
                        g.WriteLine("Paperback Book Titles:");
                        // Create a new delegate object associated with the static
                        // method Test.PrintTitle:
                        g.Invoke(bookDB, "ProcessPaperbackBooks", Exp.NewDelegate(ProcessBookDelegate, Test, "PrintTitle"));

                        // Get the average price of a paperback by using
                        // a PriceTotaller object:
                        Operand totaller = g.Local(Exp.New(PriceTotaller));
                        // Create a new delegate object associated with the nonstatic
                        // method AddBookToTotal on the object totaller:
                        g.Invoke(bookDB, "ProcessPaperbackBooks", Exp.NewDelegate(ProcessBookDelegate, totaller, "AddBookToTotal"));
                        g.WriteLine("Average Paperback Book Price: ${0:#.##}",
                                    totaller.Invoke("AveragePrice"));
                    }
                }
            }
        }