MissingMemberException Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Eccezione generata quando si tenta di accedere dinamicamente a un membro della classe che non esiste o che non è dichiarato come pubblico. Se un membro di una libreria di classi è stato rimosso o rinominato, ricompilare gli assembly che fanno riferimento a tale libreria.
public ref class MissingMemberException : MemberAccessException
public class MissingMemberException : MemberAccessException
[System.Serializable]
public class MissingMemberException : MemberAccessException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class MissingMemberException : MemberAccessException
type MissingMemberException = class
inherit MemberAccessException
type MissingMemberException = class
inherit MemberAccessException
interface ISerializable
[<System.Serializable>]
type MissingMemberException = class
inherit MemberAccessException
interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MissingMemberException = class
inherit MemberAccessException
interface ISerializable
Public Class MissingMemberException
Inherits MemberAccessException
- Ereditarietà
- Ereditarietà
- Derivato
- Attributi
- Implementazioni
Esempio
In questo esempio viene illustrato cosa accade se si tenta di usare la reflection per chiamare un metodo che non esiste e accedere a un campo che non esiste. L'applicazione viene ripristinata intercettando il MissingMethodException, MissingFieldExceptione 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.
Commenti
In genere viene generato un errore di compilazione se il codice tenta di accedere a un membro inesistente di una classe. MissingMemberException è progettato per gestire i casi in cui un campo o un metodo viene eliminato o rinominato in un assembly e la modifica non viene riflessa in un secondo assembly. In fase di esecuzione, MissingMemberException verrà generata quando il codice nel secondo assembly tenta di accedere al membro mancante nel primo assembly.
MissingMemberException è la classe di base per MissingFieldException e MissingMethodException. In generale è preferibile usare una delle classi derivate di MissingMemberException per indicare più precisamente la natura esatta dell'errore. Generare un'eccezione MissingMemberException se si è interessati solo all'acquisizione del caso generale di un errore del membro mancante.
MissingMemberException usa il COR_E_MISSINGMEMBER HRESULT, che ha il valore 0x80131512.
Per un elenco dei valori iniziali delle proprietà per un'istanza di MissingMemberException, consultare i costruttori di MissingMemberException.
Costruttori
| Nome | Descrizione |
|---|---|
| MissingMemberException() |
Inizializza una nuova istanza della classe MissingMemberException. |
| MissingMemberException(SerializationInfo, StreamingContext) |
Obsoleti.
Inizializza una nuova istanza della MissingMemberException classe con dati serializzati. |
| MissingMemberException(String, Exception) |
Inizializza una nuova istanza della MissingMemberException classe con un messaggio di errore specificato e un riferimento all'eccezione interna che rappresenta la causa radice di questa eccezione. |
| MissingMemberException(String, String) |
Inizializza una nuova istanza della MissingMemberException classe con il nome della classe e il nome del membro specificati. |
| MissingMemberException(String) |
Inizializza una nuova istanza della MissingMemberException classe con un messaggio di errore specificato. |
Campi
| Nome | Descrizione |
|---|---|
| ClassName |
Contiene il nome della classe del membro mancante. |
| MemberName |
Contiene il nome del membro mancante. |
| Signature |
Contiene la firma del membro mancante. |
Proprietà
| Nome | Descrizione |
|---|---|
| Data |
Ottiene una raccolta di coppie chiave/valore che forniscono informazioni aggiuntive definite dall'utente sull'eccezione. (Ereditato da Exception) |
| HelpLink |
Ottiene o imposta un collegamento al file della Guida associato a questa eccezione. (Ereditato da Exception) |
| HResult |
Ottiene o imposta HRESULT, valore numerico codificato assegnato a un'eccezione specifica. (Ereditato da Exception) |
| InnerException |
Ottiene l'istanza Exception che ha causato l'eccezione corrente. (Ereditato da Exception) |
| Message |
Ottiene la stringa di testo che mostra il nome della classe, il nome del membro e la firma del membro mancante. |
| Source |
Ottiene o imposta il nome dell'applicazione o dell'oggetto che causa l'errore. (Ereditato da Exception) |
| StackTrace |
Ottiene una rappresentazione di stringa dei fotogrammi immediati nello stack di chiamate. (Ereditato da Exception) |
| TargetSite |
Ottiene il metodo che genera l'eccezione corrente. (Ereditato da Exception) |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
| GetBaseException() |
Quando sottoposto a override in una classe derivata, restituisce l'oggetto Exception che rappresenta la causa radice di una o più eccezioni successive. (Ereditato da Exception) |
| GetHashCode() |
Funge da funzione hash predefinita. (Ereditato da Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Obsoleti.
Imposta l'oggetto SerializationInfo con il nome della classe, il nome del membro, la firma del membro mancante e informazioni aggiuntive sull'eccezione. |
| GetType() |
Ottiene il tipo di runtime dell'istanza corrente. (Ereditato da Exception) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Crea e restituisce una rappresentazione di stringa dell'eccezione corrente. (Ereditato da Exception) |
Eventi
| Nome | Descrizione |
|---|---|
| SerializeObjectState |
Obsoleti.
Si verifica quando viene serializzata un'eccezione per creare un oggetto stato dell'eccezione contenente dati serializzati sull'eccezione. (Ereditato da Exception) |