AmbiguousMatchException 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 AmbiguousMatchException 類別的新執行個體。
多載
AmbiguousMatchException() |
使用設定為 |
AmbiguousMatchException(String) |
使用在該類別中設定為指定訊息的訊息字串,和設定為 |
AmbiguousMatchException(String, Exception) |
使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 AmbiguousMatchException 類別的新執行個體。 |
AmbiguousMatchException()
使用設定為 null
的空訊息字串和根本原因例外狀況,來初始化 AmbiguousMatchException 類別的新執行個體。
public:
AmbiguousMatchException();
public AmbiguousMatchException ();
Public Sub New ()
備註
AmbiguousMatchException
繼承自 Exception。 此建構函式會設定 物件的屬性 Exception
,如下表所示。
屬性 | 值 |
---|---|
InnerException | null |
Message | 空字串 (“”) 。 |
另請參閱
適用於
AmbiguousMatchException(String)
使用在該類別中設定為指定訊息的訊息字串,和設定為 null
的根本原因例外狀況,來初始化 AmbiguousMatchException 類別的新執行個體。
public:
AmbiguousMatchException(System::String ^ message);
public AmbiguousMatchException (string message);
public AmbiguousMatchException (string? message);
new System.Reflection.AmbiguousMatchException : string -> System.Reflection.AmbiguousMatchException
Public Sub New (message As String)
參數
- message
- String
字串,表示這個例外狀況擲回的原因。
備註
AmbiguousMatchException
繼承自 Exception。 此建構函式會設定 物件的屬性 Exception
,如下表所示。
屬性 | 值 |
---|---|
InnerException | null |
Message |
message 字串。 |
適用於
AmbiguousMatchException(String, Exception)
使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 AmbiguousMatchException 類別的新執行個體。
public:
AmbiguousMatchException(System::String ^ message, Exception ^ inner);
public AmbiguousMatchException (string message, Exception inner);
public AmbiguousMatchException (string? message, Exception? inner);
new System.Reflection.AmbiguousMatchException : string * Exception -> System.Reflection.AmbiguousMatchException
Public Sub New (message As String, inner As Exception)
參數
- message
- String
解釋例外狀況原因的錯誤訊息。
- inner
- Exception
做為目前例外狀況發生原因的例外狀況。 如果 inner
參數不是 null
,則目前的例外狀況會在處理內部例外的 catch
區塊中引發。
範例
下列範例顯示兩個方法,分別名為 Mymethod
。 其中一個方法接受整數,另一種方法則採用字串。 如果整數傳遞至 Mymethod
,則會使用第一個方法。 如果傳遞字串,則會使用第二個方法。 如果無法判斷要使用哪 Mymethod
一個, AmbiguousMatchException
則會擲回 。
using namespace System;
using namespace System::Reflection;
namespace Ambiguity
{
ref class Myambiguous
{
public:
//The first overload is typed to an Int32
static void Mymethod(Int32 number)
{
Console::WriteLine("I am from 'Int32' method");
}
//The second overload is typed to a String^
static void Mymethod(String^ alpha)
{
Console::WriteLine("I am from 'String^' method.");
}
static void Main()
{
try
{
//The following does not cause as exception
Mymethod(2); // goes to Mymethod (Int32)
Mymethod("3"); // goes to Mymethod (String*)
Type^ Mytype = Type::GetType("Ambiguity.Myambiguous");
array<Type^>^temp0 = {Int32::typeid};
MethodInfo^ Mymethodinfo32 = Mytype->GetMethod("Mymethod", temp0);
array<Type^>^temp1 = {System::String::typeid};
MethodInfo^ Mymethodinfostr = Mytype->GetMethod("Mymethod", temp1);
//Invoke a method, utilizing a Int32 integer
array<Object^>^temp2 = {2};
Mymethodinfo32->Invoke(nullptr, temp2);
//Invoke the method utilizing a String^
array<Object^>^temp3 = {"1"};
Mymethodinfostr->Invoke(nullptr, temp3);
//The following line causes an ambiguous exception
MethodInfo^ Mymethodinfo = Mytype->GetMethod("Mymethod");
}
catch (AmbiguousMatchException^ ex)
{
Console::WriteLine("\n{0}\n{1}", ex->GetType()->FullName, ex->Message);
}
catch (...)
{
Console::WriteLine("\nSome other exception.");
}
return;
}
};
}
int main()
{
Ambiguity::Myambiguous::Main();
}
//This code produces the following output:
//
// I am from 'Int32' method
// I am from 'String^' method.
// I am from 'Int32' method
// I am from 'String^' method.
//
// System.Reflection.AmbiguousMatchException
// Ambiguous match found.
using System;
using System.Reflection;
namespace Ambiguity
{
class Myambiguous
{
//The first overload is typed to an int
public static void Mymethod(int number)
{
Console.WriteLine("I am from 'int' method");
}
//The second overload is typed to a string
public static void Mymethod(string alpha)
{
Console.WriteLine("I am from 'string' method.");
}
public static void Main()
{
try
{
//The following does not cause as exception
Mymethod(2); // goes to Mymethod(int)
Mymethod("3"); // goes to Mymethod(string)
Type Mytype = Type.GetType("Ambiguity.Myambiguous");
MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[]{typeof(int)});
MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[]{typeof(System.String)});
//Invoke a method, utilizing a int integer
Mymethodinfo32.Invoke(null, new Object[]{2});
//Invoke the method utilizing a string
Mymethodinfostr.Invoke(null, new Object[]{"1"});
//The following line causes an ambiguious exception
MethodInfo Mymethodinfo = Mytype.GetMethod("Mymethod");
} // end of try block
catch (AmbiguousMatchException ex)
{
Console.WriteLine("\n{0}\n{1}", ex.GetType().FullName, ex.Message);
}
catch
{
Console.WriteLine("\nSome other exception.");
}
return;
}
}
}
//This code produces the following output:
//
// I am from 'int' method
// I am from 'string' method.
// I am from 'int' method
// I am from 'string' method.
// System.Reflection.AmbiguousMatchException
// Ambiguous match found.
Imports System.Reflection
Namespace Ambiguity
Class Myambiguous
'The first overload is typed to an Int32
Overloads Public Shared Sub Mymethod(number As Int32)
Console.WriteLine("I am from 'Int32' method")
End Sub
'The second overload is typed to a string
Overloads Public Shared Sub Mymethod(alpha As String)
Console.WriteLine("I am from 'string' method.")
End Sub
Public Shared Sub Main()
Try
'The following does not cause as exception
Mymethod(2) ' goes to Mymethod Int32)
Mymethod("3") ' goes to Mymethod(string)
Dim Mytype As Type = Type.GetType("Ambiguity.Myambiguous")
Dim Mymethodinfo32 As MethodInfo = Mytype.GetMethod("Mymethod", New Type() {GetType(Int32)})
Dim Mymethodinfostr As MethodInfo = Mytype.GetMethod("Mymethod", New Type() {GetType(System.String)})
'Invoke a method, utilizing a Int32 integer
Mymethodinfo32.Invoke(Nothing, New Object() {2})
'Invoke the method utilizing a string
Mymethodinfostr.Invoke(Nothing, New Object() {"1"})
'The following line causes an ambiguious exception
Dim Mymethodinfo As MethodInfo = Mytype.GetMethod("Mymethod")
' end of try block
Catch ex As AmbiguousMatchException
Console.WriteLine(Environment.NewLine + "{0}" + Environment.NewLine + "{1}", ex.GetType().FullName, ex.Message)
Catch
Console.WriteLine(Environment.NewLine + "Some other exception.")
End Try
Return
End Sub
End Class
End Namespace
' This code produces the following output:
'
' I am from 'Int32' method
' I am from 'string' method.
' I am from 'Int32' method
' I am from 'string' method.
'
' System.Reflection.AmbiguousMatchException
' Ambiguous match found.
備註
被擲回以做為前一個例外狀況直接結果的例外狀況,應該在 InnerException 屬性中包含對前一個例外狀況的參考。
InnerException 屬性傳回的值會與傳入建構函式的值相同,或傳回 null
(如果 InnerException 屬性未提供內部例外狀況值給建構函式)。
下表顯示 AmbiguousMatchException 執行個體的初始屬性值。
屬性 | 值 |
---|---|
InnerException | 內部例外狀況參考。 |
Message | 錯誤訊息字串。 |