Condividi tramite


AmbiguousMatchException Costruttori

Definizione

Inizializza una nuova istanza della classe AmbiguousMatchException.

Overload

Nome Descrizione
AmbiguousMatchException()

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

AmbiguousMatchException(String)

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

AmbiguousMatchException(String, Exception)

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

AmbiguousMatchException()

Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs

Inizializza una nuova istanza della AmbiguousMatchException classe 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)

Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs

Inizializza una nuova istanza della classe con la AmbiguousMatchException relativa stringa di messaggio impostata sul messaggio specificato e 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 cui è 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)

Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs
Origine:
AmbiguousMatchException.cs

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

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 che spiega il motivo dell'eccezione.

inner
Exception

Eccezione che rappresenta la causa dell'eccezione corrente. Se il inner parametro non nullè , l'eccezione corrente viene generata in un catch blocco che gestisce l'eccezione interna.

Esempio

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

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 InnerException proprietà . La InnerException proprietà restituisce lo stesso valore passato al costruttore oppure null se la InnerException proprietà non fornisce il valore dell'eccezione interna al costruttore.

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

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

Vedi anche

Si applica a