Export (0) Print
Expand All

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
Assemblies:   mscorlib (in mscorlib.dll)
  System.Reflection (in System.Reflection.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 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 two classes, each named Mymethod. One class takes an integer and the other takes a string. If an integer is passed to Mymethod, the first class is used. If a string is passed, the second class is used. If it cannot be determined which Mymethod to use, AmbiguousMatchException is thrown.

using System;
using System.Reflection;

namespace Ambiguity
{
    class Myambiguous
    {
        //The first overload is typed to an Int32 
        public static void Mymethod(Int32 number)
        {
           Console.WriteLine("I am from 'Int32' 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(Int32)
                Mymethod("3");  // goes to Mymethod(string)

                Type Mytype = Type.GetType("Ambiguity.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 (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 'Int32' method 
// I am from 'string' method. 
// I am from 'Int32' method 
// I am from 'string' method. 

// System.Reflection.AmbiguousMatchException 
// Ambiguous match found.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft