예제 #1
0
        // example based on the MSDN Delegates Sample (compose.cs)
        public static void GenCompose(AssemblyGen ag)
        {
            var st  = ag.StaticFactory;
            var exp = ag.ExpressionFactory;

            TypeGen myDelegate = ag.Delegate(typeof(void), "MyDelegate").Parameter(typeof(string), "string");

            TypeGen myClass = ag.Class("MyClass");
            {
                CodeGen g = myClass.Public.Static.Method(typeof(void), "Hello").Parameter(typeof(string), "s");
                {
                    g.WriteLine("  Hello, {0}!", g.Arg("s"));
                }

                g = myClass.Public.Static.Method(typeof(void), "Goodbye").Parameter(typeof(string), "s");
                {
                    g.WriteLine("  Goodbye, {0}!", g.Arg("s"));
                }

                g = myClass.Public.Static.Method(typeof(void), "Main");
                {
                    ContextualOperand a = g.Local(), b = g.Local(), c = g.Local(), d = g.Local();

                    // Create the delegate object a that references
                    // the method Hello:
                    ITypeMapper typeMapper = ag.TypeMapper;
                    g.Assign(a, exp.NewDelegate(myDelegate, myClass, "Hello"));
                    // Create the delegate object b that references
                    // the method Goodbye:
                    ITypeMapper typeMapper1 = ag.TypeMapper;
                    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");
                }
            }
        }
예제 #2
0
        public static void GenTypeAttributeTest(AssemblyGen ag)
		{
			TypeGen MyAttribute = ag.Public.Class("MyAttribute", typeof(Attribute))
				.BeginAttribute(typeof(AttributeUsageAttribute), AttributeTargets.Class).Set("AllowMultiple", true).End()
				;
			FieldGen testField = MyAttribute.Field(typeof(object), "testField")
				.Attribute(typeof(DescriptionAttribute), "Test field")
				;
			PropertyGen testProperty = MyAttribute.Public.SimpleProperty(testField, "TestProperty")
				.Attribute(typeof(ObsoleteAttribute), "Do not use this")
				;

			testProperty.Getter().Attribute(typeof(DescriptionAttribute), "Getter method");
			testProperty.Getter().ReturnParameter.Attribute(typeof(DescriptionAttribute), "Getter return value");
			testProperty.Setter().Attribute(typeof(DescriptionAttribute), "Setter method");

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

			tg.Public.Static.Method(typeof(void), "Main")
				.BeginAttribute(MyAttribute).Set("TestProperty", 3).End()
				;

			TypeGen SimpleDelegate = ag.Delegate(typeof(void), "SimpleDelegate")
				.Attribute(typeof(DescriptionAttribute), "Test delegate")
				;

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

			TestEvent.AddMethod().Attribute(typeof(DescriptionAttribute), "Event add method");
			TestEvent.RemoveMethod().Attribute(typeof(DescriptionAttribute), "Event remove method");
		}
        public static void GenTypeAttributeTest(AssemblyGen ag)
        {
            TypeGen MyAttribute = ag.Public.Class("MyAttribute", typeof(Attribute))
                                  .BeginAttribute(typeof(AttributeUsageAttribute), AttributeTargets.Class).Set("AllowMultiple", true).End()
            ;
            FieldGen testField = MyAttribute.Field(typeof(object), "testField")
                                 .Attribute(typeof(DescriptionAttribute), "Test field")
            ;
            PropertyGen testProperty = MyAttribute.Public.SimpleProperty(testField, "TestProperty")
                                       .Attribute(typeof(ObsoleteAttribute), "Do not use this")
            ;

            testProperty.Getter().Attribute(typeof(DescriptionAttribute), "Getter method");
            testProperty.Getter().ReturnParameter.Attribute(typeof(DescriptionAttribute), "Getter return value");
            testProperty.Setter().Attribute(typeof(DescriptionAttribute), "Setter method");

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

            tg.Static.Method(typeof(void), "Main")
            .BeginAttribute(MyAttribute).Set("TestProperty", 3).End()
            ;

            TypeGen SimpleDelegate = ag.Delegate(typeof(void), "SimpleDelegate")
                                     .Attribute(typeof(DescriptionAttribute), "Test delegate")
            ;

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

            TestEvent.AddMethod().Attribute(typeof(DescriptionAttribute), "Event add method");
            TestEvent.RemoveMethod().Attribute(typeof(DescriptionAttribute), "Event remove method");
        }
예제 #4
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.Delegate(typeof(void), "ChangedEventHandler").Parameter(typeof(object), "sender").Parameter(typeof(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.Method(typeof(void), "OnChanged").Parameter(typeof(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(typeof(int), "Add").Parameter(typeof(object), "value");
                    {
                        Operand i = g.Local(g.Base().Invoke("Add", g.Arg("value")));
                        g.Invoke(g.This(), "OnChanged", Static.Field(typeof(EventArgs), "Empty"));
                        g.Return(i);
                    }

                    g = ListWithChangedEvent.Public.Override.Method(typeof(void), "Clear");
                    {
                        g.Invoke(g.Base(), "Clear");
                        g.Invoke(g.This(), "OnChanged", Static.Field(typeof(EventArgs), "Empty"));
                    }

                    g = ListWithChangedEvent.Public.Override.Indexer(typeof(object)).Index(typeof(int), "index").Setter();
                    {
                        g.Assign(g.Base()[g.Arg("index")], g.PropertyValue());
                        g.Invoke(g.This(), "OnChanged", Static.Field(typeof(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.Method(typeof(void), "ListChanged").Parameter(typeof(object), "sender").Parameter(typeof(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.Method(typeof(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.Method(typeof(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");
                    }
                }
            }
        }
예제 #5
0
        // example based on the MSDN Delegates Sample (compose.cs)
        public static void GenCompose(AssemblyGen ag)
        {
            TypeGen MyDelegate = ag.Delegate(typeof(void), "MyDelegate").Parameter(typeof(string), "string");

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

                g = MyClass.Public.Static.Method(typeof(void), "Goodbye").Parameter(typeof(string), "s");
                {
                    g.WriteLine("  Goodbye, {0}!", g.Arg("s"));
                }

                g = MyClass.Public.Static.Method(typeof(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");
                }
            }
        }
예제 #6
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.Delegate(typeof(void), "ChangedEventHandler").Parameter(typeof(object), "sender").Parameter(typeof(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.Method(typeof(void), "OnChanged").Parameter(typeof(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(typeof(int), "Add").Parameter(typeof(object), "value");
                    {
                        Operand i = g.Local(g.Base().Invoke("Add", g.Arg("value")));
                        g.Invoke(g.This(), "OnChanged", Static.Field(typeof(EventArgs), "Empty"));
                        g.Return(i);
                    }

                    g = ListWithChangedEvent.Public.Override.Method(typeof(void), "Clear");
                    {
                        g.Invoke(g.Base(), "Clear");
                        g.Invoke(g.This(), "OnChanged", Static.Field(typeof(EventArgs), "Empty"));
                    }

                    g = ListWithChangedEvent.Public.Override.Indexer(typeof(object)).Index(typeof(int), "index").Setter();
                    {
                        g.Assign(g.Base()[g.Arg("index")], g.PropertyValue());
                        g.Invoke(g.This(), "OnChanged", Static.Field(typeof(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.Method(typeof(void), "ListChanged").Parameter(typeof(object), "sender").Parameter(typeof(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.Method(typeof(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.Method(typeof(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");
                    }
                }
            }
        }