Udostępnij za pośrednictwem


AmbiguousMatchException Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy AmbiguousMatchException.

Przeciążenia

Nazwa Opis
AmbiguousMatchException()

Inicjuje nowe wystąpienie AmbiguousMatchException klasy z pustym ciągiem komunikatu i główną przyczyną wyjątku ustawioną na nullwartość .

AmbiguousMatchException(String)

Inicjuje nowe wystąpienie AmbiguousMatchException klasy z ciągiem komunikatu ustawionym na daną wiadomość i główną przyczyną wyjątku ustawioną na nullwartość .

AmbiguousMatchException(String, Exception)

Inicjuje nowe wystąpienie AmbiguousMatchException klasy z określonym komunikatem o błędzie i odwołaniem do wyjątku wewnętrznego, który jest przyczyną tego wyjątku.

AmbiguousMatchException()

Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs

Inicjuje nowe wystąpienie AmbiguousMatchException klasy z pustym ciągiem komunikatu i główną przyczyną wyjątku ustawioną na nullwartość .

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

Uwagi

AmbiguousMatchException dziedziczy z Exception. Ten konstruktor ustawia właściwości Exception obiektu, jak pokazano w poniższej tabeli.

Majątek Wartość
InnerException null
Message Pusty ciąg ("").

Zobacz też

Dotyczy

AmbiguousMatchException(String)

Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs

Inicjuje nowe wystąpienie AmbiguousMatchException klasy z ciągiem komunikatu ustawionym na daną wiadomość i główną przyczyną wyjątku ustawioną na nullwartość .

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)

Parametry

message
String

Ciąg wskazujący przyczynę zgłoszenia tego wyjątku.

Uwagi

AmbiguousMatchException dziedziczy z Exception. Ten konstruktor ustawia właściwości Exception obiektu, jak pokazano w poniższej tabeli.

Majątek Wartość
InnerException null
Message Ciąg message .

Dotyczy

AmbiguousMatchException(String, Exception)

Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs
Źródło:
AmbiguousMatchException.cs

Inicjuje nowe wystąpienie AmbiguousMatchException klasy z określonym komunikatem o błędzie i odwołaniem do wyjątku wewnętrznego, który jest przyczyną tego wyjątku.

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)

Parametry

message
String

Komunikat o błędzie wyjaśniający przyczynę wyjątku.

inner
Exception

Wyjątek, który jest przyczyną bieżącego wyjątku. inner Jeśli parametr nie nulljest , bieżący wyjątek jest zgłaszany w catch bloku, który obsługuje wyjątek wewnętrzny.

Przykłady

W poniższym przykładzie przedstawiono dwie metody, z których każda ma nazwę Mymethod. Jedna metoda przyjmuje liczbę całkowitą, a druga przyjmuje ciąg. Jeśli liczba całkowita zostanie przekazana do Mymethodmetody , zostanie użyta pierwsza metoda. Jeśli ciąg zostanie przekazany, zostanie użyta druga metoda. Jeśli nie można określić, którego Mymethod z nich użyć, AmbiguousMatchException zostanie zgłoszony.

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.

Uwagi

Wyjątek zgłaszany bezpośrednio w wyniku poprzedniego wyjątku powinien zawierać odwołanie do poprzedniego wyjątku InnerException we właściwości . Właściwość InnerException zwraca tę samą wartość, która jest przekazywana do konstruktora lub null jeśli InnerException właściwość nie dostarcza wartości wyjątku wewnętrznego do konstruktora.

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia AmbiguousMatchExceptionklasy .

Majątek Wartość
InnerException Odwołanie do wyjątku wewnętrznego.
Message Ciąg komunikatu o błędzie.

Zobacz też

Dotyczy