AmbiguousMatchException Konstruktorer

Definition

Initierar en ny instans av AmbiguousMatchException klassen.

Överlagringar

Name Description
AmbiguousMatchException()

Initierar en ny instans av AmbiguousMatchException klassen med en tom meddelandesträng och rotorsaksfelet inställt på null.

AmbiguousMatchException(String)

Initierar en ny instans av AmbiguousMatchException klassen med dess meddelandesträng inställd på det angivna meddelandet och rotorsaksfelet inställt på null.

AmbiguousMatchException(String, Exception)

Initierar en ny instans av AmbiguousMatchException klassen med ett angivet felmeddelande och en referens till det inre undantaget som är orsaken till det här undantaget.

AmbiguousMatchException()

Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs

Initierar en ny instans av AmbiguousMatchException klassen med en tom meddelandesträng och rotorsaksfelet inställt på null.

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

Kommentarer

AmbiguousMatchException ärver från Exception. Den här konstruktorn anger egenskaperna för Exception objektet enligt följande tabell.

Fastighet Value
InnerException null
Message Den tomma strängen ("").

Se även

Gäller för

AmbiguousMatchException(String)

Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs

Initierar en ny instans av AmbiguousMatchException klassen med dess meddelandesträng inställd på det angivna meddelandet och rotorsaksfelet inställt på 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)

Parametrar

message
String

En sträng som anger orsaken till att undantaget utlöstes.

Kommentarer

AmbiguousMatchException ärver från Exception. Den här konstruktorn anger egenskaperna för Exception objektet enligt följande tabell.

Fastighet Value
InnerException null
Message Strängen message .

Gäller för

AmbiguousMatchException(String, Exception)

Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs
Källa:
AmbiguousMatchException.cs

Initierar en ny instans av AmbiguousMatchException klassen med ett angivet felmeddelande och en referens till det inre undantaget som är orsaken till det här undantaget.

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)

Parametrar

message
String

Felmeddelandet som förklarar orsaken till undantaget.

inner
Exception

Undantaget som är orsaken till det aktuella undantaget. Om parametern inner inte nullär genereras det aktuella undantaget i ett catch block som hanterar det inre undantaget.

Exempel

I följande exempel visas två metoder med namnet Mymethod. En metod tar ett heltal och den andra tar en sträng. Om ett heltal skickas till Mymethodanvänds den första metoden. Om en sträng skickas används den andra metoden. Om det inte går att avgöra vilken som Mymethod ska användas, AmbiguousMatchException utlöses.

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.

Kommentarer

Ett undantag som genereras som ett direkt resultat av ett tidigare undantag bör innehålla en referens till det tidigare undantaget i InnerException egenskapen. Egenskapen InnerException returnerar samma värde som skickas till konstruktorn, eller null om InnerException egenskapen inte anger det inre undantagsvärdet till konstruktorn.

I följande tabell visas de inledande egenskapsvärdena för en instans av AmbiguousMatchException.

Fastighet Value
InnerException Den inre undantagsreferensen.
Message Felmeddelandesträngen.

Se även

Gäller för