ArrayTypeMismatchException Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Exception levée lorsqu’une tentative est effectuée pour stocker un élément du type incorrect dans un tableau.
public ref class ArrayTypeMismatchException : Exception
public ref class ArrayTypeMismatchException : SystemException
public class ArrayTypeMismatchException : Exception
[System.Serializable]
public class ArrayTypeMismatchException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArrayTypeMismatchException : SystemException
public class ArrayTypeMismatchException : SystemException
type ArrayTypeMismatchException = class
inherit Exception
[<System.Serializable>]
type ArrayTypeMismatchException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArrayTypeMismatchException = class
inherit SystemException
type ArrayTypeMismatchException = class
inherit SystemException
Public Class ArrayTypeMismatchException
Inherits Exception
Public Class ArrayTypeMismatchException
Inherits SystemException
- Héritage
- Héritage
- Attributs
Exemples
L’exemple de code suivant illustre les scénarios où ArrayTypeMismatchException est levée.
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
Remarques
ArrayTypeMismatchException est levée lorsque le système ne peut pas convertir l’élément en type déclaré pour le tableau. Par exemple, un élément de type String ne peut pas être stocké dans un Int32 tableau, car la conversion entre ces types n’est pas prise en charge. Il est généralement inutile pour les applications de lever cette exception.
Les instructions msIL (Microsoft intermediate language) suivantes lèvent ArrayTypeMismatchException :
ldelem.<type>ldelemastelem.<type>
ArrayTypeMismatchException utilise le COR_E_ARRAYTYPEMISMATCH HRESULT, qui a la valeur 0x80131503.
Pour obtenir la liste des valeurs de propriété initiales d’une instance de ArrayTypeMismatchException, consultez les ArrayTypeMismatchException constructeurs.
Constructeurs
| Nom | Description |
|---|---|
| ArrayTypeMismatchException() |
Initialise une nouvelle instance de la classe ArrayTypeMismatchException. |
| ArrayTypeMismatchException(SerializationInfo, StreamingContext) |
Initialise une nouvelle instance de la classe ArrayTypeMismatchException avec des données sérialisées. |
| ArrayTypeMismatchException(String, Exception) |
Initialise une nouvelle instance de la ArrayTypeMismatchException classe avec un message d’erreur spécifié et une référence à l’exception interne qui est la cause de cette exception. |
| ArrayTypeMismatchException(String) |
Initialise une nouvelle instance de la ArrayTypeMismatchException classe avec un message d’erreur spécifié. |
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 un message qui décrit l’exception actuelle. (Hérité de Exception) |
| 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) |
En cas de substitution dans une classe dérivée, définit l'SerializationInfo avec des informations sur l’exception. (Hérité de Exception) |
| 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) |