MissingFieldException 类

定义

试图动态访问不存在的字段时引发的异常。The exception that is thrown when there is an attempt to dynamically access a field that does not exist. 如果类库中的某个字段已被移除或重命名,请重新编译引用该库的所有程序集。If a field in a class library has been removed or renamed, recompile any assemblies that reference that library.

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

示例

此示例演示当你尝试使用反射来调用不存在的方法并访问不存在的字段时会发生的情况。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 code attempts to access a nonexistent member of a class. MissingFieldException旨在处理尝试动态访问未通过其强名称引用的程序集的已重命名或已删除字段的情况。MissingFieldException is designed to handle cases where an attempt is made to dynamically access a renamed or deleted field of an assembly that is not referenced by its strong name. 当依赖程序集中的代码尝试访问已修改的程序集中缺少的字段时,将引发。MissingFieldExceptionThe MissingFieldException is thrown when code in a dependent assembly attempts to access a missing field in an assembly that was modified.

MissingFieldException使用具有值0x80131511 的 HRESULT COR_E_MISSINGFIELD。MissingFieldException uses the HRESULT COR_E_MISSINGFIELD, that has the value 0x80131511.

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

构造函数

MissingFieldException()

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

MissingFieldException(SerializationInfo, StreamingContext)

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

MissingFieldException(String)

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

MissingFieldException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 MissingFieldException 类的新实例。Initializes a new instance of the MissingFieldException class with a specified error message and a reference to the inner exception that is the cause of this exception.

MissingFieldException(String, String)

使用指定的类名称和字段名称初始化 MissingFieldException 类的新实例。Initializes a new instance of the MissingFieldException class with the specified class name and field name.

字段

ClassName

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

(继承自 MissingMemberException)
MemberName

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

(继承自 MissingMemberException)
Signature

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

(继承自 MissingMemberException)

属性

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 signature of the missing field, the class name, and the field name. 此属性是只读的。This property is read-only.

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.

(继承自 MissingMemberException)
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)

适用于

另请参阅