MissingMemberException 类

定义

当尝试动态访问一个不存在或未声明为 public 的类成员时引发的异常。The exception that is thrown when there is an attempt to dynamically access a class member that does not exist or that is not declared as public. 如果类库中的某个成员已移除或重命名,请重新编译引用该库的所有程序集。If a member in a class library has been removed or renamed, recompile any assemblies that reference that library.

public ref class MissingMemberException : MemberAccessException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class MissingMemberException : MemberAccessException
type MissingMemberException = class
    inherit MemberAccessException
    interface ISerializable
Public Class MissingMemberException
Inherits MemberAccessException
继承
派生
属性
实现

示例

此示例演示当你尝试使用反射来调用不存在的方法并访问不存在的字段时会发生的情况。This example shows what happens if you attempt to use reflection to call a method that does not exist and access a field that does not exist. 应用程序通过捕获MissingMethodExceptionMissingFieldExceptionMissingMemberException来恢复。The application recovers by catching the MissingMethodException, MissingFieldException, and 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.
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.
Imports System.Reflection

Public Class App
    Public Shared Sub 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.
            GetType(App).InvokeMember("DoSomething", BindingFlags.Static Or BindingFlags.InvokeMethod, _
                                       Nothing, Nothing, Nothing)
        Catch e As MissingMethodException
            ' Show the user that the DoSomething method cannot be called.
            Console.WriteLine("Unable to call the DoSomething method: {0}", e.Message)
        End Try
        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.
            GetType(App).InvokeMember("AField", BindingFlags.Static Or BindingFlags.SetField, _
                                       Nothing, Nothing, New [Object]() {5})
        Catch e As MissingFieldException
            ' Show the user that the AField field cannot be accessed.
            Console.WriteLine("Unable to access the AField field: {0}", e.Message)
        End Try
        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.
            GetType(App).InvokeMember("AnotherField", BindingFlags.Static Or BindingFlags.GetField, _
                                       Nothing, Nothing, Nothing)
        Catch e As MissingMemberException
            ' 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)
        End Try
    End Sub 
End Class 
' 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.

注解

通常, 如果代码尝试访问不存在的类成员, 则会生成编译错误。Normally a compilation error is generated if the code attempts to access a nonexistent member of a class. MissingMemberException设计用于处理在一个程序集中删除或重命名某一字段或方法, 并且该更改未反映在另一个程序集中的情况。MissingMemberException is designed to handle cases where a field or a method is deleted or renamed in one assembly and the change is not reflected in a second assembly. 在运行时MissingMemberException , 当第二个程序集中的代码尝试访问第一个程序集中缺少的成员时, 将引发。At runtime, MissingMemberException will be thrown when the code in the second assembly attempts to access the missing member in the first assembly.

MissingMemberExceptionMissingFieldExceptionMissingMethodException的基类。MissingMemberException is the base class for MissingFieldException and MissingMethodException. 通常, 更好的方法是使用的一个派生类MissingMemberException , 以便更准确地指示错误的确切性质。In general it is better to use one of the derived classes of MissingMemberException to more precisely indicate the exact nature of the error. MissingMemberException如果你只想捕获缺少成员错误的一般事例, 则引发。Throw a MissingMemberException if you are only interested in capturing the general case of a missing member error.

MissingMemberException使用具有值0x80131512 的 HRESULT COR_E_MISSINGMEMBER。MissingMemberException uses the HRESULT COR_E_MISSINGMEMBER, that has the value 0x80131512.

有关实例的初始属性值的列表MissingMemberException,请参阅MissingMemberException构造函数。For a list of initial property values for an instance of MissingMemberException, see the MissingMemberException constructors.

构造函数

MissingMemberException()

初始化 MissingMemberException 类的新实例。Initializes a new instance of the MissingMemberException class.

MissingMemberException(SerializationInfo, StreamingContext)

用序列化数据初始化 MissingMemberException 类的新实例。Initializes a new instance of the MissingMemberException class with serialized data.

MissingMemberException(String)

用指定的错误消息初始化 MissingMemberException 类的新实例。Initializes a new instance of the MissingMemberException class with a specified error message.

MissingMemberException(String, Exception)

使用指定错误消息以及对内部异常(为该异常的根源)的引用来初始化 MissingMemberException 类的新实例。Initializes a new instance of the MissingMemberException class with a specified error message and a reference to the inner exception that is the root cause of this exception.

MissingMemberException(String, String)

使用指定的类名称和成员名称初始化 MissingMemberException 类的新实例。Initializes a new instance of the MissingMemberException class with the specified class name and member name.

字段

ClassName

保留缺少成员的类名。Holds the class name of the missing member.

MemberName

保留缺少成员的名称。Holds the name of the missing member.

Signature

保留缺少成员的签名。Holds the signature of the missing member.

属性

Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(继承自 Exception)
Message

获取显示类名、成员名和缺少成员签名的文本字符串。Gets the text string showing the class name, the member name, and the signature of the missing member.

Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(继承自 Exception)
TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(继承自 Exception)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(继承自 Exception)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

用类名、成员名、缺少成员的签名和其他异常信息设置 SerializationInfo 对象。Sets the SerializationInfo object with the class name, the member name, the signature of the missing member, and additional exception information.

GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(继承自 Exception)

事件

SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(继承自 Exception)

适用于

另请参阅