共用方式為


AmbiguousMatchException 建構函式

定義

初始化 AmbiguousMatchException 類別的新執行個體。

多載

名稱 Description
AmbiguousMatchException()

初始化一個新的類別實例 AmbiguousMatchException ,使用空訊息字串並將根因例外設為 null

AmbiguousMatchException(String)

初始化一個新的類別實例 AmbiguousMatchException ,其訊息字串設為給定訊息,根因例外設為 null

AmbiguousMatchException(String, Exception)

初始化類別的新實例 AmbiguousMatchException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。

AmbiguousMatchException()

來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs

初始化一個新的類別實例 AmbiguousMatchException ,使用空訊息字串並將根因例外設為 null

public:
 AmbiguousMatchException();
public AmbiguousMatchException();
Public Sub New ()

備註

AmbiguousMatchException 繼承自 Exception。 此建構器設定物件的 Exception 屬性如下表所示。

房產 價值
InnerException null
Message 空字串(“”)。

另請參閱

適用於

AmbiguousMatchException(String)

來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs

初始化一個新的類別實例 AmbiguousMatchException ,其訊息字串設為給定訊息,根因例外設為 null

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.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs
來源:
AmbiguousMatchException.cs

初始化類別的新實例 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,則使用第一種方法。 如果傳遞字串,則使用第二種方法。 若無法決定使用哪一種 MymethodAmbiguousMatchException 則會被拋出。

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 錯誤訊息字串。

另請參閱

適用於