ArrayTypeMismatchException 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 ett försök görs att lagra ett element av fel typ i en matris.
public ref class ArrayTypeMismatchException : Exception
public ref class ArrayTypeMismatchException : SystemException
public class ArrayTypeMismatchException : Exception
public class ArrayTypeMismatchException : SystemException
[System.Serializable]
public class ArrayTypeMismatchException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArrayTypeMismatchException : SystemException
type ArrayTypeMismatchException = class
inherit Exception
type ArrayTypeMismatchException = class
inherit SystemException
[<System.Serializable>]
type ArrayTypeMismatchException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArrayTypeMismatchException = class
inherit SystemException
Public Class ArrayTypeMismatchException
Inherits Exception
Public Class ArrayTypeMismatchException
Inherits SystemException
- Arv
- Arv
- Attribut
Exempel
I följande kodexempel visas scenarier där ArrayTypeMismatchException genereras.
using System;
namespace ArrayTypeMismatch
{
class Class1
{
static void Main(string[] args)
{
string[] names = {"Dog", "Cat", "Fish"};
Object[] objs = (Object[]) names;
try
{
objs[2] = "Mouse";
foreach (object animalName in objs)
{
System.Console.WriteLine(animalName);
}
}
catch (System.ArrayTypeMismatchException)
{
// Not reached; "Mouse" is of the correct type.
System.Console.WriteLine("Exception Thrown.");
}
try
{
Object obj = (Object) 13;
objs[2] = obj;
}
catch (System.ArrayTypeMismatchException)
{
// Always reached, 13 is not a string.
System.Console.WriteLine(
"New element is not of the correct type.");
}
// Set objs to an array of objects instead of
// an array of strings.
objs = new Object[3];
try
{
objs[0] = (Object) "Turtle";
objs[1] = (Object) 12;
objs[2] = (Object) 2.341;
foreach (object element in objs)
{
System.Console.WriteLine(element);
}
}
catch (System.ArrayTypeMismatchException)
{
// ArrayTypeMismatchException is not thrown this time.
System.Console.WriteLine("Exception Thrown.");
}
}
}
}
open System
[<EntryPoint>]
let main _ =
let names = [| "Dog"; "Cat"; "Fish" |]
let objs = box names :?> obj[]
try
objs[2] <- "Mouse"
for animalName in objs do
printfn $"{animalName}"
with :? ArrayTypeMismatchException ->
// Not reached; "Mouse" is of the correct type.
printfn "Exception Thrown."
try
let obj = 13 :> obj
objs[2] <- obj
with :? ArrayTypeMismatchException ->
// Always reached, 13 is not a string.
printfn "New element is not of the correct type."
// Shadow objs as an array of objects instead of an array of strings.
let objs: obj[] = [| "Turtle"; 12; 2.341 |]
try
for element in objs do
printfn $"{element}"
with :? ArrayTypeMismatchException ->
// ArrayTypeMismatchException is not thrown this time.
printfn "Exception Thrown."
0
Option Explicit On
Option Strict On
Namespace ArrayTypeMismatch
Class Class1
Public Shared Sub Main(ByVal args() As String)
Dim names As String() = {"Dog", "Cat", "Fish"}
Dim objs As System.Object() = CType(names, System.Object())
Try
objs(2) = "Mouse"
Dim animalName As Object
For Each animalName In objs
System.Console.WriteLine(animalName)
Next animalName
Catch exp As System.ArrayTypeMismatchException
' Not reached, "Mouse" is of the correct type.
System.Console.WriteLine("Exception Thrown.")
End Try
Try
Dim obj As System.Object
obj = CType(13, System.Object)
objs(2) = obj
Catch exp As System.ArrayTypeMismatchException
' Always reached, 13 is not a string.
System.Console.WriteLine("New element is not of the correct type.")
End Try
' Set objs to an array of objects instead of an array of strings.
Dim objs2(3) As System.Object
Try
objs2(0) = "Turtle"
objs2(1) = 12
objs2(2) = 2.341
Dim element As Object
For Each element In objs2
System.Console.WriteLine(element)
Next element
Catch exp As System.ArrayTypeMismatchException
' ArrayTypeMismatchException is not thrown this time.
System.Console.WriteLine("Exception Thrown.")
End Try
End Sub
End Class
End Namespace
Kommentarer
ArrayTypeMismatchException genereras när systemet inte kan konvertera elementet till den typ som deklarerats för matrisen. Det går till exempel inte att lagra ett element av typen String i en Int32 matris eftersom konvertering mellan dessa typer inte stöds. Det är i allmänhet onödigt att program genererar det här undantaget.
Följande instruktioner för Microsoft mellanliggande språk (MSIL) genererar ArrayTypeMismatchException:
ldelem.<type>ldelemastelem.<type>
ArrayTypeMismatchException använder HRESULT-COR_E_ARRAYTYPEMISMATCH, som har värdet 0x80131503.
För en lista över inledande egenskapsvärden för en instans av ArrayTypeMismatchException, se i ArrayTypeMismatchException-konstruktorn.
Konstruktorer
| Name | Description |
|---|---|
| ArrayTypeMismatchException() |
Initierar en ny instans av ArrayTypeMismatchException klassen. |
| ArrayTypeMismatchException(SerializationInfo, StreamingContext) |
Föråldrad.
Initierar en ny instans av ArrayTypeMismatchException klassen med serialiserade data. |
| ArrayTypeMismatchException(String, Exception) |
Initierar en ny instans av ArrayTypeMismatchException klassen med ett angivet felmeddelande och en referens till det inre undantaget som är orsaken till det här undantaget. |
| ArrayTypeMismatchException(String) |
Initierar en ny instans av ArrayTypeMismatchException klassen med ett angivet felmeddelande. |
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 ett meddelande som beskriver det aktuella undantaget. (Ärvd från Exception) |
| 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.
När åsidosättas i en härledd klass anger du SerializationInfo med information om undantaget. (Ärvd från Exception) |
| 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) |