使用 NUnit+Reflection,在我的 ActiveX 控件上找不到任何事件
我正在使用 NUnit 和 C# 对老式 COM/ActiveX 控件进行单元测试。 我正在动态地执行所有操作,没有引用或编译时类型信息,因为我正在测试的控件主要是从 javascript 使用的 - 当然,它是动态执行所有操作的。我想连接一些事件处理程序并确保事件被正确触发,但我找不到事件!我使用 System.Activator.CreateInstance 动态构造控件的实例,如下所示(省略了一些细节;-):
Type T = Type.GetTypeFromCLSID(guid);
eztwain = System.Activator.CreateInstance(T);
EZTwainX = eztwain.GetType();
属性和方法的测试工作正常,如下所示:
EZTwainX.InvokeMember("Clear", BindingFlags.InvokeMethod, null, eztwain, null);
Assert.AreEqual(0, (int)EZTwainX.InvokeMember("ImageCount", BindingFlags.GetProperty, null, eztwain, null), "ImageCount");
以下全部失败,返回 null 或空数组或抛出“名称未找到”异常:
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
我只是假设(咳咳)我可以使用 Reflection API 做一些事情,相当于:
eztwain.AcquireDone += <event handler>;
但我不知道等效的东西是什么。 编辑:我相信该控件上的该事件,因为在 Javascript 中它可以工作(并捕获事件):
eztwain.attachEvent("AcquireDone", function() { me.onAcquireDone(); });
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(); });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看如何:处理 COM 源引发的事件
另外,请注意您可以使用的实用程序 (Ildasm.exe(IL 反汇编程序))来获取事件签名。
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.