AmbiguousMatchException Constructor (String, Exception)

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)

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 null, the current exception is raised in a catch block that handles the inner exception.

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 null 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.

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.


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.
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft