次の方法で共有


AmbiguousMatchException Constructor (String, Exception)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the AmbiguousMatchException class with a specified error message and a reference to the inner exception that is the cause of this exception.

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

Syntax

'Declaration
Public Sub New ( _
    message As String, _
    inner As Exception _
)
public AmbiguousMatchException(
    string message,
    Exception inner
)

Parameters

  • message
    Type: System.String
    The error message that explains the reason for the exception.
  • inner
    Type: System.Exception
    The exception that is the cause of the current exception. If the inner parameter is not nulla null reference (Nothing in Visual Basic), the current exception is raised in a catch block that handles the inner exception.

Remarks

An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the InnerException property. The InnerException property returns the same value that is passed into the constructor, or returns nulla null reference (Nothing in Visual Basic) if the InnerException property does not supply the inner exception value to the constructor.

The following table shows the initial property values for an instance of AmbiguousMatchException.

Property

Value

InnerException

The inner exception reference.

Message

The error message string.

Examples

The following example shows a method named Mymethod that has two overloads. One overload takes an integer, and the other takes a string. The method is invoked with late binding and an integer is passed to Mymethod, so the first overload is used. A string is then passed, and the second overload is used. Finally, the Type.GetMethod method is called with just the name Mymethod. The binder cannot determine which overload of Mymethod to return, so an AmbiguousMatchException is thrown.

Imports System.Reflection

Class Example

   Private Shared outputBlock As System.Windows.Controls.TextBlock

   'The first overload is typed to an Int32
   Public Overloads Shared Sub Mymethod(ByVal number As Integer)
      outputBlock.Text &= "Integer overload called." & vbLf
   End Sub 

   'The second overload is typed to a string
   Public Overloads Shared Sub Mymethod(ByVal alpha As String)
      outputBlock.Text &= "String overload called." & vbLf
   End Sub 

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      Try
         'The following does not cause an exception
         Mymethod(2)      ' Calls Mymethod(Integer)
         Mymethod("t3")   ' Calls Mymethod(String)

         Dim Mytype As Type = GetType(Example)

         Dim Mymethodinfo32 As MethodInfo = _
            Mytype.GetMethod("Mymethod", New Type() {GetType(Int32)}, Nothing)
         Dim Mymethodinfostr As MethodInfo = _
            Mytype.GetMethod("Mymethod", New Type() {GetType(System.String)})

         ' Invoke the static method using an Integer.
         Mymethodinfo32.Invoke(Nothing, New Object() {2})

         ' Invoke the method using a String
         Mymethodinfostr.Invoke(Nothing, New Object() {"1"})

         ' The following line causes an ambiguous match exception.
         Dim Mymethodinfo As MethodInfo = Mytype.GetMethod("Mymethod")

      Catch theException As System.Reflection.AmbiguousMatchException
         outputBlock.Text &= String.Format("AmbiguousMatchException message - {0}", theException.Message)
      End Try
   End Sub 
End Class 

'This code produces the following output:
'
'Integer overload called.
'String overload called.
'Integer overload called.
'String overload called.
'AmbiguousMatchException message - Ambiguous match found.
using System;
using System.Reflection;

class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   //The first overload is typed to an Int32
   public static void Mymethod(int number)
   {
      outputBlock.Text += "Int32 overload called.\n";
   }

   //The second overload is typed to a string
   public static void Mymethod(string alpha)
   {
      outputBlock.Text += "String overload called.\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      try
      {
         //The following does not cause as exception
         Mymethod(2);     // Calls Mymethod(int)
         Mymethod("3");   // Calls Mymethod(string)

         Type Mytype = typeof(Example);

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

         // Invoke the static method using an int.
         Mymethodinfo32.Invoke(null, new Object[] { 2 });

         // Invoke the method using a string
         Mymethodinfostr.Invoke(null, new Object[] { "1" });

         // The following line causes an ambiguious match exception
         MethodInfo Mymethodinfo = Mytype.GetMethod("Mymethod");
      }  
      catch (System.Reflection.AmbiguousMatchException theException)
      {
         outputBlock.Text += String.Format("AmbiguousMatchException message - {0}", theException.Message);
      }
   }
}

/* This code produces the following output:

Int32 overload called.
String overload called.
Int32 overload called.
String overload called.
AmbiguousMatchException message - Ambiguous match found.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.