ArrayTypeMismatchException 类

定义

当尝试在数组内存储错误类型的元素时引发的异常。

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
继承
ArrayTypeMismatchException
继承
ArrayTypeMismatchException
属性

示例

下面的代码示例演示引发的方案 ArrayTypeMismatchException

using namespace System;
int main()
{
   array<String^>^names = { "Dog", "Cat", "Fish"};
   array<Object^>^objs = dynamic_cast<array<Object^>^>(names);
   try
   {
      objs[ 2 ] = (Object^)"Mouse";
      for ( Int32 i = 0; i < objs->Length; i++ )
      {
         Console::WriteLine( objs[ i ] );

      }
   }
   catch ( System::ArrayTypeMismatchException^ ) 
   {
      
      // Not reached, "Mouse" is of the correct type
      Console::WriteLine(  "Exception Thrown" );
   }

   try
   {
      Object^ obj = 13;
      objs[ 2 ] = obj;
   }
   catch ( System::ArrayTypeMismatchException^ ) 
   {
      
      // Always reached, 13 is not a string.
      Console::WriteLine(  "New element is not of the correct type" );
   }

   
   // Set obj to an array of objects instead of an array of strings
   array<Object^>^objs2 = gcnew array<Object^>(3);
   try
   {
      objs2[ 0 ] = (Object^)"Turtle";
      objs2[ 1 ] = 12;
      objs2[ 2 ] = 2.341;
      for ( Int32 i = 0; i < objs->Length; i++ )
      {
         Console::WriteLine( objs2[ i ] );

      }
   }
   catch ( System::ArrayTypeMismatchException^ ) 
   {
      
      // ArrayTypeMismatchException is not thrown this time.
      Console::WriteLine(  "Exception Thrown" );
   }

}

/*expected return values:
Dog
Cat
Mouse
New element is not of the correct type
Turtle
12
2.341
*/
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

注解

ArrayTypeMismatchException 当系统无法将元素转换为为数组声明的类型时引发。 例如,类型元素 String 不能存储在数组中 Int32 ,因为不支持这些类型之间的转换。 应用程序通常不需要引发此异常。

以下 Microsoft 中间语言 (MSIL) 指令引发 ArrayTypeMismatchException

  • ldelem.<type>

  • ldelema

  • stelem.<type>

ArrayTypeMismatchException 使用具有值0x80131503的 HRESULT COR_E_ARRAYTYPEMISMATCH。

有关实例的初始属性值的列表ArrayTypeMismatchException,请参阅ArrayTypeMismatchException构造函数。

构造函数

ArrayTypeMismatchException()

初始化 ArrayTypeMismatchException 类的新实例。

ArrayTypeMismatchException(SerializationInfo, StreamingContext)

用序列化数据初始化 ArrayTypeMismatchException 类的新实例。

ArrayTypeMismatchException(String)

用指定的错误消息初始化 ArrayTypeMismatchException 类的新实例。

ArrayTypeMismatchException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 ArrayTypeMismatchException 类的新实例。

属性

Data

获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。

(继承自 Exception)
Message

获取描述当前异常的消息。

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。

(继承自 Exception)
TargetSite

获取引发当前异常的方法。

(继承自 Exception)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。

(继承自 Exception)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfo

(继承自 Exception)
GetType()

获取当前实例的运行时类型。

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。

(继承自 Exception)

事件

SerializeObjectState
已过时。

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。

(继承自 Exception)

适用于

另请参阅