关于System.MissingMethodException异常
什么是MissingMethodException
试图动态访问不存在的方法时引发的异常。
- 继承
说明
通常, 如果代码尝试访问不存在的类方法, 则会生成编译错误。 MissingMethodException旨在处理尝试动态访问未通过其强名称引用的程序集的已重命名或已删除方法的情况。 MissingMethodException当依赖程序集中的代码尝试访问已修改的程序集中缺少的方法时, 将引发。
HRESULT
MissingMethodException使用具有值0x80131513 的 HRESULT COR_E_MISSINGMETHOD。
示例
此示例演示当你尝试使用反射来调用不存在的方法并访问不存在的字段时会发生的情况。 应用程序通过捕获MissingMethodException、 MissingFieldException和MissingMemberException来恢复。
using namespace System; using namespace System::Reflection; ref class App { }; int main() { try { // Attempt to call a static DoSomething method defined in the App class. // However, because the App class does not define this method, // a MissingMethodException is thrown. App::typeid->InvokeMember("DoSomething", BindingFlags::Static | BindingFlags::InvokeMethod, nullptr, nullptr, nullptr); } catch (MissingMethodException^ ex) { // Show the user that the DoSomething method cannot be called. Console::WriteLine("Unable to call the DoSomething method: {0}", ex->Message); } try { // Attempt to access a static AField field defined in the App class. // However, because the App class does not define this field, // a MissingFieldException is thrown. App::typeid->InvokeMember("AField", BindingFlags::Static | BindingFlags::SetField, nullptr, nullptr, gcnew array<Object^>{5}); } catch (MissingFieldException^ ex) { // Show the user that the AField field cannot be accessed. Console::WriteLine("Unable to access the AField field: {0}", ex->Message); } try { // Attempt to access a static AnotherField field defined in the App class. // However, because the App class does not define this field, // a MissingFieldException is thrown. App::typeid->InvokeMember("AnotherField", BindingFlags::Static | BindingFlags::GetField, nullptr, nullptr, nullptr); } catch (MissingMemberException^ ex) { // Notice that this code is catching MissingMemberException which is the // base class of MissingMethodException and MissingFieldException. // Show the user that the AnotherField field cannot be accessed. Console::WriteLine("Unable to access the AnotherField field: {0}", ex->Message); } } // This code produces the following output. // // Unable to call the DoSomething method: Method 'App.DoSomething' not found. // Unable to access the AField field: Field 'App.AField' not found. // Unable to access the AnotherField field: Field 'App.AnotherField' not found.
为虫子生,为虫子死,为虫子奋斗一辈子