4
votes

I'm unit-testing an old-school COM/ActiveX control using NUnit and C#. I'm doing everything dynamically, no References or compile-time type information, because the control I'm testing is used primarily from javascript - which, of course, does everything dynamically. I want to hook up some event handlers and make sure events are being fired appropriately, but I can't find the events! I dynamically construct an instance of the control using System.Activator.CreateInstance, like this (some details omitted ;-):

Type T = Type.GetTypeFromCLSID(guid);
eztwain = System.Activator.CreateInstance(T);
EZTwainX = eztwain.GetType();

Tests of properties and methods work fine, like so:

EZTwainX.InvokeMember("Clear", BindingFlags.InvokeMethod, null, eztwain, null);
Assert.AreEqual(0, (int)EZTwainX.InvokeMember("ImageCount", BindingFlags.GetProperty, null, eztwain, null), "ImageCount");

The following all fail, returning null or an empty array or throwing a 'name not found' exception as appropriate:

EZTwainX.GetEvent("AcquireDone");           // returns null
EZTwainX.GetEvents();                       // returns empty array
EZTwainX.GetEvents(BindingFlags.Public |    // returns empty array
            BindingFlags.NonPublic |
            BindingFlags.Static | BindingFlags.Instance);
PropertyInfo propertyInfo = EZTwainX.GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);      // returns null
MemberInfo[] mimfo = EZTwainX.GetMember("AcquireDone", MemberTypes.Event, BindingFlags.Public | BindingFlags.NonPublic);      // returns empty array

I just assumed (ahem) that I could do something using the Reflection API, equivalent to:

eztwain.AcquireDone += <event handler>;

but I can't figure out what that equivalent thing is. EDIT: I believe in that event on that control because in Javascript this works (and catches events):

eztwain.attachEvent("AcquireDone", function() { me.onAcquireDone(); });
1

1 Answers

0
votes

Have a look at How to: Handle Events Raised by a COM Source

Also, Notice the utility you can use (Ildasm.exe (IL Disassembler)) to get event signatures.