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_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 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));
        }
Exemple #8
0
        public void Properties_NoSuchEvent_ThrowsNullReferenceException()
        {
            var attribute = new ComAwareEventInfo(typeof(NonComObject), string.Empty);

            Assert.Throws <NullReferenceException>(() => attribute.Attributes);
            Assert.Throws <NullReferenceException>(() => attribute.DeclaringType);
            Assert.Throws <NullReferenceException>(() => attribute.Name);
            Assert.Throws <NullReferenceException>(() => attribute.ReflectedType);
        }
Exemple #9
0
        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));
        }
Exemple #10
0
        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);
        }
Exemple #11
0
        public void Ctor_Type_EventName()
        {
            EventInfo expectedEvent = typeof(NonComObject).GetEvent(nameof(NonComObject.Event));
            var       attribute     = new ComAwareEventInfo(typeof(NonComObject), nameof(NonComObject.Event));

            Assert.Equal(expectedEvent.Attributes, attribute.Attributes);
            Assert.Equal(expectedEvent.DeclaringType, attribute.DeclaringType);
            Assert.Equal(expectedEvent.Name, attribute.Name);
            Assert.Equal(expectedEvent.ReflectedType, attribute.ReflectedType);

            Assert.Equal(expectedEvent.GetAddMethod(), attribute.GetAddMethod());
            Assert.Equal(expectedEvent.GetRaiseMethod(), attribute.GetRaiseMethod());
            Assert.Equal(expectedEvent.GetRemoveMethod(), attribute.GetRemoveMethod());

            Assert.Equal(expectedEvent.GetCustomAttributes(typeof(ExcludeFromCodeCoverageAttribute), true), attribute.GetCustomAttributes(typeof(ExcludeFromCodeCoverageAttribute), true));
            Assert.Equal(expectedEvent.GetCustomAttributes(true), attribute.GetCustomAttributes(true));
            Assert.Equal(expectedEvent.IsDefined(typeof(ExcludeFromCodeCoverageAttribute)), attribute.IsDefined(typeof(ExcludeFromCodeCoverageAttribute)));
        }
Exemple #12
0
        // 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;
            }
        }
Exemple #13
0
        public void AddEventHandler_NullTarget_ThrowsArgumentNullException()
        {
            var attribute = new ComAwareEventInfo(typeof(NonComObject), nameof(NonComObject.Event));

            AssertExtensions.Throws <ArgumentNullException>("o", () => attribute.AddEventHandler(null, new EventHandler(EventHandler)));
        }