AmbiguousMatchException Constructores

Definición

Inicializa una nueva instancia de la clase AmbiguousMatchException.

Sobrecargas

AmbiguousMatchException()

Inicializa una nueva instancia de la clase AmbiguousMatchException con una cadena de mensaje vacía y el valor de la excepción de causa principal establecido en null.

AmbiguousMatchException(String)

Inicializa una nueva instancia de la clase AmbiguousMatchException con su cadena de mensaje establecida en el mensaje dado y el valor de la excepción de causa principal establecido en null.

AmbiguousMatchException(String, Exception)

Inicializa una nueva instancia de la clase AmbiguousMatchException con el mensaje de error especificado y una referencia a la excepción interna que representa la causa de esta excepción.

AmbiguousMatchException()

Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs

Inicializa una nueva instancia de la clase AmbiguousMatchException con una cadena de mensaje vacía y el valor de la excepción de causa principal establecido en null.

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

Comentarios

AmbiguousMatchException hereda de Exception. Este constructor establece las propiedades del Exception objeto como se muestra en la tabla siguiente.

Propiedad Value
InnerException null
Message Cadena vacía ("").

Consulte también

Se aplica a

AmbiguousMatchException(String)

Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs

Inicializa una nueva instancia de la clase AmbiguousMatchException con su cadena de mensaje establecida en el mensaje dado y el valor de la excepción de causa principal establecido en 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

Cadena que indica la razón por la que se produjo esta excepción.

Comentarios

AmbiguousMatchException hereda de Exception. Este constructor establece las propiedades del Exception objeto como se muestra en la tabla siguiente.

Propiedad Value
InnerException null
Message Cadena message .

Se aplica a

AmbiguousMatchException(String, Exception)

Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs

Inicializa una nueva instancia de la clase AmbiguousMatchException con el mensaje de error especificado y una referencia a la excepción interna que representa la causa de esta excepción.

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

Mensaje de error que explica el motivo de la excepción.

inner
Exception

Excepción que es la causa de la excepción actual. Si el parámetro inner no es null, la excepción actual se produce en un bloque catch que controla la excepción interna.

Ejemplos

En el ejemplo siguiente se muestran dos métodos, cada uno denominado Mymethod. Un método toma un entero y el otro toma una cadena. Si se pasa un entero a Mymethod, se usa el primer método. Si se pasa una cadena, se usa el segundo método. Si no se puede determinar qué Mymethod usar, AmbiguousMatchException se produce .

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.

Comentarios

Una excepción que se produce como consecuencia directa de una excepción anterior debe incluir una referencia a esta última en la propiedad InnerException. La propiedad InnerException devuelve el mismo valor que se haya pasado al constructor, o bien null en caso de que la propiedad InnerException no le haya proporcionado al constructor el valor de la excepción interna.

En la tabla siguiente se muestran los valores de propiedad iniciales para una instancia de AmbiguousMatchException.

Propiedad Valor
InnerException Referencia a la excepción interna.
Message Cadena con el mensaje de error.

Consulte también

Se aplica a