Esempio n. 1
0
        public static void GenTypeAttributeTest(AssemblyGen ag)
        {
            TypeGen MyAttribute = ag.Public.Class("MyAttribute", typeof(Attribute))
                                  .BeginAttribute <AttributeUsageAttribute>(AttributeTargets.Class).Set("AllowMultiple", true).End();

            FieldGen testField = MyAttribute.Field <object>("testField")
                                 .Attribute <DescriptionAttribute>("Test field");

            PropertyGen testProperty = MyAttribute.Public.SimpleProperty(testField, "TestProperty")
                                       .Attribute <ObsoleteAttribute>("Do not use this");

            testProperty.Getter().Attribute <DescriptionAttribute>("Getter method");
            testProperty.Getter().ReturnParameter.Attribute <DescriptionAttribute>("Getter return value");
            testProperty.Setter().Attribute <DescriptionAttribute>("Setter method");

            TypeGen tg = ag.Class("Test")
                         .BeginAttribute(MyAttribute).Set("TestProperty", 3).End()
                         .Attribute <DescriptionAttribute>("Test class");

            tg.Static.Void("Main")
            .BeginAttribute(MyAttribute).Set("TestProperty", 3).End();

            TypeGen SimpleDelegate = ag.DelegateVoid("SimpleDelegate")
                                     .Attribute <DescriptionAttribute>("Test delegate");

            EventGen TestEvent = tg.Static.Event(SimpleDelegate, "TestEvent")
                                 .Attribute <DescriptionAttribute>("Test event");

            TestEvent.AddMethod().Attribute <DescriptionAttribute>("Event add method");
            TestEvent.RemoveMethod().Attribute <DescriptionAttribute>("Event remove method");
        }
Esempio n. 2
0
        // example based on the MSDN Delegates Sample (compose.cs)
        public static void GenCompose(AssemblyGen ag)
        {
            TypeGen MyDelegate = ag.DelegateVoid("MyDelegate").Parameter <string>("string");

            TypeGen MyClass = ag.Class("MyClass");
            {
                CodeGen g = MyClass.Public.Static.Void("Hello").Parameter <string>("s");
                {
                    g.WriteLine("  Hello, {0}!", g.Arg("s"));
                }

                g = MyClass.Public.Static.Void("Goodbye").Parameter <string>("s");
                {
                    g.WriteLine("  Goodbye, {0}!", g.Arg("s"));
                }

                g = MyClass.Public.Static.Void("Main");
                {
                    Operand a = g.Local(), b = g.Local(), c = g.Local(), d = g.Local();

                    // Create the delegate object a that references
                    // the method Hello:
                    g.Assign(a, Exp.NewDelegate(MyDelegate, MyClass, "Hello"));
                    // Create the delegate object b that references
                    // the method Goodbye:
                    g.Assign(b, Exp.NewDelegate(MyDelegate, MyClass, "Goodbye"));
                    // The two delegates, a and b, are composed to form c,
                    // which calls both methods in order:
                    g.Assign(c, a + b);
                    // Remove a from the composed delegate, leaving d,
                    // which calls only the method Goodbye:
                    g.Assign(d, c - a);

                    g.WriteLine("Invoking delegate a:");
                    g.InvokeDelegate(a, "A");
                    g.WriteLine("Invoking delegate b:");
                    g.InvokeDelegate(b, "B");
                    g.WriteLine("Invoking delegate c:");
                    g.InvokeDelegate(c, "C");
                    g.WriteLine("Invoking delegate d:");
                    g.InvokeDelegate(d, "D");
                }
            }
        }
Esempio n. 3
0
        // example based on the MSDN Events Sample (events1.cs)
        public static void GenEvents1(AssemblyGen ag)
        {
            TypeGen ChangedEventHandler, ListWithChangedEvent;

            using (ag.Namespace("MyCollections"))
            {
                // A delegate type for hooking up change notifications.
                ChangedEventHandler = ag.DelegateVoid("ChangedEventHandler").Parameter("sender").Parameter <EventArgs>("e");

                // A class that works just like ArrayList, but sends event
                // notifications whenever the list changes.
                ListWithChangedEvent = ag.Public.Class("ListWithChangedEvent", typeof(ArrayList));
                {
                    // An event that clients can use to be notified whenever the
                    // elements of the list change.
                    EventGen Changed = ListWithChangedEvent.Public.Event(ChangedEventHandler, "Changed");

                    // Invoke the Changed event; called whenever list changes
                    CodeGen g = ListWithChangedEvent.Protected.Virtual.Void("OnChanged").Parameter <EventArgs>("e");
                    {
                        g.If(Changed != null);
                        {
                            g.InvokeDelegate(Changed, g.This(), g.Arg("e"));
                        }
                        g.End();
                    }

                    // Override some of the methods that can change the list;
                    // invoke event after each
                    g = ListWithChangedEvent.Public.Override.Method <int>("Add").Parameter("value");
                    {
                        Operand i = g.Local(g.Base().Invoke("Add", g.Arg("value")));
                        g.Invoke(g.This(), "OnChanged", Static.Field <EventArgs>("Empty"));
                        g.Return(i);
                    }

                    g = ListWithChangedEvent.Public.Override.Void("Clear");
                    {
                        g.Invoke(g.Base(), "Clear");
                        g.Invoke(g.This(), "OnChanged", Static.Field <EventArgs>("Empty"));
                    }

                    g = ListWithChangedEvent.Public.Override.Indexer <object>().Index <int>("index").Setter();
                    {
                        g.Assign(g.Base()[g.Arg("index")], g.PropertyValue());
                        g.Invoke(g.This(), "OnChanged", Static.Field <EventArgs>("Empty"));
                    }
                }
            }

            using (ag.Namespace("TestEvents"))
            {
                TypeGen EventListener = ag.Class("EventListener");
                {
                    FieldGen List = EventListener.Field(ListWithChangedEvent, "List");

                    // This will be called whenever the list changes.
                    CodeGen g = EventListener.Private.Void("ListChanged").Parameter("sender").Parameter <EventArgs>("eventArgs");
                    {
                        g.WriteLine("This is called when the event fires.");
                    }

                    g = EventListener.Public.Constructor().Parameter(ListWithChangedEvent, "list");
                    {
                        g.Assign(List, g.Arg("list"));
                        // Add "ListChanged" to the Changed event on "List".
                        g.SubscribeEvent(List, "Changed", Exp.NewDelegate(ChangedEventHandler, g.This(), "ListChanged"));
                    }

                    g = EventListener.Public.Void("Detach");
                    {
                        // Detach the event and delete the list
                        g.UnsubscribeEvent(List, "Changed", Exp.NewDelegate(ChangedEventHandler, g.This(), "ListChanged"));
                        g.Assign(List, null);
                    }
                }

                TypeGen Test = ag.Class("Test");
                {
                    // Test the ListWithChangedEvent class.
                    CodeGen g = Test.Public.Static.Void("Main");
                    {
                        // Create a new list.
                        Operand list = g.Local(Exp.New(ListWithChangedEvent));

                        // Create a class that listens to the list's change event.
                        Operand listener = g.Local(Exp.New(EventListener, list));

                        // Add and remove items from the list.
                        g.Invoke(list, "Add", "item 1");
                        g.Invoke(list, "Clear");
                        g.Invoke(listener, "Detach");
                    }
                }
            }
        }