AmbiguousMatchException 构造函数

定义

初始化 AmbiguousMatchException 类的新实例。Initializes a new instance of the AmbiguousMatchException class.

重载

AmbiguousMatchException()

通过使用空消息字符串和将根源异常设置为 AmbiguousMatchException 来初始化 null 类的新实例。Initializes a new instance of the AmbiguousMatchException class with an empty message string and the root cause exception set to null.

AmbiguousMatchException(String)

初始化 AmbiguousMatchException 类的一个新实例,将其消息字符串设置为给定消息,将根源异常设置为 nullInitializes a new instance of the AmbiguousMatchException class with its message string set to the given message and the root cause exception set to null.

AmbiguousMatchException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 AmbiguousMatchException 类的新实例。Initializes a new instance of the AmbiguousMatchException class with a specified error message and a reference to the inner exception that is the cause of this exception.

AmbiguousMatchException()

通过使用空消息字符串和将根源异常设置为 AmbiguousMatchException 来初始化 null 类的新实例。Initializes a new instance of the AmbiguousMatchException class with an empty message string and the root cause exception set to null.

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

注解

AmbiguousMatchException 继承自 ExceptionAmbiguousMatchException inherits from Exception. 此构造函数将设置 Exception 对象的属性,如下表所示。This constructor sets the properties of the Exception object as shown in the following table.

属性Property Value
InnerException null
Message 空字符串("")。The empty string ("").

另请参阅

AmbiguousMatchException(String)

初始化 AmbiguousMatchException 类的一个新实例,将其消息字符串设置为给定消息,将根源异常设置为 nullInitializes a new instance of the AmbiguousMatchException class with its message string set to the given message and the root cause exception set to null.

public:
 AmbiguousMatchException(System::String ^ message);
public AmbiguousMatchException (string message);
new System.Reflection.AmbiguousMatchException : string -> System.Reflection.AmbiguousMatchException
Public Sub New (message As String)

参数

message
String

指示此异常的引发原因的字符串。A string indicating the reason this exception was thrown.

注解

AmbiguousMatchException 继承自 ExceptionAmbiguousMatchException inherits from Exception. 此构造函数将设置 Exception 对象的属性,如下表所示。This constructor sets the properties of the Exception object as shown in the following table.

属性Property Value
InnerException null
Message message 字符串。The message string.

AmbiguousMatchException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 AmbiguousMatchException 类的新实例。Initializes a new instance of the AmbiguousMatchException class with a specified error message and a reference to the inner exception that is the cause of this exception.

public:
 AmbiguousMatchException(System::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

解释异常原因的错误消息。The error message that explains the reason for the exception.

inner
Exception

引起当前异常的异常。The exception that is the cause of the current exception. 如果 inner 参数不为 null,则当前异常将在处理内部异常的 catch 块中引发。If the inner parameter is not null, the current exception is raised in a catch block that handles the inner exception.

示例

下面的示例演示两个类,每个类都命名为 MymethodThe following example shows two classes, each named Mymethod. 一个类采用整数,另一个类采用字符串。One class takes an integer and the other takes a string. 如果将整数传递到 Mymethod,则使用第一个类。If an integer is passed to Mymethod, the first class is used. 如果传递字符串,则使用第二个类。If a string is passed, the second class is used. 如果无法确定要使用 Mymethod,则会引发 AmbiguousMatchExceptionIf it cannot be determined which Mymethod to use, AmbiguousMatchException is thrown.

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 Int32
        public static void Mymethod(Int32 number)
        {
           Console.WriteLine("I am from 'Int32' 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(Int32)
                Mymethod("3");  // goes to Mymethod(string)

                Type Mytype = Type.GetType("Ambiguity.Myambiguous");

                MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[]{typeof(Int32)});
                MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[]{typeof(System.String)});

                //Invoke a method, utilizing a Int32 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 'Int32' method
// I am from 'string' method.
// I am from 'Int32' 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(vbNewLine + "{0}" + vbNewLine + "{1}", ex.GetType().FullName, ex.Message)
            Catch
                Console.WriteLine(vbNewLine + "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 属性中包含对前一个异常的引用。An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the InnerException property. InnerException 属性返回的值与传递到构造函数中的值相同;或者,如果 null 属性没有向构造函数提供内部异常值,则为 InnerExceptionThe InnerException property returns the same value that is passed into the constructor, or null if the InnerException property does not supply the inner exception value to the constructor.

下表显示 AmbiguousMatchException 的实例的初始属性值。The following table shows the initial property values for an instance of AmbiguousMatchException.

属性Property Value
InnerException 内部异常引用。The inner exception reference.
Message 错误消息字符串。The error message string.

另请参阅

适用于