MissingMethodException 类

试图动态访问不存在的方法时引发的异常。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class MissingMethodException
    Inherits MissingMemberException
    Implements ISerializable
用法
Dim instance As MissingMethodException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class MissingMethodException : MissingMemberException, ISerializable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class MissingMethodException : public MissingMemberException, ISerializable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class MissingMethodException extends MissingMemberException implements ISerializable
SerializableAttribute 
ComVisibleAttribute(true) 
public class MissingMethodException extends MissingMemberException implements ISerializable

备注

通常,如果代码试图访问类中不存在的方法,会生成编译错误。MissingMethodException 设计来处理这样一种情况:试图动态访问程序集中已重命名或已删除的方法,而该程序集不是用其强名称来引用。当相关程序集中的代码试图访问已被修改的程序集中已不存在的方法时,就引发 MissingMethodException

MissingMethodException 使用值为 0x80131513 的 HRESULT COR_E_MISSINGMETHOD。

有关 MissingMethodException 实例的初始属性值列表,请参见 MissingMethodException 构造函数。

示例

此示例演示在您尝试使用反射来调用不存在的方法和访问不存在的字段时所发生的情况。应用程序通过捕捉 MissingMethodExceptionMissingFieldExceptionMissingMemberException 来恢复。

using System;
using System.Reflection;

public class App 
{
    public static void Main() 
    { 
        
        try 
        {
            // Attempt to call a static DoSomething method defined in the App class.
            // However, because the App class does not define this field, 
            // a MissingFieldException is thrown.
            typeof(App).InvokeMember("DoSomething", BindingFlags.Static | 
                BindingFlags.InvokeMethod, null, null, null);
        }
        catch (MissingMethodException e) 
        {
            // Show the user that the DoSomething method cannot be called.
            Console.WriteLine("Unable to call the DoSomething method: {0}", e.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.
            typeof(App).InvokeMember("AField", BindingFlags.Static | BindingFlags.SetField, 
                null, null, new Object[] { 5 });
        }
        catch (MissingFieldException e) 
        {
         // Show the user that the AField field cannot be accessed.
         Console.WriteLine("Unable to access the AField field: {0}", e.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.
            typeof(App).InvokeMember("AnotherField", BindingFlags.Static | 
                BindingFlags.GetField, null, null, null);
        }        
        catch (MissingMemberException e) 
        {
         // 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}", e.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.
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 field, 
        // a MissingFieldException 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.

继承层次结构

System.Object
   System.Exception
     System.SystemException
       System.MemberAccessException
         System.MissingMemberException
          System.MissingMethodException

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

MissingMethodException 成员
System 命名空间
Exception 类

其他资源

处理和引发异常