AmbiguousMatchException Constructor (String, Exception)
Collapse the table of content
Expand the table of content

AmbiguousMatchException Constructor (String, Exception)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

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

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 Nothing, 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 Nothing 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.


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.


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft