MissingMemberException 类
试图动态访问不存在的类成员时引发的异常。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class MissingMemberException
Inherits MemberAccessException
Implements ISerializable
用法
Dim instance As MissingMemberException
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class MissingMemberException : MemberAccessException, ISerializable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class MissingMemberException : public MemberAccessException, ISerializable
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public class MissingMemberException extends MemberAccessException implements ISerializable
SerializableAttribute
ComVisibleAttribute(true)
public class MissingMemberException extends MemberAccessException implements ISerializable
备注
通常,如果代码试图访问不存在的类成员,会生成编译错误。MissingMemberException 旨在处理字段或方法在一个程序集内被删除或重命名,但此更改没有反映在第二个程序集中的情况。运行时,当第二个程序集中的代码试图访问第一个程序集中缺少的成员时,将引发 MissingMemberException。
MissingMemberException 是 MissingFieldException 和 MissingMethodException 的基类。一般情况下,最好使用 MissingMemberException 的一个派生类来更精确地指示错误的具体特性。如果只对捕获缺少成员这样的一般错误情况感兴趣,则引发 MissingMemberException。
MissingMemberException 使用值为 0x80131512 的 HRESULT COR_E_MISSINGMEMBER。
有关 MissingMemberException 实例的初始属性值列表,请参见 MissingMemberException 构造函数。
示例
此示例演示在您尝试使用反射来调用不存在的方法和访问不存在的字段时所发生的情况。应用程序通过捕捉 MissingMethodException、MissingFieldException 和 MissingMemberException 来恢复。
using System;
using System.Reflection;
public class App
{
public static void Main()
{
try
{
// Attempt to call a static DoSomething method defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
typeof(App).InvokeMember("DoSomething", BindingFlags.Static |
BindingFlags.InvokeMethod, null, null, null);
}
catch (MissingMethodException e)
{
// Show the user that the DoSomething method cannot be called.
Console.WriteLine("Unable to call the DoSomething method: {0}", e.Message);
}
try
{
// Attempt to access a static AField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
typeof(App).InvokeMember("AField", BindingFlags.Static | BindingFlags.SetField,
null, null, new Object[] { 5 });
}
catch (MissingFieldException e)
{
// Show the user that the AField field cannot be accessed.
Console.WriteLine("Unable to access the AField field: {0}", e.Message);
}
try
{
// Attempt to access a static AnotherField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
typeof(App).InvokeMember("AnotherField", BindingFlags.Static |
BindingFlags.GetField, null, null, null);
}
catch (MissingMemberException e)
{
// Notice that this code is catching MissingMemberException which is the
// base class of MissingMethodException and MissingFieldException.
// Show the user that the AnotherField field cannot be accessed.
Console.WriteLine("Unable to access the AnotherField field: {0}", e.Message);
}
}
}
// This code produces the following output.
//
// Unable to call the DoSomething method: Method 'App.DoSomething' not found.
// Unable to access the AField field: Field 'App.AField' not found.
// Unable to access the AnotherField field: Field 'App.AnotherField' not found.
using namespace System;
using namespace System::Reflection;
ref class App
{
};
int main()
{
try
{
// Attempt to call a static DoSomething method defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
App::typeid->InvokeMember("DoSomething", BindingFlags::Static |
BindingFlags::InvokeMethod, nullptr, nullptr, nullptr);
}
catch (MissingMethodException^ ex)
{
// Show the user that the DoSomething method cannot be called.
Console::WriteLine("Unable to call the DoSomething method: {0}",
ex->Message);
}
try
{
// Attempt to access a static AField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
App::typeid->InvokeMember("AField", BindingFlags::Static |
BindingFlags::SetField, nullptr, nullptr, gcnew array<Object^>{5});
}
catch (MissingFieldException^ ex)
{
// Show the user that the AField field cannot be accessed.
Console::WriteLine("Unable to access the AField field: {0}",
ex->Message);
}
try
{
// Attempt to access a static AnotherField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
App::typeid->InvokeMember("AnotherField", BindingFlags::Static |
BindingFlags::GetField, nullptr, nullptr, nullptr);
}
catch (MissingMemberException^ ex)
{
// Notice that this code is catching MissingMemberException which is the
// base class of MissingMethodException and MissingFieldException.
// Show the user that the AnotherField field cannot be accessed.
Console::WriteLine("Unable to access the AnotherField field: {0}",
ex->Message);
}
}
// This code produces the following output.
//
// Unable to call the DoSomething method: Method 'App.DoSomething' not found.
// Unable to access the AField field: Field 'App.AField' not found.
// Unable to access the AnotherField field: Field 'App.AnotherField' not found.
继承层次结构
System.Object
System.Exception
System.SystemException
System.MemberAccessException
System.MissingMemberException
System.MissingFieldException
System.MissingMethodException
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
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
请参见
参考
MissingMemberException 成员
System 命名空间
Exception 类
MissingFieldException 类
MissingMethodException