ArrayTypeMismatchException 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 ArrayTypeMismatchException 类的新实例。
重载
ArrayTypeMismatchException() |
初始化 ArrayTypeMismatchException 类的新实例。 |
ArrayTypeMismatchException(String) |
用指定的错误消息初始化 ArrayTypeMismatchException 类的新实例。 |
ArrayTypeMismatchException(SerializationInfo, StreamingContext) |
已过时.
用序列化数据初始化 ArrayTypeMismatchException 类的新实例。 |
ArrayTypeMismatchException(String, Exception) |
使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 ArrayTypeMismatchException 类的新实例。 |
ArrayTypeMismatchException()
初始化 ArrayTypeMismatchException 类的新实例。
public:
ArrayTypeMismatchException();
public ArrayTypeMismatchException ();
Public Sub New ()
示例
以下示例演示 类的 ArrayTypeMismatchException () 构造函数 ArrayTypeMismatchException 。 它包含一个函数,该函数采用两个数组作为参数,并检查这两个数组是否属于同一类型。 如果数组属于不同的类型,则会引发新的 ArrayTypeMismatchException ,然后在调用方法中捕获。
using namespace System;
public ref class ArrayTypeMisMatchConst
{
public:
void CopyArray( Array^ myArray, Array^ myArray1 )
{
String^ typeArray1 = myArray->GetType()->ToString();
String^ typeArray2 = myArray1->GetType()->ToString();
// Check whether the two arrays are of same type or not.
if ( typeArray1 == typeArray2 )
{
// Copy the values from one array to another.
myArray->SetValue( String::Concat( "Name: ", myArray1->GetValue( 0 )->ToString() ), 0 );
myArray->SetValue( String::Concat( "Name: ", myArray1->GetValue( 1 )->ToString() ), 1 );
}
else
{
// Throw an exception of type 'ArrayTypeMismatchException'.
throw gcnew ArrayTypeMismatchException;
}
}
};
int main()
{
try
{
array<String^>^myStringArray = gcnew array<String^>(2);
myStringArray->SetValue( "Jones", 0 );
myStringArray->SetValue( "John", 1 );
array<Int32>^myIntArray = gcnew array<Int32>(2);
ArrayTypeMisMatchConst^ myArrayType = gcnew ArrayTypeMisMatchConst;
myArrayType->CopyArray( myStringArray, myIntArray );
}
catch ( ArrayTypeMismatchException^ e )
{
Console::WriteLine( "The Exception is : {0}", e );
}
}
using System;
public class ArrayTypeMismatchConst
{
public void CopyArray(Array myArray, Array myArray1)
{
string typeArray1 = myArray.GetType().ToString();
string typeArray2 = myArray1.GetType().ToString();
// Check whether the two arrays are of same type or not.
if (typeArray1 == typeArray2)
{
// Copy the values from one array to another.
myArray.SetValue("Name: " + myArray1.GetValue(0), 0);
myArray.SetValue("Name: " + myArray1.GetValue(1), 1);
}
else
{
// Throw an exception of type 'ArrayTypeMismatchException'.
throw new ArrayTypeMismatchException();
}
}
static void Main()
{
try
{
string[] myStringArray = new string[2];
myStringArray.SetValue("Jones", 0);
myStringArray.SetValue("John", 1);
int[] myIntArray = new int[2];
ArrayTypeMismatchConst myArrayType = new();
myArrayType.CopyArray(myStringArray, myIntArray);
}
catch (ArrayTypeMismatchException e)
{
Console.WriteLine("The Exception is :" + e);
}
}
}
open System
let copyArray (myArray: Array) (myArray1: Array) =
let typeArray1 = myArray.GetType() |> string
let typeArray2 = myArray1.GetType() |> string
// Check whether the two arrays are of same type or not.
if typeArray1 = typeArray2 then
// Copy the values from one array to another.
myArray.SetValue($"Name: {myArray1.GetValue 0}", 0)
myArray.SetValue($"Name: {myArray1.GetValue 1}", 1)
else
// Throw an exception of type 'ArrayTypeMismatchException'.
raise (ArrayTypeMismatchException())
try
let myStringArray = [| "Jones"; "John" |]
let myIntArray = Array.zeroCreate<int> 2
copyArray myStringArray myIntArray
with :? ArrayTypeMismatchException as e ->
printfn $"The Exception is: {e}"
Public Class ArrayTypeMisMatchConst
Public Sub CopyArray(myArray As Array, myArray1 As Array)
Dim typeArray1 As String = myArray.GetType().ToString()
Dim typeArray2 As String = myArray1.GetType().ToString()
' Check whether the two arrays are of same type or not.
If typeArray1 = typeArray2 Then
' Copy the values from one array to another.
myArray.SetValue("Name: " + myArray1.GetValue(0), 0)
myArray.SetValue("Name: " + myArray1.GetValue(1), 1)
Else
' Throw an exception of type 'ArrayTypeMismatchException'.
Throw New ArrayTypeMismatchException()
End If
End Sub
Shared Sub Main()
Try
Dim myStringArray(1) As String
myStringArray.SetValue("Jones", 0)
myStringArray.SetValue("John", 1)
Dim myIntArray(1) As Integer
Dim myArrayType As New ArrayTypeMisMatchConst()
myArrayType.CopyArray(myStringArray, myIntArray)
Catch e As ArrayTypeMismatchException
Console.WriteLine("The Exception is :" + e.ToString())
End Try
End Sub
End Class
注解
此构造函数将新 实例的 属性初始化 Message 为系统提供的消息,该消息描述错误,例如“无法将源数组类型分配给目标数组类型”。此消息考虑了当前系统区域性。
下表显示了 ArrayTypeMismatchException 实例的初始属性值。
properties | 值 |
---|---|
InnerException | 空引用(在 Visual Basic 中为 Nothing )。 |
Message | 本地化的错误消息字符串。 |
适用于
ArrayTypeMismatchException(String)
用指定的错误消息初始化 ArrayTypeMismatchException 类的新实例。
public:
ArrayTypeMismatchException(System::String ^ message);
public ArrayTypeMismatchException (string message);
public ArrayTypeMismatchException (string? message);
new ArrayTypeMismatchException : string -> ArrayTypeMismatchException
Public Sub New (message As String)
参数
示例
以下示例演示 类的 ArrayTypeMismatchException (String) 构造函数 ArrayTypeMismatchException 。 它包含一个函数,该函数采用两个数组作为参数,并检查这两个数组是否属于同一类型。 如果数组属于不同的类型,则会引发新的 ArrayTypeMismatchException ,然后在调用方法中捕获。
using namespace System;
public ref class ArrayTypeMisMatchConst
{
public:
void CopyArray( Array^ myArray, Array^ myArray1 )
{
String^ typeArray1 = myArray->GetType()->ToString();
String^ typeArray2 = myArray1->GetType()->ToString();
// Check whether the two arrays are of same type or not.
if ( typeArray1 == typeArray2 )
{
// Copies the values from one array to another.
myArray->SetValue( String::Concat( "Name: ", myArray1->GetValue( 0 )->ToString() ), 0 );
myArray->SetValue( String::Concat( "Name: ", myArray1->GetValue( 1 )->ToString() ), 1 );
}
else
{
// Throw an exception of type 'ArrayTypeMismatchException' with a message String* as parameter.
throw gcnew ArrayTypeMismatchException( "The Source and destination arrays are not of same type." );
}
}
};
int main()
{
try
{
array<String^>^myStringArray = gcnew array<String^>(2);
myStringArray->SetValue( "Jones", 0 );
myStringArray->SetValue( "John", 1 );
array<Int32>^myIntArray = gcnew array<Int32>(2);
ArrayTypeMisMatchConst^ myArrayType = gcnew ArrayTypeMisMatchConst;
myArrayType->CopyArray( myStringArray, myIntArray );
}
catch ( ArrayTypeMismatchException^ e )
{
Console::WriteLine( "The Exception Message is : {0}", e->Message );
}
}
using System;
public class ArrayTypeMismatchConst2
{
public void CopyArray(Array myArray, Array myArray1)
{
string typeArray1 = myArray.GetType().ToString();
string typeArray2 = myArray1.GetType().ToString();
// Check whether the two arrays are of same type or not.
if (typeArray1 == typeArray2)
{
// Copies the values from one array to another.
myArray.SetValue("Name: " + myArray1.GetValue(0), 0);
myArray.SetValue("Name: " + myArray1.GetValue(1), 1);
}
else
{
// Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter.
throw new ArrayTypeMismatchException("The Source and destination arrays are not of same type.");
}
}
static void Main()
{
try
{
string[] myStringArray = new string[2];
myStringArray.SetValue("Jones", 0);
myStringArray.SetValue("John", 1);
int[] myIntArray = new int[2];
ArrayTypeMismatchConst2 myArrayType = new();
myArrayType.CopyArray(myStringArray, myIntArray);
}
catch (ArrayTypeMismatchException e)
{
Console.WriteLine("The Exception Message is : " + e.Message);
}
}
}
open System
let copyArray (myArray: Array) (myArray1: Array) =
let typeArray1 = myArray.GetType() |> string
let typeArray2 = myArray1.GetType() |> string
// Check whether the two arrays are of same type or not.
if typeArray1 = typeArray2 then
// Copy the values from one array to another.
myArray.SetValue($"Name: {myArray1.GetValue 0}", 0)
myArray.SetValue($"Name: {myArray1.GetValue 1}", 1)
else
// Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter.
raise (ArrayTypeMismatchException "The Source and destination arrays are not of same type.")
try
let myStringArray = [| "Jones"; "John" |]
let myIntArray = Array.zeroCreate<int> 2
copyArray myStringArray myIntArray
with :? ArrayTypeMismatchException as e ->
printfn $"The Exception is: {e}"
Public Class ArrayTypeMisMatchConst
Public Sub CopyArray(myArray As Array, myArray1 As Array)
Dim typeArray1 As String = myArray.GetType().ToString()
Dim typeArray2 As String = myArray1.GetType().ToString()
' Check whether the two arrays are of same type or not.
If typeArray1 = typeArray2 Then
' Copies the values from one array to another.
myArray.SetValue("Name: " + myArray1.GetValue(0), 0)
myArray.SetValue("Name: " + myArray1.GetValue(1), 1)
Else
' Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter.
Throw New ArrayTypeMismatchException("The Source and destination arrays are not of same type.")
End If
End Sub
Shared Sub Main()
Try
Dim myStringArray(1) As String
myStringArray.SetValue("Jones", 0)
myStringArray.SetValue("John", 1)
Dim myIntArray(1) As Integer
Dim myArrayType As New ArrayTypeMisMatchConst()
myArrayType.CopyArray(myStringArray, myIntArray)
Catch e As ArrayTypeMismatchException
Console.WriteLine(("The Exception Message is : " + e.Message))
End Try
End Sub
End Class
注解
message
参数的内容应为人所理解。 此构造函数的调用方需要确保此字符串已针对当前系统区域性进行了本地化。
下表显示了 ArrayTypeMismatchException 实例的初始属性值。
properties | 值 |
---|---|
InnerException | 空引用(在 Visual Basic 中为 Nothing )。 |
Message | 错误消息字符串。 |
适用于
ArrayTypeMismatchException(SerializationInfo, StreamingContext)
注意
This API supports obsolete formatter-based serialization. It should not be called or extended by application code.
用序列化数据初始化 ArrayTypeMismatchException 类的新实例。
protected:
ArrayTypeMismatchException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected ArrayTypeMismatchException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected ArrayTypeMismatchException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new ArrayTypeMismatchException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> ArrayTypeMismatchException
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new ArrayTypeMismatchException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> ArrayTypeMismatchException
Protected Sub New (info As SerializationInfo, context As StreamingContext)
参数
- info
- SerializationInfo
承载序列化对象数据的对象。
- context
- StreamingContext
关于来源和目标的上下文信息
- 属性
注解
在反序列化期间调用此构造函数以重新构造在流上传输的异常对象。 有关详细信息,请参阅 XML 和 SOAP 序列化。
另请参阅
适用于
ArrayTypeMismatchException(String, Exception)
使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 ArrayTypeMismatchException 类的新实例。
public:
ArrayTypeMismatchException(System::String ^ message, Exception ^ innerException);
public ArrayTypeMismatchException (string message, Exception innerException);
public ArrayTypeMismatchException (string? message, Exception? innerException);
new ArrayTypeMismatchException : string * Exception -> ArrayTypeMismatchException
Public Sub New (message As String, innerException As Exception)
参数
- message
- String
解释异常原因的错误消息。
- innerException
- Exception
导致当前异常的异常。 如果 innerException
参数不为空引用,则在处理内部异常的 catch
块中引发当前异常。
示例
下面的代码示例演示 ArrayTypeMismatchException 类的 ArrayTypeMismatchException 构造函数。 它包含一个函数,该函数采用两个数组作为参数,并检查这两个数组是否属于同一类型。 如果数组属于不同的类型,则会引发新的 ArrayTypeMismatchException ,然后在调用方法中捕获。
using namespace System;
public ref class ArrayTypeMisMatchConst
{
public:
void CopyArray( Array^ myArray, Array^ myArray1 )
{
try
{
// Copies the value of one array into another array.
myArray->SetValue( myArray1->GetValue( 0 ), 0 );
myArray->SetValue( myArray1->GetValue( 1 ), 1 );
}
catch ( Exception^ e )
{
// Throw an exception of with a message and innerexception.
throw gcnew ArrayTypeMismatchException( "The Source and destination arrays are of not same type.",e );
}
}
};
int main()
{
try
{
array<String^>^myStringArray = gcnew array<String^>(2);
myStringArray->SetValue( "Jones", 0 );
myStringArray->SetValue( "John", 1 );
array<Int32>^myIntArray = gcnew array<Int32>(2);
ArrayTypeMisMatchConst^ myArrayType = gcnew ArrayTypeMisMatchConst;
myArrayType->CopyArray( myStringArray, myIntArray );
}
catch ( ArrayTypeMismatchException^ e )
{
Console::WriteLine( "The Exception Message is : {0}", e->Message );
Console::WriteLine( "The Inner exception is : {0}", e->InnerException );
}
}
using System;
public class ArrayTypeMismatchConst3
{
public void CopyArray(Array myArray, Array myArray1)
{
try
{
// Copies the value of one array into another array.
myArray.SetValue(myArray1.GetValue(0), 0);
myArray.SetValue(myArray1.GetValue(1), 1);
}
catch (Exception e)
{
// Throw an exception of with a message and innerexception.
throw new ArrayTypeMismatchException("The Source and destination arrays are of not same type.", e);
}
}
static void Main()
{
try
{
string[] myStringArray = new string[2];
myStringArray.SetValue("Jones", 0);
myStringArray.SetValue("John", 1);
int[] myIntArray = new int[2];
ArrayTypeMismatchConst3 myArrayType = new();
myArrayType.CopyArray(myStringArray, myIntArray);
}
catch (ArrayTypeMismatchException e)
{
Console.WriteLine("The Exception Message is : " + e.Message);
Console.WriteLine("The Inner exception is :" + e.InnerException);
}
}
}
open System
let copyArray (myArray: Array) (myArray1: Array) =
try
// Copies the value of one array into another array.
myArray.SetValue(myArray1.GetValue 0, 0)
myArray.SetValue(myArray1.GetValue 1, 1)
with e ->
// Throw an exception of with a message and innerexception.
raise (ArrayTypeMismatchException("The Source and destination arrays are of not same type.", e))
try
let myStringArray = [| "Jones"; "John" |]
let myIntArray = Array.zeroCreate<int> 2
copyArray myStringArray myIntArray
with :? ArrayTypeMismatchException as e ->
printfn $"The Exception Message is : {e.Message}"
printfn $"The Inner exception is: {e.InnerException}"
Public Class ArrayTypeMisMatchConst
Public Sub CopyArray(myArray As Array, myArray1 As Array)
Try
' Copies the value of one array into another array.
myArray.SetValue(myArray1.GetValue(0), 0)
myArray.SetValue(myArray1.GetValue(1), 1)
Catch e As Exception
' Throw an exception of type 'ArrayTypeMismatchException' with a message and innerexception.
Throw New ArrayTypeMismatchException("The Source and destination arrays are of not same type", e)
End Try
End Sub
Shared Sub Main()
Try
Dim myStringArray(1) As String
myStringArray.SetValue("Jones", 0)
myStringArray.SetValue("John", 1)
Dim myIntArray(1) As Integer
Dim myArrayType As New ArrayTypeMisMatchConst()
myArrayType.CopyArray(myStringArray, myIntArray)
Catch e As ArrayTypeMismatchException
Console.WriteLine("The Exception Message is : " + e.Message)
Console.WriteLine("The Inner exception is :" + e.InnerException.ToString())
End Try
End Sub
End Class
注解
因前一个异常而直接引发的异常应在 InnerException 属性中包含对前一个异常的引用。
InnerException 属性返回传递到构造函数中的相同值,或者如果 Nothing
属性不向构造函数提供内部异常值,则为空引用(在 Visual Basic 中为 InnerException)。
下表显示了 ArrayTypeMismatchException 实例的初始属性值。
properties | “值” |
---|---|
InnerException | 内部异常引用。 |
Message | 错误消息字符串。 |