我有使用object.GetType()。InvokeMember()的方法:
private void InvokeComObject(object callback, object parameter)
{
callback.GetType().InvokeMember(
name: "[DispID=0]",
invokeAttr: BindingFlags.Instance | BindingFlags.InvokeMethod,
binder: null,
target: callback,
args: new Object[] { parameter == null ? "" : parameter });
}
和它的单元测试:
[Test]
public void should_execute_action()
{
var executor = CreateExecutor();
Action<int> callback = Substitute.For<Action<int>>();
executor.ExecuteAction("name", "value", callback);
callback.Received(1).GetType().InvokeMember(Arg.Is<string>(r => r == "[DispID=0]"),
Arg.Is<BindingFlags>(r => r.HasFlag(BindingFlags.Instance) && r.HasFlag(BindingFlags.InvokeMethod)),
Arg.Is<Binder>(r => r == null), Arg.Is<object>(r => ReferenceEquals(r, callback)),
Arg.Is<Object[]>(r => r == new Object[] { 2 }));
}
executor.ExecuteAction inside使用“callback”执行InvokeComObject,我将其传递给此方法。此方法失败,“callback.Received(1).GetType()。InvokeMember”异常: “System.ArgumentException:必须指定描述所需调用操作的绑定标志(BindingFlags.InvokeMethod CreateInstance GetField SetField GetProperty SetProperty)。” 我怎样才能使这个测试工作?