System.MissingMethodException 类

方法描述

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

语法定义(C# System.MissingMethodException 类 的用法)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class MissingMethodException : MissingMemberException, 
	ISerializable

构造函数

构造函数名称 构造函数描述
MissingMethodException() 初始化 MissingMethodException 类的新实例。
MissingMethodException(String) 使用指定错误信息初始化 MissingMethodException 类的新实例。
MissingMethodException(SerializationInfo, StreamingContext) 用序列化数据初始化 MissingMethodException 类的新实例。
MissingMethodException(String, Exception) 使用指定错误信息和对作为此异常原因的内部异常的引用来初始化 MissingMethodException 类的新实例。
MissingMethodException(String, String) 使用指定的类名称和方法名称初始化 MissingMethodException 类的新实例。

成员/方法

方法名称 方法描述
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetBaseException 当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。 (继承自 Exception。)
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetObjectData 用类名、成员名、缺少成员的签名和其他异常信息设置 SerializationInfo 对象。 (继承自 MissingMemberException。)
GetType 获取当前实例的运行时类型。 (继承自 Exception。) 在 XNA Framework3.0GetType()。
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ToString 创建并返回当前异常的字符串表示形式。 (继承自 Exception。)

提示和注释

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

MissingMethodException 使用值为 0x80131513 的 HRESULT COR_E_MISSINGMETHOD。

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

System.MissingMethodException 类例子

应用程序通过捕捉 MissingMethodException、MissingFieldException 和 MissingMemberException 来恢复。

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 method,
            // a MissingMethodException 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 example 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

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

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

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。