MissingMemberException-Klasse

Die Ausnahme, die bei einem Versuch ausgelöst wird, dynamisch auf einen nicht vorhandenen Klassenmember zuzugreifen.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class MissingMemberException
    Inherits MemberAccessException
    Implements ISerializable
'Usage
Dim instance As MissingMemberException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class MissingMemberException : MemberAccessException, ISerializable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class MissingMemberException : public MemberAccessException, ISerializable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class MissingMemberException extends MemberAccessException implements ISerializable
SerializableAttribute 
ComVisibleAttribute(true) 
public class MissingMemberException extends MemberAccessException implements ISerializable

Hinweise

In der Regel wird ein Kompilierungsfehler generiert, wenn Code versucht, auf einen nicht vorhandenen Member einer Klasse zuzugreifen. MissingMemberException ist dazu bestimmt, Fälle zu behandeln, in denen ein Feld oder eine Methode in einer Assembly gelöscht oder umbenannt und die Änderung in einer zweiten Assembly nicht wiedergegeben wird. Zur Laufzeit wird MissingMemberException ausgelöst, wenn Code in der zweiten Assembly versucht, auf den fehlenden Member in der ersten Assembly zuzugreifen.

MissingMemberException ist die Basisklasse für MissingFieldException und MissingMethodException. Im Allgemeinen empfiehlt es sich, die Art des vorliegenden Fehlers mithilfe einer der von MissingMemberException abgeleiteten Klassen genau anzugeben. Lösen Sie MissingMemberException aus, wenn Sie nur den allgemeinen Fall eines durch einen fehlenden Member verursachten Fehlers erfassen möchten.

MissingMemberException verwendet HRESULT COR_E_MISSINGMEMBER mit dem Wert 0x80131512.

Eine Liste der anfänglichen Eigenschaftenwerte für eine Instanz von MissingMemberException finden Sie unter MissingMemberException-Konstruktoren.

Beispiel

In diesem Beispiel wird veranschaulicht, was passiert, wenn Sie versuchen, mithilfe der Reflektion eine nicht vorhandene Methode aufzurufen oder auf ein nicht vorhandenes Feld zuzugreifen. Der Fehler wird von der Anwendung durch Abfangen der MissingMethodException, der MissingFieldException und der MissingMemberException behoben.

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.

Vererbungshierarchie

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

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

MissingMemberException-Member
System-Namespace
Exception-Klasse
MissingFieldException-Klasse
MissingMethodException

Weitere Ressourcen

Behandeln und Auslösen von Ausnahmen