指定したエラー メッセージと、この例外の原因である内部例外への参照を使用して、AmbiguousMatchException クラスの新しいインスタンスを初期化します。
名前空間: System.Reflection
アセンブリ: mscorlib (mscorlib.dll 内)
構文
'宣言
Public Sub New ( _
message As String, _
inner As Exception _
)
'使用
Dim message As String
Dim inner As Exception
Dim instance As New AmbiguousMatchException(message, inner)
public AmbiguousMatchException (
string message,
Exception inner
)
public:
AmbiguousMatchException (
String^ message,
Exception^ inner
)
public AmbiguousMatchException (
String message,
Exception inner
)
public function AmbiguousMatchException (
message : String,
inner : Exception
)
パラメータ
- message
例外の原因を説明するエラー メッセージ。
- inner
現在の例外の原因である例外。inner パラメータが null 参照 (Visual Basic では Nothing) でない場合は、内部例外を処理する catch ブロックで現在の例外が発生します。
解説
前の例外の直接の結果としてスローされる例外については、InnerException プロパティに、前の例外への参照が格納されます。InnerException プロパティは、コンストラクタに渡されたものと同じ値を返します。InnerException プロパティによって内部例外値がコンストラクタに渡されなかった場合は、null 参照 (Visual Basic では Nothing) を返します。
AmbiguousMatchException のインスタンスの初期プロパティ値を次の表に示します。
プロパティ |
値 |
---|---|
InnerException |
内部例外参照。 |
エラー メッセージ文字列。 |
使用例
Mymethod
という名前の 2 つのクラスを次の例で示します。一方のクラスは整数をとり、もう一方のクラスは文字列をとります。整数が Mymethod
に渡されると、最初のクラスが使用されます。文字列が渡された場合は、2 番目のクラスが使用されます。どちらの Mymethod
を使用するか判断できない場合は、AmbiguousMatchException がスローされます。
Class Myambiguous
'The first overload is typed to an Int32
Overloads Public Shared Sub Mymethod(number As Int32)
Console.Write(ControlChars.Cr + "{0}", "I am from Int32 method")
End Sub 'Mymethod
'The second overload is typed to a string
Overloads Public Shared Sub Mymethod(alpha As String)
Console.Write(ControlChars.Cr + "{0}", "I am from a string.")
End Sub 'Mymethod
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("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 theException As System.Reflection.AmbiguousMatchException
Console.Write(ControlChars.Cr & "AmbiguousMatchException message - {0}", theException.Message)
Catch
End Try
Return
End Sub 'Main
End Class 'Myambiguous
'This code produces the following output:
'I am from Int32 method
'I am from a string.
'I am from Int32 method
'I am from a string.
'AmbiguousMatchException message - Ambiguous match found.
class Myambiguous {
//The first overload is typed to an Int32
public static void Mymethod (Int32 number){
Console.Write("\n{0}", "I am from Int32 method");
}
//The second overload is typed to a string
public static void Mymethod (string alpha) {
Console.Write("\n{0}", "I am from a string.");
}
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("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(System.Reflection.AmbiguousMatchException theException) {
Console.Write("\nAmbiguousMatchException message - {0}", theException.Message);
}
catch {
Console.Write("\nError thrown");
}
return;
}
}
//This code produces the following output:
//I am from Int32 method
//I am from a string.
//I am from Int32 method
//I am from a string.
//AmbiguousMatchException message - Ambiguous match found.
ref class Myambiguous
{
public:
//The first overload is typed to an Int32
static void Mymethod( Int32 number )
{
Console::Write( "\nI am from Int32 method" );
}
//The second overload is typed to a String*
static void Mymethod( String^ alpha )
{
Console::Write( "\nI am from a String*." );
}
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( "Myambiguous" );
array<Type^>^temp0 = {Int32::typeid};
MethodInfo^ Mymethodinfo32 = Mytype->GetMethod( "Mymethod", temp0 );
array<Type^>^temp1 = {System::String::typeid};
MethodInfo^ Mymethodinfostr = Mytype->GetMethod( "Mymethod", temp1 );
//Invoke a method, utilizing a Int32 integer
array<Object^>^temp2 = {2};
Mymethodinfo32->Invoke( nullptr, temp2 );
//Invoke the method utilizing a String*
array<Object^>^temp3 = {"1"};
Mymethodinfostr->Invoke( nullptr, temp3 );
//The following line causes an ambiguous exception
MethodInfo^ Mymethodinfo = Mytype->GetMethod( "Mymethod" );
}
catch ( System::Reflection::AmbiguousMatchException^ theException )
{
Console::Write( "\nAmbiguousMatchException message - {0}", theException->Message );
}
catch ( Exception^ )
{
Console::Write( "\nError thrown" );
}
return;
}
};
int main()
{
Myambiguous::main();
}
//This code produces the following output:
//I am from Int32 method
//I am from a String*.
//I am from Int32 method
//I am from a String*.
//AmbiguousMatchException message - Ambiguous match found.
class MyAmbiguous
{
//The first overload is typed to an Int32
public static void MyMethod(Integer number)
{
Console.Write("\n{0}", "I am from Int32 method");
} //MyMethod
//The second overload is typed to a string
public static void MyMethod(String alpha)
{
Console.Write("\n{0}", "I am from a string.");
} //MyMethod
public static void main(String[] args)
{
try {
//The following does not cause as exception
MyMethod(new Integer(2)); // goes to MyMethod (Int32)
MyMethod("3"); // goes to MyMethod (string)
Type myType = Type.GetType("MyAmbiguous");
MethodInfo myMethodInfo32 = myType.GetMethod("MyMethod",
new Type[] { Integer.class.ToType() });
MethodInfo myMethodInfoStr = myType.GetMethod("MyMethod",
new Type[] { String.class.ToType() });
//Invoke a method, utilizing a Int32 integer
myMethodInfo32.Invoke(null, new Object[] { new Integer(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 (System.Reflection.AmbiguousMatchException theException) {
Console.Write("\nAmbiguousMatchException message - {0}",
theException.get_Message());
}
catch (System.Exception exp) {
Console.Write("\nError thrown");
}
return;
} //main
} //MyAmbiguous
//This code produces the following output:
//I am from Int32 method
//I am from a string.
//I am from Int32 method
//I am from a string.
//AmbiguousMatchException message - Ambiguous match found.
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 2.0、1.0
参照
関連項目
AmbiguousMatchException クラス
AmbiguousMatchException メンバ
System.Reflection 名前空間
Exception