Inheritance: _EventBuilder
	protected void SetUp () {
		AssemblyName assemblyName = new AssemblyName();
		assemblyName.Name = GetType().FullName;

		AssemblyBuilder assembly 
			= Thread.GetDomain().DefineDynamicAssembly(
				assemblyName, AssemblyBuilderAccess.Run);

		module = assembly.DefineDynamicModule("module1");
		
	    tb = module.DefineType("class1", 
							   TypeAttributes.Public);

		eb = tb.DefineEvent ("event1", EventAttributes.None, typeof (AnEvent));
		mb = 
			tb.DefineMethod ("OnAnEvent",
							 MethodAttributes.Public, typeof (void),
							 new Type [] { typeof (AnEvent) });
		ILGenerator ilgen = mb.GetILGenerator();
		ilgen.Emit (OpCodes.Ret);

		// These two are required
		eb.SetAddOnMethod (mb);
		eb.SetRemoveOnMethod (mb);
	}
Exemple #2
0
		public EventEmitter(AbstractTypeEmitter typeEmitter, string name, EventAttributes attributes, Type type)
		{
			this.typeEmitter = typeEmitter;
			this.type = type;
			this.name = name;
			eventBuilder = typeEmitter.TypeBuilder.DefineEvent(name, attributes, type);
		}
Exemple #3
0
		public EasyEvent(AbstractEasyType maintype, String name, Type eventHandlerType)
		{
			m_name = name;
			m_maintype = maintype;
			m_builder = maintype.TypeBuilder.DefineEvent(
				name, EventAttributes.None, eventHandlerType);
		}
		public EventEmitter(AbstractTypeEmitter typeEmitter, string name, EventAttributes attributes, Type type)
		{
			if (name == null) throw new ArgumentNullException("name");
			if (type == null) throw new ArgumentNullException("type");
			this.typeEmitter = typeEmitter;
			this.type = type;
			eventBuilder = typeEmitter.TypeBuilder.DefineEvent(name, attributes, type);
		}
		internal EventGen(TypeGen owner, string name, Type type, MethodAttributes mthAttr)
		{
			this.owner = owner;
			this.name = name;
			this.type = type;
			this.attrs = mthAttr;

			eb = owner.TypeBuilder.DefineEvent(name, EventAttributes.None, type);
			owner.RegisterForCompletion(this);
		}
        private void BuildAdder(TypeBuilder typeBuilder, EventInfo contractProperty, EventBuilder builder)
        {
            var addMethod = contractProperty.GetAddMethod();

            if (null != addMethod)
            {
                var addMethodBuilder = new ProxyMethodImplementationStrategy().BuildMethodProxy(typeBuilder, addMethod);
                builder.SetAddOnMethod(addMethodBuilder);
            }
        }
        private void BuildRemover(TypeBuilder typeBuilder, EventInfo contractProperty, EventBuilder builder)
        {
            var removeMethod = contractProperty.GetRemoveMethod();

            if (null != removeMethod)
            {
                var removeMethodBuilder = new ProxyMethodImplementationStrategy().BuildMethodProxy(typeBuilder, removeMethod);
                builder.SetRemoveOnMethod(removeMethodBuilder);
            }
        }
 internal EventOnTypeBuilderInst(MonoGenericClass instantiation, EventBuilder evt)
 {
     this.instantiation = instantiation;
     this.evt           = evt;
 }
Exemple #9
0
 private extern EventInfo get_event_info(EventBuilder eb);
		private EventInfo get_event_info (EventBuilder eb)
		{
			throw new System.NotImplementedException();
		}
Exemple #11
0
 private void EmitCustomAttributes(EventBuilder eventBuilder, IEnumerable<Cci.ICustomAttribute> attributes)
 {
     foreach (var attribute in attributes)
     {
         eventBuilder.SetCustomAttribute(CreateCustomAttributeBuilder(attribute));
     }
 }
Exemple #12
0
		public EvntInfo AddEvent(IEventNode ev, EventBuilder eb)
		{
			EvntInfo ei = new EvntInfo(eb);
			defs[ev] = ei;
			return ei;
		}
		internal EventOnTypeBuilderInst (TypeBuilderInstantiation instantiation, EventBuilder evt)
		{
			this.instantiation = instantiation;
			this.event_builder = evt;
		}
Exemple #14
0
 public ILEventDebugImpl(EventBuilder eventBuilder)
 {
     _eventBuilder = eventBuilder;
 }
Exemple #15
0
		public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
		{
			MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);

			// And now store the values in our own fields.
			
			declaring_type = type_builder;

			reflected_type = type_builder;
			
			attributes = event_attr;
			this.name = name;
			my_event = ev;
			this.event_type = event_type;
		}
Exemple #16
0
        public override bool Define()
        {
            if (!base.Define ())
                return false;

            if (!TypeManager.IsDelegateType (MemberType)) {
                Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
            }

            parameters = ParametersCompiled.CreateFullyResolved (
                new Parameter (null, "value", Parameter.Modifier.NONE, null, Location), MemberType);

            if (!CheckBase ())
                return false;

            //
            // Now define the accessors
            //
            AddBuilder = Add.Define (Parent);
            if (AddBuilder == null)
                return false;

            RemoveBuilder = Remove.Define (Parent);
            if (RemoveBuilder == null)
                return false;

            EventBuilder = Parent.TypeBuilder.DefineEvent (Name, EventAttributes.None, MemberType.GetMetaInfo ());
            EventBuilder.SetAddOnMethod (AddBuilder);
            EventBuilder.SetRemoveOnMethod (RemoveBuilder);

            var spec = new EventSpec (Parent.Definition, this, MemberType, ModFlags, Add.Spec, Remove.Spec);

            Parent.MemberCache.AddMember (this, Name, spec);
            Parent.MemberCache.AddMember (this, AddBuilder.Name, Add.Spec);
            Parent.MemberCache.AddMember (this, RemoveBuilder.Name, Remove.Spec);

            return true;
        }
		internal EventOnTypeBuilderInst (MonoGenericClass instantiation, EventBuilder evt)
		{
			this.instantiation = instantiation;
			this.event_builder = evt;
		}
        static MethodBuilder BuildOnPropertyChanged(System.Reflection.Emit.TypeBuilder typeBuilder, EventBuilder eventBuilder, FieldInfo eventBackingField)
        {
            var raisePropertyChanged = typeBuilder.DefineMethod("OnPropertyChanged", MethodAttributes.Private, typeof(void), new[] {typeof(string)});
            var generator = raisePropertyChanged.GetILGenerator();
            var returnLabel = generator.DefineLabel();

            generator.DeclareLocal(typeof(PropertyChangedEventHandler));
            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldfld, eventBackingField);
            generator.Emit(OpCodes.Stloc_0);
            generator.Emit(OpCodes.Ldloc_0);
            generator.Emit(OpCodes.Ldnull);
            generator.Emit(OpCodes.Ceq);
            generator.Emit(OpCodes.Brtrue, returnLabel);

            generator.Emit(OpCodes.Ldloc_0);
            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldarg_1);
            generator.Emit(OpCodes.Newobj, _createEventArgs.Value);
            generator.Emit(OpCodes.Callvirt, Info<ProgressChangedEventHandler>.GetMethod("Invoke").First().MethodInfo);

            generator.MarkLabel(returnLabel);
            generator.Emit(OpCodes.Ret);

            eventBuilder.SetRaiseMethod(raisePropertyChanged);
            return raisePropertyChanged;
        }
Exemple #19
0
 private EventInfo get_event_info(EventBuilder eb)
 {
     throw new System.NotImplementedException();
 }
        /// <summary>
        /// Implementation of the add_PropertyChanged method. The lines bellow but in IL Code
        ///  MethodImplAttribute(MethodImplOptions.Synchronized)]
        ///  public void add_PropertyChanged(PropertyChangedEventHandler handler) 
        ///  {
        ///     PropertyChanged = (PropertyChangedEventHandler) Delegate.Combine(PropertyChanged, handler);
        ///  }
        ///  Disassembled il code:
        ///  IL_0000:  /* 02   |                  */ ldarg.0
        ///  IL_0001:  /* 02   |                  */ ldarg.0
        ///  IL_0002:  /* 7B   | (04)000001       */ ldfld      class [System/*23000002*/]System.ComponentModel.PropertyChangedEventHandler/*01000004*/ ReflectionEmit.SimplePropertyChangedImplementation/*02000003*/::PropertyChanged /* 04000001 */
        ///  IL_0007:  /* 03   |                  */ ldarg.1
        ///  IL_0008:  /* 28   | (0A)00003B       */ call       class [mscorlib/*23000001*/]System.Delegate/*01000032*/ [mscorlib/*23000001*/]System.Delegate/*01000032*/::Combine(class [mscorlib/*23000001*/]System.Delegate/*01000032*/,
        ///                                                    class [mscorlib/*23000001*/]System.Delegate/*01000032*/) /* 0A00003B */
        ///  IL_000d:  /* 74   | (01)000004       */ castclass  [System/*23000002*/]System.ComponentModel.PropertyChangedEventHandler/*01000004*/
        ///  IL_0012:  /* 7D   | (04)000001       */ stfld      class [System/*23000002*/]System.ComponentModel.PropertyChangedEventHandler/*01000004*/ ReflectionEmit.SimplePropertyChangedImplementation/*02000003*/::PropertyChanged /* 04000001 */
        ///  IL_0017:  /* 2A   |                  */ ret
        /// 
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="eventBuilder"></param>
        /// <param name="eventHandler"></param>
        /// <param name="operation"></param>
        private static void addOrRemoveMethod(TypeBuilder builder, EventBuilder eventBuilder, FieldBuilder eventHandler, bool operation)
        {
            string methodName;

            if (operation)
            {
                methodName = "add_PropertyChanged";
            }
            else
            {
                methodName = "remove_PropertyChanged";
            }

            // code  generation
            MethodBuilder method = builder.DefineMethod(
                             methodName,
                             MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.NewSlot | MethodAttributes.SpecialName |
                             MethodAttributes.HideBySig, typeof(void), new Type[] { typeof(PropertyChangedEventHandler) });

            ILGenerator mthdIL = method.GetILGenerator();

            mthdIL.Emit(OpCodes.Ldarg_0);
            mthdIL.Emit(OpCodes.Ldarg_0);
            mthdIL.Emit(OpCodes.Ldfld, eventHandler);
            mthdIL.Emit(OpCodes.Ldarg_1);
            if (operation)
            {
                mthdIL.Emit(OpCodes.Call, typeof(System.Delegate).GetMethod("Combine", new Type[] { typeof(Delegate), typeof(Delegate) }));
            }
            else
            {
                mthdIL.Emit(OpCodes.Call, typeof(System.Delegate).GetMethod("Remove", new Type[] { typeof(Delegate), typeof(Delegate) }));
            }

            mthdIL.Emit(OpCodes.Castclass, typeof(PropertyChangedEventHandler));
            mthdIL.Emit(OpCodes.Stfld, eventHandler);
            mthdIL.Emit(OpCodes.Ret);

            // attach the methods to the event
            if (operation)
            {
                eventBuilder.SetAddOnMethod(method);
            }
            else
            {
                eventBuilder.SetRemoveOnMethod(method);
            }
        }
Exemple #21
0
		public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
		{
			check_name ("name", name);
			if (eventtype == null)
				throw new ArgumentNullException ("type");
			check_not_created ();

			EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
			if (events != null) {
				EventBuilder[] new_events = new EventBuilder [events.Length+1];
				System.Array.Copy (events, new_events, events.Length);
				new_events [events.Length] = res;
				events = new_events;
			} else {
				events = new EventBuilder [1];
				events [0] = res;
			}
			return res;
		}
Exemple #22
0
		private extern EventInfo get_event_info (EventBuilder eb);
Exemple #23
0
		public override bool Define ()
		{
			if (!base.Define ())
				return false;

			if (!TypeManager.IsDelegateType (MemberType)) {
				Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
			}

			if (!CheckBase ())
				return false;

			//
			// Now define the accessors
			//
			var AddBuilder = Add.Define (Parent);
			if (AddBuilder == null)
				return false;

			var RemoveBuilder = remove.Define (Parent);
			if (RemoveBuilder == null)
				return false;

			EventBuilder = Parent.TypeBuilder.DefineEvent (Name, EventAttributes.None, MemberType.GetMetaInfo ());
			EventBuilder.SetAddOnMethod (AddBuilder);
			EventBuilder.SetRemoveOnMethod (RemoveBuilder);

			spec = new EventSpec (Parent.Definition, this, MemberType, ModFlags, Add.Spec, remove.Spec);

			Parent.MemberCache.AddMember (this, Name, spec);
			Parent.MemberCache.AddMember (this, AddBuilder.Name, Add.Spec);
			Parent.MemberCache.AddMember (this, RemoveBuilder.Name, remove.Spec);

			return true;
		}
Exemple #24
0
		public EvntInfo(EventBuilder ei)
		{
			_ei = ei;
		}
 internal EventOnTypeBuilderInst(TypeBuilderInstantiation instantiation, EventBuilder evt)
 {
     this.instantiation = instantiation;
     this.event_builder = evt;
 }
	public void TestCreation () {
		eb = tb.DefineEvent ("event2", EventAttributes.SpecialName, typeof (AnEvent));

		eb.SetRaiseMethod (mb);
		eb.SetAddOnMethod (mb);
		eb.SetRemoveOnMethod (mb);
		eb.AddOtherMethod (mb);
		eb.AddOtherMethod (mb);

		Type t = tb.CreateType ();

		MethodInfo mi = t.GetMethod ("OnAnEvent");

		EventInfo[] events = t.GetEvents ();
		AssertEquals (2, events.Length);

		{
			EventInfo ev = t.GetEvent ("event1");
			AssertEquals ("event1", ev.Name);
			AssertEquals (EventAttributes.None, ev.Attributes);
			AssertEquals (t, ev.DeclaringType);

			AssertEquals (typeof (AnEvent), ev.EventHandlerType);
			AssertEquals (true, ev.IsMulticast);
			AssertEquals (false, ev.IsSpecialName);

			AssertEquals (mi, ev.GetAddMethod ());
			AssertEquals (null, ev.GetRaiseMethod ());
			AssertEquals (mi, ev.GetRemoveMethod ());
		}

		{
			EventInfo ev = t.GetEvent ("event2");
			AssertEquals ("event2", ev.Name);
			AssertEquals (EventAttributes.SpecialName, ev.Attributes);
			AssertEquals (t, ev.DeclaringType);

			AssertEquals (typeof (AnEvent), ev.EventHandlerType);
			AssertEquals (true, ev.IsMulticast);
			AssertEquals (true, ev.IsSpecialName);

			AssertEquals (mi, ev.GetAddMethod ());
			AssertEquals (mi, ev.GetRaiseMethod ());
			AssertEquals (mi, ev.GetRemoveMethod ());
		}
	}