AmbiguousMatchException Costruttori

Definizione

Inizializza una nuova istanza della classe AmbiguousMatchException.

Overload

AmbiguousMatchException()

Inizializza una nuova istanza della classe AmbiguousMatchException con una stringa di messaggio vuota e l'eccezione della causa radice impostata su null.

AmbiguousMatchException(String)

Inizializza una nuova istanza della classe AmbiguousMatchException con la stringa di messaggio impostata sul messaggio specificato e con l'eccezione della causa radice impostata su null.

AmbiguousMatchException(String, Exception)

Inizializza una nuova istanza della classe AmbiguousMatchException con un messaggio di errore specificato e un riferimento all'eccezione interna che è la causa dell'eccezione corrente.

AmbiguousMatchException()

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

Inizializza una nuova istanza della classe AmbiguousMatchException con una stringa di messaggio vuota e l'eccezione della causa radice impostata su null.

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

Commenti

AmbiguousMatchException eredita da Exception. Questo costruttore imposta le proprietà dell'oggetto Exception , come illustrato nella tabella seguente.

Proprietà Valore
InnerException null
Message Stringa vuota ("").

Vedi anche

Si applica a

AmbiguousMatchException(String)

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

Inizializza una nuova istanza della classe AmbiguousMatchException con la stringa di messaggio impostata sul messaggio specificato e con l'eccezione della causa radice impostata su 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)

Parametri

message
String

Stringa che indica il motivo per il quale è stata generata l'eccezione.

Commenti

AmbiguousMatchException eredita da Exception. Questo costruttore imposta le proprietà dell'oggetto Exception , come illustrato nella tabella seguente.

Proprietà Valore
InnerException null
Message Stringa message .

Si applica a

AmbiguousMatchException(String, Exception)

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

Inizializza una nuova istanza della classe AmbiguousMatchException con un messaggio di errore specificato e un riferimento all'eccezione interna che è la causa dell'eccezione corrente.

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)

Parametri

message
String

Messaggio di errore in cui viene indicato il motivo dell'eccezione.

inner
Exception

Eccezione che ha determinato l'eccezione corrente. Se il parametro inner non è null, l'eccezione corrente viene generata in un blocco catch in cui viene gestita l'eccezione interna.

Esempio

Nell'esempio seguente vengono illustrati due metodi, ognuno denominato Mymethod. Un metodo accetta un intero e l'altro accetta una stringa. Se viene passato un intero a Mymethod, viene usato il primo metodo. Se viene passata una stringa, viene usato il secondo metodo. Se non è possibile determinare quale Mymethod usare, AmbiguousMatchException viene generata.

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.

Commenti

Un'eccezione generata come risultato diretto di un'eccezione precedente deve includere un riferimento all'eccezione precedente nella proprietà InnerException. La proprietà InnerException restituisce lo stesso valore passato nel costruttore oppure il valore null se la proprietà InnerException non fornisce al costruttore il valore dell'eccezione interna.

Nella tabella seguente vengono illustrati i valori di proprietà iniziali per un'istanza di AmbiguousMatchException.

Proprietà Valore
InnerException Riferimento all'eccezione interna.
Message Stringa del messaggio di errore.

Vedi anche

Si applica a