MissingFieldException Classe

Définition

Exception levée lorsqu’il existe une tentative d’accès dynamique à un champ qui n’existe pas. Si un champ d’une bibliothèque de classes a été supprimé ou renommé, recompilez tous les assemblys qui référencent cette bibliothèque.

public ref class MissingFieldException : MissingMemberException
public class MissingFieldException : MissingMemberException
[System.Serializable]
public class MissingFieldException : MissingMemberException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class MissingFieldException : MissingMemberException
type MissingFieldException = class
    inherit MissingMemberException
[<System.Serializable>]
type MissingFieldException = class
    inherit MissingMemberException
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MissingFieldException = class
    inherit MissingMemberException
    interface ISerializable
type MissingFieldException = class
    inherit MissingMemberException
    interface ISerializable
Public Class MissingFieldException
Inherits MissingMemberException
Héritage
Héritage
Attributs
Implémente

Exemples

Cet exemple montre ce qui se passe si vous tentez d’utiliser la réflexion pour appeler une méthode qui n’existe pas et accéder à un champ qui n’existe pas. L’application récupère en interceptant le MissingMethodException, MissingFieldExceptionet 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.
open System
open System.Reflection

type App = class end

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)
    |> ignore
with :? MissingMethodException as e ->
    // Show the user that the DoSomething method cannot be called.
    printfn $"Unable to call the DoSomething method: {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, [| box 5 |])
    |> ignore
with :? MissingFieldException as e ->
    // Show the user that the AField field cannot be accessed.
    printfn $"Unable to access the AField field: {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)
    |> ignore
with :? MissingMemberException as 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.
    printfn $"Unable to access the AnotherField field: {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.

Remarques

Normalement, une erreur de compilation est générée si le code tente d’accéder à un membre inexistant d’une classe. MissingFieldException est conçu pour gérer les cas où une tentative d’accès dynamique à un champ renommé ou supprimé d’un assembly qui n’est pas référencé par son nom fort. Le MissingFieldException code d’un assembly dépendant tente d’accéder à un champ manquant dans un assembly modifié.

MissingFieldException utilise le COR_E_MISSINGFIELD HRESULT, qui a la valeur 0x80131511.

Pour obtenir la liste des valeurs de propriété initiales d’une instance de MissingFieldException, consultez les MissingFieldException constructeurs.

Constructeurs

Nom Description
MissingFieldException()

Initialise une nouvelle instance de la classe MissingFieldException.

MissingFieldException(SerializationInfo, StreamingContext)

Initialise une nouvelle instance de la classe MissingFieldException avec des données sérialisées.

MissingFieldException(String, Exception)

Initialise une nouvelle instance de la MissingFieldException classe avec un message d’erreur spécifié et une référence à l’exception interne qui est la cause de cette exception.

MissingFieldException(String, String)

Initialise une nouvelle instance de la MissingFieldException classe avec le nom de classe et le nom de champ spécifiés.

MissingFieldException(String)

Initialise une nouvelle instance de la MissingFieldException classe avec un message d’erreur spécifié.

Champs

Nom Description
ClassName

Contient le nom de classe du membre manquant.

(Hérité de MissingMemberException)
MemberName

Contient le nom du membre manquant.

(Hérité de MissingMemberException)
Signature

Contient la signature du membre manquant.

(Hérité de MissingMemberException)

Propriétés

Nom Description
Data

Obtient une collection de paires clé/valeur qui fournissent des informations supplémentaires définies par l’utilisateur sur l’exception.

(Hérité de Exception)
HelpLink

Obtient ou définit un lien vers le fichier d’aide associé à cette exception.

(Hérité de Exception)
HResult

Obtient ou définit HRESULT, valeur numérique codée affectée à une exception spécifique.

(Hérité de Exception)
InnerException

Obtient l’instance Exception qui a provoqué l’exception actuelle.

(Hérité de Exception)
Message

Obtient la chaîne de texte montrant la signature du champ manquant, le nom de la classe et le nom du champ. Cette propriété est en lecture seule.

Source

Obtient ou définit le nom de l’application ou de l’objet qui provoque l’erreur.

(Hérité de Exception)
StackTrace

Obtient une représentation sous forme de chaîne des images immédiates sur la pile des appels.

(Hérité de Exception)
TargetSite

Obtient la méthode qui lève l’exception actuelle.

(Hérité de Exception)

Méthodes

Nom Description
Equals(Object)

Détermine si l’objet spécifié est égal à l’objet actuel.

(Hérité de Object)
GetBaseException()

En cas de substitution dans une classe dérivée, retourne la Exception qui est la cause racine d’une ou plusieurs exceptions ultérieures.

(Hérité de Exception)
GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetObjectData(SerializationInfo, StreamingContext)

Définit l’objet avec le SerializationInfo nom de la classe, le nom du membre, la signature du membre manquant et des informations d’exception supplémentaires.

(Hérité de MissingMemberException)
GetType()

Obtient le type d’exécution de l’instance actuelle.

(Hérité de Exception)
MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
ToString()

Crée et retourne une représentation sous forme de chaîne de l’exception actuelle.

(Hérité de Exception)

Événements

Nom Description
SerializeObjectState

Se produit lorsqu’une exception est sérialisée pour créer un objet d’état d’exception qui contient des données sérialisées sur l’exception.

(Hérité de Exception)

S’applique à

Voir aussi