示例#1
0
        public void DefineFullEvent_Adds_Methods_And_Event()
        {
            var moduleScope    = new ModuleScope();
            var generator      = new ProxyGenerator(moduleScope);
            var typeDefinition = generator.GetTypeDefinition(typeof(EmpyType), null, null);

            TypeBuilder typeBulder = moduleScope.Module.DefineType(typeDefinition.FullName, typeDefinition.TypeAttributes);

            var proxyScope = new ProxyScope(moduleScope, typeBulder, typeDefinition);

            proxyScope.DefineTypeAndMembers();

            Assert.AreEqual(0, proxyScope.Events.Count);
            Assert.AreEqual(0, proxyScope.Methods.Count);

            FieldBuilder field = proxyScope.DefineField("_myEvent", typeof(EventHandler), FieldAttributes.Private);

            var m = proxyScope.DefineFullEvent("MyEvent", EventAttributes.None, typeof(EventHandler), field);

            Assert.AreEqual(1, proxyScope.Events.Count);
            Assert.AreEqual("MyEvent", proxyScope.Events[0].GetName());

            Assert.AreEqual(2, proxyScope.Methods.Count);
            Assert.AreEqual("add_MyEvent", proxyScope.Methods[0].Name);
            Assert.AreEqual("remove_MyEvent", proxyScope.Methods[1].Name);
        }
        public void CreatePropertyChangedEventHandler(ProxyScope proxyScope)
        {
            EventBuilder propertyChangedEventBuilder = proxyScope.Events.FirstOrDefault(p => p.GetName() == EventName);

            if (propertyChangedEventBuilder == null)
            {
                FieldBuilder eventField = proxyScope.DefineField(EventName, typeof(PropertyChangedEventHandler), FieldAttributes.Private);
                proxyScope.DefineFullEvent(EventName, EventAttributes.None, typeof(PropertyChangedEventHandler), eventField);
            }
        }