public void AddEventHandler_ComObjectWithMultipleComEventInterfaceAttribute_ThrowsAmbiguousMatchException() { // C# doesn't let us apply multiple ComEventInterface values, so RefEmit is necessary. AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("assembly"), AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("module"); TypeBuilder typeBuilder = moduleBuilder.DefineType("name", TypeAttributes.Interface | TypeAttributes.Abstract); typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(ComEventInterfaceAttribute).GetConstructors()[0], new object[] { typeof(int), typeof(string) })); typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(ComEventInterfaceAttribute).GetConstructors()[0], new object[] { typeof(string), typeof(string) })); MethodBuilder addMethod = typeBuilder.DefineMethod("add_Event", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract, typeof(void), new Type[] { typeof(EventHandler) }); addMethod.GetILGenerator().Emit(OpCodes.Ret); EventBuilder eventBuilder = typeBuilder.DefineEvent("Event", EventAttributes.None, typeof(EventHandler)); eventBuilder.SetAddOnMethod(addMethod); var attribute = new ComAwareEventInfo(typeBuilder.CreateType(), "Event"); var target = new ComImportObject(); Delegate handler = new EventHandler(EventHandler); Assert.Throws <AmbiguousMatchException>(() => attribute.AddEventHandler(target, handler)); Assert.Throws <AmbiguousMatchException>(() => attribute.RemoveEventHandler(target, handler)); }
public void AddEventHandler_NoSuchSourceTypeEventInterface_ThrowsArgumentNullException() { var attribute = new ComAwareEventInfo(typeof(NoSuchSourceType), nameof(NoSuchSourceType.Event)); var target = new ComImportObject(); Delegate handler = new EventHandler(EventHandler); AssertExtensions.Throws <ArgumentNullException>("element", () => attribute.AddEventHandler(target, handler)); AssertExtensions.Throws <ArgumentNullException>("element", () => attribute.RemoveEventHandler(target, handler)); }
public void AddEventHandler_NullSourceTypeEventInterface_ThrowsNullReferenceException() { var attribute = new ComAwareEventInfo(typeof(NullSourceType), nameof(NullSourceType.Event)); var target = new ComImportObject(); Delegate handler = new EventHandler(EventHandler); Assert.Throws <NullReferenceException>(() => attribute.AddEventHandler(target, handler)); Assert.Throws <NullReferenceException>(() => attribute.RemoveEventHandler(target, handler)); }
public void AddEventHandler_TargetNotIConnectionIConnectionPointContainer_ThrowsInvalidCastException() { var attribute = new ComAwareEventInfo(typeof(DispAttributeInterface), nameof(DispAttributeInterface.Event)); var target = new ComImportObject(); Delegate handler = new EventHandler(EventHandler); Assert.Throws <InvalidCastException>(() => attribute.AddEventHandler(target, handler)); attribute.RemoveEventHandler(target, handler); }
public void AddEventHandler_ComObjectWithoutComEventInterfaceAttribute_ThrowsInvalidOperationException() { var attribute = new ComAwareEventInfo(typeof(NonComObject), nameof(NonComObject.Event)); var target = new ComImportObject(); Delegate handler = new EventHandler(EventHandler); Assert.Throws <InvalidOperationException>(() => attribute.AddEventHandler(target, handler)); Assert.Throws <InvalidOperationException>(() => attribute.RemoveEventHandler(target, handler)); }
public void AddEventHandler_DispIdAttribute_ThrowsPlatformNotSupportedException() { var attribute = new ComAwareEventInfo(typeof(DispAttributeInterface), nameof(DispAttributeInterface.Event)); var target = new ComObject(); Delegate handler = new EventHandler(EventHandler); Assert.Throws <PlatformNotSupportedException>(() => attribute.AddEventHandler(target, handler)); Assert.Throws <PlatformNotSupportedException>(() => attribute.RemoveEventHandler(target, handler)); }
public void AddEventHandler_NoDispIdAttribute_ThrowsInvalidOperationException() { var attribute = new ComAwareEventInfo(typeof(NoDispAttributeInterface), nameof(NoDispAttributeInterface.Event)); var target = new ComObject(); Delegate handler = new EventHandler(EventHandler); Assert.Throws <InvalidOperationException>(() => attribute.AddEventHandler(target, handler)); Assert.Throws <InvalidOperationException>(() => attribute.RemoveEventHandler(target, handler)); }
public void Methods_NoSuchEvent_ThrowsNullReferenceException() { var attribute = new ComAwareEventInfo(typeof(NonComObject), string.Empty); Assert.Throws <NullReferenceException>(() => attribute.AddEventHandler(new object(), new EventHandler(EventHandler))); Assert.Throws <NullReferenceException>(() => attribute.RemoveEventHandler(new object(), new EventHandler(EventHandler))); Assert.Throws <NullReferenceException>(() => attribute.GetAddMethod(false)); Assert.Throws <NullReferenceException>(() => attribute.GetRaiseMethod(false)); Assert.Throws <NullReferenceException>(() => attribute.GetRemoveMethod(false)); Assert.Throws <NullReferenceException>(() => attribute.GetCustomAttributes(typeof(ComVisibleAttribute), false)); Assert.Throws <NullReferenceException>(() => attribute.GetCustomAttributes(false)); Assert.Throws <NullReferenceException>(() => attribute.IsDefined(typeof(ComVisibleAttribute), false)); }
public void AddEventHandler_NonCom_Success() { var attribute = new ComAwareEventInfo(typeof(NonComObject), nameof(NonComObject.Event)); var target = new NonComObject(); Delegate handler = new EventHandler(EventHandler); attribute.AddEventHandler(target, handler); target.Raise(1); Assert.True(CalledEventHandler); CalledEventHandler = false; attribute.RemoveEventHandler(target, handler); Assert.False(CalledEventHandler); }
// The ComAwareEventInfo is used by the compiler when PIAs // containing COM Events are embedded. static void Validate_COMEventViaComAwareEventInfo() { Console.WriteLine($"{nameof(Validate_COMEventViaComAwareEventInfo)}..."); var eventTesting = (EventTesting) new EventTestingClass(); // Verify event handler subscription // Add event var comAwareEventInfo = new ComAwareEventInfo(typeof(TestingEvents_Event), nameof(TestingEvents_Event.OnEvent)); var handler = new TestingEvents_OnEventEventHandler(OnEventEventHandler); comAwareEventInfo.AddEventHandler(eventTesting, handler); bool eventFired = false; string message = string.Empty; eventTesting.FireEvent(); Assert.IsTrue(eventFired, "Event didn't fire"); Assert.AreEqual(nameof(EventTesting.FireEvent), message, "Event message is incorrect"); comAwareEventInfo.RemoveEventHandler(eventTesting, handler); // Verify event handler removed eventFired = false; eventTesting.FireEvent(); Assert.IsFalse(eventFired, "Event shouldn't fire"); void OnEventEventHandler(string msg) { eventFired = true; message = msg; } }
public void AddEventHandler_NullTarget_ThrowsArgumentNullException() { var attribute = new ComAwareEventInfo(typeof(NonComObject), nameof(NonComObject.Event)); AssertExtensions.Throws <ArgumentNullException>("o", () => attribute.AddEventHandler(null, new EventHandler(EventHandler))); }