AmbiguousMatchException Construtores

Definição

Inicializa uma nova instância da classe AmbiguousMatchException.Initializes a new instance of the AmbiguousMatchException class.

Sobrecargas

AmbiguousMatchException()

Inicializa uma nova instância da classe AmbiguousMatchException com uma cadeia de caracteres de mensagem vazia e a exceção de causa raiz definida como null.Initializes a new instance of the AmbiguousMatchException class with an empty message string and the root cause exception set to null.

AmbiguousMatchException(String)

Inicializa uma nova instância da classe AmbiguousMatchException com sua cadeia de caracteres de mensagem definida como a mensagem fornecida e a exceção de causa raiz definida como null.Initializes 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)

Inicializa uma nova instância da classe AmbiguousMatchException com uma mensagem de erro especificada e uma referência à exceção interna que é a causa da exceção.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()

Inicializa uma nova instância da classe AmbiguousMatchException com uma cadeia de caracteres de mensagem vazia e a exceção de causa raiz definida como 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 ()

Comentários

AmbiguousMatchException herda de Exception.AmbiguousMatchException inherits from Exception. Esse construtor define as propriedades do Exception objeto, conforme mostrado na tabela a seguir.This constructor sets the properties of the Exception object as shown in the following table.

PropriedadeProperty ValorValue
InnerException null
Message A cadeia de caracteres vazia ("").The empty string ("").

Confira também

Aplica-se a

AmbiguousMatchException(String)

Inicializa uma nova instância da classe AmbiguousMatchException com sua cadeia de caracteres de mensagem definida como a mensagem fornecida e a exceção de causa raiz definida como null.Initializes 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);
public AmbiguousMatchException (string? message);
new System.Reflection.AmbiguousMatchException : string -> System.Reflection.AmbiguousMatchException
Public Sub New (message As String)

Parâmetros

message
String

Uma cadeia de caracteres que indica o motivo pelo qual esta exceção foi lançada.A string indicating the reason this exception was thrown.

Comentários

AmbiguousMatchException herda de Exception.AmbiguousMatchException inherits from Exception. Esse construtor define as propriedades do Exception objeto, conforme mostrado na tabela a seguir.This constructor sets the properties of the Exception object as shown in the following table.

PropriedadeProperty ValorValue
InnerException null
Message A message cadeia de caracteres.The message string.

Aplica-se a

AmbiguousMatchException(String, Exception)

Inicializa uma nova instância da classe AmbiguousMatchException com uma mensagem de erro especificada e uma referência à exceção interna que é a causa da exceção.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);
public AmbiguousMatchException (string? message, Exception? inner);
new System.Reflection.AmbiguousMatchException : string * Exception -> System.Reflection.AmbiguousMatchException
Public Sub New (message As String, inner As Exception)

Parâmetros

message
String

A mensagem de erro que explica a razão da exceção.The error message that explains the reason for the exception.

inner
Exception

A exceção que é a causa da exceção atual.The exception that is the cause of the current exception. Caso o parâmetro inner não seja null, a exceção atual é acionada em um bloco catch que identifica a exceção interna.If the inner parameter is not null, the current exception is raised in a catch block that handles the inner exception.

Exemplos

O exemplo a seguir mostra duas classes, cada uma denominada Mymethod .The following example shows two classes, each named Mymethod. Uma classe usa um inteiro e a outra usa uma cadeia de caracteres.One class takes an integer and the other takes a string. Se um inteiro for passado para Mymethod , a primeira classe será usada.If an integer is passed to Mymethod, the first class is used. Se uma cadeia de caracteres for passada, a segunda classe será usada.If a string is passed, the second class is used. Se não for possível determinar o que Mymethod usar, AmbiguousMatchException será gerado.If 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(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.

Comentários

Uma exceção que é lançada como um resultado direto de uma exceção anterior deve incluir uma referência para a exceção anterior na propriedade 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. A propriedade InnerException retorna o mesmo valor passado ao construtor, ou null se a propriedade InnerException não fornecer o valor da exceção interna ao construtor.The 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.

A tabela a seguir mostra os valores de propriedade inicial de uma instância de AmbiguousMatchException.The following table shows the initial property values for an instance of AmbiguousMatchException.

PropriedadeProperty ValorValue
InnerException A referência de exceção interna.The inner exception reference.
Message A cadeia de caracteres da mensagem de erro.The error message string.

Confira também

Aplica-se a