Freigeben über


AmbiguousMatchException-Konstruktor (String, Exception)

Initialisiert eine neue Instanz der AmbiguousMatchException-Klasse mit einer angegebenen Fehlermeldung und einem Verweis auf die innere Ausnahme, die diese Ausnahme verursacht hat.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub New ( _
    message As String, _
    inner As Exception _
)
'Usage
Dim message As String
Dim inner As Exception

Dim instance As New AmbiguousMatchException(message, inner)
public AmbiguousMatchException (
    string message,
    Exception inner
)
public:
AmbiguousMatchException (
    String^ message, 
    Exception^ inner
)
public AmbiguousMatchException (
    String message, 
    Exception inner
)
public function AmbiguousMatchException (
    message : String, 
    inner : Exception
)

Parameter

  • message
    Die Fehlermeldung, in der die Ursache der Ausnahme erklärt wird.
  • inner
    Die Ausnahme, die Ursache der aktuellen Ausnahme ist. Wenn der inner-Parameter nicht NULL (Nothing in Visual Basic) ist, wird die aktuelle Ausnahme in einem catch-Block ausgelöst, der die innere Ausnahme behandelt.

Hinweise

Eine Ausnahme, die als direktes Ergebnis einer vorhergehenden Ausnahme ausgelöst wird, muss in der InnerException-Eigenschaft über einen Verweis auf die vorhergehende Ausnahme verfügen. Die InnerException-Eigenschaft gibt den gleichen Wert zurück, der an den Konstruktor übergeben wird, oder NULL (Nothing in Visual Basic), wenn die InnerException-Eigenschaft den Wert der inneren Ausnahme nicht an den Konstruktor übergibt.

In der folgenden Tabelle werden die anfänglichen Eigenschaftenwerte für eine AmbiguousMatchException-Instanz aufgeführt.

Eigenschaft

Wert

InnerException

Der Verweis auf die innere Ausnahme.

Message

Die Zeichenfolge der Fehlermeldung.

Beispiel

Im folgenden Beispiel haben beide Klassen den Namen Mymethod. Die eine Klasse akzeptiert eine ganze Zahl und die andere eine Zeichenfolge. Wenn eine ganze Zahl an Mymethod übergeben wird, wird die erste Klasse verwendet. Wenn eine Zeichenfolge übergeben wird, wird die zweite Klasse verwendet. Wenn nicht bestimmt werden kann, welche Mymethod verwendet werden soll, wird AmbiguousMatchException ausgelöst.

Class Myambiguous
    
    'The first overload is typed to an Int32
    Overloads Public Shared Sub Mymethod(number As Int32)
        Console.Write(ControlChars.Cr + "{0}", "I am from Int32 method")
    End Sub 'Mymethod
    
    
    'The second overload is typed to a string
    Overloads Public Shared Sub Mymethod(alpha As String)
        Console.Write(ControlChars.Cr + "{0}", "I am from a string.")
    End Sub 'Mymethod
    
    
    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("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 theException As System.Reflection.AmbiguousMatchException
            Console.Write(ControlChars.Cr & "AmbiguousMatchException message - {0}", theException.Message)
        Catch
        End Try
        Return
    End Sub 'Main
End Class 'Myambiguous

'This code produces the following output:
'I am from Int32 method
'I am from a string.
'I am from Int32 method
'I am from a string.
'AmbiguousMatchException message - Ambiguous match found.
class Myambiguous {
    //The first overload is typed to an Int32
    public static void Mymethod (Int32 number){
       Console.Write("\n{0}", "I am from Int32 method");
    }

    //The second overload is typed to a string
    public static void Mymethod (string alpha) {
       Console.Write("\n{0}", "I am from a string.");
    }
    
    public 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("Myambiguous");

            MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[]{typeof(Int32)});
            MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[]{typeof(System.String)});

            //Invoke a method, utilizing a Int32 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(System.Reflection.AmbiguousMatchException theException) {
            Console.Write("\nAmbiguousMatchException message - {0}", theException.Message);
        }
        catch {
            Console.Write("\nError thrown");
        }
        return;
    }
}
 
 //This code produces the following output:
 //I am from Int32 method
 //I am from a string.
 //I am from Int32 method
 //I am from a string.
 //AmbiguousMatchException message - Ambiguous match found.
ref class Myambiguous
{
public:

   //The first overload is typed to an Int32
   static void Mymethod( Int32 number )
   {
      Console::Write( "\nI am from Int32 method" );
   }

   //The second overload is typed to a String*
   static void Mymethod( String^ alpha )
   {
      Console::Write( "\nI am from a String*." );
   }

   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( "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 ( System::Reflection::AmbiguousMatchException^ theException ) 
      {
         Console::Write( "\nAmbiguousMatchException message - {0}", theException->Message );
      }
      catch ( Exception^ ) 
      {
         Console::Write( "\nError thrown" );
      }

      return;
   }
};

int main()
{
   Myambiguous::main();
}

//This code produces the following output:
//I am from Int32 method
//I am from a String*.
//I am from Int32 method
//I am from a String*.
//AmbiguousMatchException message - Ambiguous match found.
class MyAmbiguous
{
    //The first overload is typed to an Int32
    public static void MyMethod(Integer number)
    {
        Console.Write("\n{0}", "I am from Int32 method");
    } //MyMethod

    //The second overload is typed to a string
    public static void MyMethod(String alpha)
    {
        Console.Write("\n{0}", "I am from a string.");
    } //MyMethod

    public static void main(String[] args)
    {
        try {
            //The following does not cause as exception
            MyMethod(new Integer(2)); // goes to MyMethod (Int32)
            MyMethod("3"); // goes to MyMethod (string)

            Type myType = Type.GetType("MyAmbiguous");
            MethodInfo myMethodInfo32 = myType.GetMethod("MyMethod", 
                new Type[] { Integer.class.ToType() });
            MethodInfo myMethodInfoStr = myType.GetMethod("MyMethod", 
                new Type[] { String.class.ToType() });

            //Invoke a method, utilizing a Int32 integer
            myMethodInfo32.Invoke(null, new Object[] { new Integer(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 (System.Reflection.AmbiguousMatchException theException) {
            Console.Write("\nAmbiguousMatchException message - {0}", 
                theException.get_Message());
        }
        catch (System.Exception exp) {
            Console.Write("\nError thrown");
        }
        return;
    } //main
} //MyAmbiguous

//This code produces the following output:
//I am from Int32 method
//I am from a string.
//I am from Int32 method
//I am from a string.
//AmbiguousMatchException message - Ambiguous match found.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

AmbiguousMatchException-Klasse
AmbiguousMatchException-Member
System.Reflection-Namespace
Exception

Weitere Ressourcen

Behandeln und Auslösen von Ausnahmen