Angelo Lee's Blog
This is my kingdom .If i don't fight for it ,who will ?

调用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);

posted on 2010-12-28 13:51  Angelo Lee  阅读(367)  评论(0编辑  收藏  举报