MissingMethodException Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Undantaget som utlöses när det görs ett försök att dynamiskt komma åt en metod som inte finns.
public ref class MissingMethodException : MissingMemberException
public class MissingMethodException : MissingMemberException
[System.Serializable]
public class MissingMethodException : MissingMemberException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class MissingMethodException : MissingMemberException
type MissingMethodException = class
inherit MissingMemberException
type MissingMethodException = class
inherit MissingMemberException
interface ISerializable
[<System.Serializable>]
type MissingMethodException = class
inherit MissingMemberException
interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MissingMethodException = class
inherit MissingMemberException
interface ISerializable
Public Class MissingMethodException
Inherits MissingMemberException
- Arv
- Arv
-
MissingMethodException
- Attribut
- Implementeringar
Exempel
Det här exemplet visar vad som händer om du försöker använda reflektion för att anropa en metod som inte finns och få åtkomst till ett fält som inte finns. Programmet återställs genom att MissingMethodExceptionfånga , MissingFieldExceptionoch 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.
Kommentarer
Normalt genereras ett kompileringsfel om kod försöker komma åt en icke-existerande metod för en klass. MissingMethodException är utformad för att hantera fall där ett försök görs att dynamiskt komma åt en omdöpt eller borttagen metod för en sammansättning som inte refereras till med dess starka namn. MissingMethodException genereras när kod i en beroende sammansättning försöker komma åt en metod som saknas i en sammansättning som har ändrats.
MissingMethodException använder HRESULT-COR_E_MISSINGMETHOD som har värdet 0x80131513.
För en lista över inledande egenskapsvärden för en instans av MissingMethodException, se i MissingMethodException-konstruktorn.
Den exakta tidpunkten för när statiskt refererade metoder läses in är ospecificerad. Det här undantaget kan genereras innan metoden som refererar till den saknade metoden börjar köras.
Note
Det här undantaget ingår inte i .NET för Windows Store-appar eller Portable Class Library, men det genereras av vissa medlemmar. Om du vill fånga undantaget i det fallet skriver du en catch instruktion för MissingMemberException i stället.
Konstruktorer
| Name | Description |
|---|---|
| MissingMethodException() |
Initierar en ny instans av MissingMethodException klassen. |
| MissingMethodException(SerializationInfo, StreamingContext) |
Föråldrad.
Initierar en ny instans av MissingMethodException klassen med serialiserade data. |
| MissingMethodException(String, Exception) |
Initierar en ny instans av MissingMethodException klassen med ett angivet felmeddelande och en referens till det inre undantaget som är orsaken till det här undantaget. |
| MissingMethodException(String, String) |
Initierar en ny instans av MissingMethodException klassen med det angivna klassnamnet och metodnamnet. |
| MissingMethodException(String) |
Initierar en ny instans av MissingMethodException klassen med ett angivet felmeddelande. |
Fält
| Name | Description |
|---|---|
| ClassName |
Innehåller klassnamnet för den saknade medlemmen. (Ärvd från MissingMemberException) |
| MemberName |
Innehåller namnet på den medlem som saknas. (Ärvd från MissingMemberException) |
| Signature |
Innehåller signaturen för den saknade medlemmen. (Ärvd från MissingMemberException) |
Egenskaper
| Name | Description |
|---|---|
| Data |
Hämtar en samling nyckel/värde-par som ger ytterligare användardefinierad information om undantaget. (Ärvd från Exception) |
| HelpLink |
Hämtar eller anger en länk till hjälpfilen som är associerad med det här undantaget. (Ärvd från Exception) |
| HResult |
Hämtar eller anger HRESULT, ett kodat numeriskt värde som har tilldelats ett specifikt undantag. (Ärvd från Exception) |
| InnerException |
Hämtar den Exception instans som orsakade det aktuella undantaget. (Ärvd från Exception) |
| Message |
Hämtar textsträngen som visar klassnamnet, metodnamnet och signaturen för den saknade metoden. Den här egenskapen är skrivskyddad. |
| Source |
Hämtar eller anger namnet på programmet eller objektet som orsakar felet. (Ärvd från Exception) |
| StackTrace |
Hämtar en strängrepresentation av de omedelbara ramarna i anropsstacken. (Ärvd från Exception) |
| TargetSite |
Hämtar den metod som utlöser det aktuella undantaget. (Ärvd från Exception) |
Metoder
| Name | Description |
|---|---|
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| GetBaseException() |
När den åsidosätts i en härledd klass returnerar den Exception som är rotorsaken till ett eller flera efterföljande undantag. (Ärvd från Exception) |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Föråldrad.
SerializationInfo Anger objektet med klassnamnet, medlemsnamnet, signaturen för den saknade medlemmen och ytterligare undantagsinformation. (Ärvd från MissingMemberException) |
| GetType() |
Hämtar körningstypen för den aktuella instansen. (Ärvd från Exception) |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| ToString() |
Skapar och returnerar en strängrepresentation av det aktuella undantaget. (Ärvd från Exception) |
Händelser
| Name | Description |
|---|---|
| SerializeObjectState |
Föråldrad.
Inträffar när ett undantag serialiseras för att skapa ett undantagstillståndsobjekt som innehåller serialiserade data om undantaget. (Ärvd från Exception) |