调用Type.InvokeMember()时出现MissingMethodException

调用Type.InvokeMember()时出现MissingMethodException经常是由于缺少必要的BindingFlags引起的。

在关于Type.InvokeMember   Method的地方有一大段例子代码。这段代码中有一个片断是“//Call   an   instance   method”,其中BindingFlags用的是BindingFlags.Public   |   BindingFlags.InvokeMethod,即:form1.GetType().InvokeMember( "ShowDialog ",   BindingFlags.Public   |   BindingFlags.InvokeMethod,   null,   form1,   null);

而事实上,如果当调用Form.ShowDialog()时只使用这两个flag,程序就会报一个你们所看到的Exception。但是如果再加上 BindingFlags.Instance,问题就马上解决了:form1.GetType().InvokeMember( "ShowDialog ",   BindingFlags.Public   |   BindingFlags.InvokeMethod   |   BindingFlags.Instance   ,   null,   form1,   null);

作者:Angelo Lee
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/yefengmeander/p/2887771.html