AmbiguousMatchException 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 AmbiguousMatchException 类的新实例。
重载
| 名称 | 说明 |
|---|---|
| AmbiguousMatchException() |
使用空消息字符串和根本原因异常设置为 |
| AmbiguousMatchException(String) |
初始化类的新实例 AmbiguousMatchException ,其消息字符串设置为给定消息,并将根本原因异常设置为 |
| AmbiguousMatchException(String, Exception) |
使用指定的错误消息和对作为此异常原因的内部异常的引用初始化类的新实例 AmbiguousMatchException 。 |
AmbiguousMatchException()
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
使用空消息字符串和根本原因异常设置为
public:
AmbiguousMatchException();
public AmbiguousMatchException();
Public Sub New ()
注解
AmbiguousMatchException 继承自 Exception. 此构造函数设置对象的属性 Exception ,如下表所示。
| 财产 | 价值 |
|---|---|
| InnerException | null |
| Message | 空字符串(“)。 |
另请参阅
适用于
AmbiguousMatchException(String)
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
初始化类的新实例 AmbiguousMatchException ,其消息字符串设置为给定消息,并将根本原因异常设置为 null。
public:
AmbiguousMatchException(System::String ^ message);
public AmbiguousMatchException(string message);
public AmbiguousMatchException(string? message);
new System.Reflection.AmbiguousMatchException : string -> System.Reflection.AmbiguousMatchException
Public Sub New (message As String)
参数
- message
- String
一个字符串,指示引发此异常的原因。
注解
AmbiguousMatchException 继承自 Exception. 此构造函数设置对象的属性 Exception ,如下表所示。
| 财产 | 价值 |
|---|---|
| InnerException | null |
| Message | 字符串 message 。 |
适用于
AmbiguousMatchException(String, Exception)
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
- Source:
- AmbiguousMatchException.cs
使用指定的错误消息和对作为此异常原因的内部异常的引用初始化类的新实例 AmbiguousMatchException 。
public:
AmbiguousMatchException(System::String ^ message, Exception ^ inner);
public AmbiguousMatchException(string message, Exception inner);
public AmbiguousMatchException(string? message, Exception? inner);
new System.Reflection.AmbiguousMatchException : string * Exception -> System.Reflection.AmbiguousMatchException
Public Sub New (message As String, inner As Exception)
参数
- message
- String
说明异常原因的错误消息。
- inner
- Exception
是当前异常原因的异常。
inner如果参数不是null,则当前异常在处理内部异常的块中catch引发。
示例
以下示例显示了两个方法,每个方法分别命名 Mymethod。 一种方法采用整数,另一种方法采用字符串。 如果传递给 Mymethod整数,则使用第一个方法。 如果传递字符串,则使用第二种方法。 如果无法确定要使用的项 Mymethod , AmbiguousMatchException 则会引发。
using System;
using System.Reflection;
namespace Ambiguity
{
class Myambiguous
{
//The first overload is typed to an int
public static void Mymethod(int number)
{
Console.WriteLine("I am from 'int' 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(int)
Mymethod("3"); // goes to Mymethod(string)
Type Mytype = Type.GetType("Ambiguity.Myambiguous");
MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[]{typeof(int)});
MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[]{typeof(System.String)});
//Invoke a method, utilizing a int 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 'int' method
// I am from 'string' method.
// I am from 'int' method
// I am from 'string' method.
// System.Reflection.AmbiguousMatchException
// Ambiguous match found.
Imports System.Reflection
Namespace Ambiguity
Class Myambiguous
'The first overload is typed to an Int32
Overloads Public Shared Sub Mymethod(number As Int32)
Console.WriteLine("I am from 'Int32' method")
End Sub
'The second overload is typed to a string
Overloads Public Shared Sub Mymethod(alpha As String)
Console.WriteLine("I am from 'string' method.")
End Sub
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("Ambiguity.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 ex As AmbiguousMatchException
Console.WriteLine(Environment.NewLine + "{0}" + Environment.NewLine + "{1}", ex.GetType().FullName, ex.Message)
Catch
Console.WriteLine(Environment.NewLine + "Some other exception.")
End Try
Return
End Sub
End Class
End Namespace
' 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.
注解
作为上一个异常的直接结果引发的异常应包含对属性中上一异常的 InnerException 引用。 该 InnerException 属性返回传入构造函数的相同值,或者 null 该 InnerException 属性未向构造函数提供内部异常值。
下表显示了实例 AmbiguousMatchException的初始属性值。
| 财产 | 价值 |
|---|---|
| InnerException | 内部异常引用。 |
| Message | 错误消息字符串。 |