DefaultMemberAttribute 类
定义某类型的成员,该成员是 InvokeMember 使用的默认成员。
**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Interface)> _
Public NotInheritable Class DefaultMemberAttribute
Inherits Attribute
用法
Dim instance As DefaultMemberAttribute
[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface)]
public sealed class DefaultMemberAttribute : Attribute
[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Interface)]
public ref class DefaultMemberAttribute sealed : public Attribute
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface) */
public final class DefaultMemberAttribute extends Attribute
SerializableAttribute
ComVisibleAttribute(true)
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface)
public final class DefaultMemberAttribute extends Attribute
备注
如果属性带有参数,或者属性的名称或其某个访问器的名称与 DefaultMemberAttribute 属性指定的名称匹配,则该属性作为索引器导入(Visual Basic 中的默认索引属性)。如果包含类型中不存在 DefaultMemberAttribute,则该类型没有索引器。C# 编译器对包含索引器的任何类型发出 DefaultMemberAttribute。在 C# 中,如果某个类型已经声明了索引器,则使用 DefaultMemberAttribute 手动为该类型指定属性是一种错误。
示例
Imports System
Imports System.Reflection
Imports System.IO
Imports Microsoft.VisualBasic
<DefaultMemberAttribute("Age")> Public Class [MyClass]
Public Sub Name(ByVal s As String)
End Sub 'Name
Public ReadOnly Property Age() As Integer
Get
Return 20
End Get
End Property
Public Shared Sub Main()
Try
Dim myType As Type = GetType([MyClass])
Dim memberInfoArray As MemberInfo() = myType.GetDefaultMembers()
If memberInfoArray.Length > 0 Then
Dim memberInfoObj As MemberInfo
For Each memberInfoObj In memberInfoArray
Console.WriteLine("The default member name is: " + memberInfoObj.ToString())
Next memberInfoObj
Else
Console.WriteLine("No default members are available.")
End If
Catch e As InvalidOperationException
Console.WriteLine("InvalidOperationException: " + e.Message)
Catch e As IOException
Console.WriteLine("IOException: " + e.Message)
Catch e As Exception
Console.WriteLine("Exception: " + e.Message)
End Try
End Sub 'Main
End Class '[MyClass]
using System;
using System.Reflection;
using System.IO;
[DefaultMemberAttribute("Age")]
public class MyClass
{
public void Name(String s) {}
public int Age
{
get
{
return 20;
}
}
public static void Main()
{
try
{
Type myType = typeof(MyClass);
MemberInfo[] memberInfoArray = myType.GetDefaultMembers();
if (memberInfoArray.Length > 0)
{
foreach(MemberInfo memberInfoObj in memberInfoArray)
{
Console.WriteLine("The default member name is: " + memberInfoObj.ToString());
}
}
else
{
Console.WriteLine("No default members are available.");
}
}
catch(InvalidOperationException e)
{
Console.WriteLine("InvalidOperationException: " + e.Message);
}
catch(IOException e)
{
Console.WriteLine("IOException: " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
}
using namespace System;
using namespace System::Reflection;
using namespace System::IO;
[DefaultMemberAttribute("Age")]
public ref class MyClass
{
public:
void Name( String^ s ){}
property int Age
{
int get()
{
return 20;
}
}
};
int main()
{
try
{
Type^ myType = MyClass::typeid;
array<MemberInfo^>^memberInfoArray = myType->GetDefaultMembers();
if ( memberInfoArray->Length > 0 )
{
System::Collections::IEnumerator^ myEnum = memberInfoArray->GetEnumerator();
while ( myEnum->MoveNext() )
{
MemberInfo^ memberInfoObj = safe_cast<MemberInfo^>(myEnum->Current);
Console::WriteLine( "The default member name is: {0}", memberInfoObj );
}
}
else
{
Console::WriteLine( "No default members are available." );
}
}
catch ( InvalidOperationException^ e )
{
Console::WriteLine( "InvalidOperationException: {0}", e->Message );
}
catch ( IOException^ e )
{
Console::WriteLine( "IOException: {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
import System.*;
import System.Reflection.*;
import System.IO.*;
/** @attribute DefaultMemberAttribute("Age")
*/
public class MyClass
{
public void Name(String s)
{
} //Name
/** @property
*/
public int get_Age()
{
return 20;
} //get_Age
public static void main(String[] args)
{
try {
Type myType = MyClass.class.ToType();
MemberInfo memberInfoArray[] = myType.GetDefaultMembers();
if (memberInfoArray.length > 0) {
for (int iCtr = 0; iCtr < memberInfoArray.length; iCtr++) {
MemberInfo memberInfoObj = memberInfoArray[iCtr];
Console.WriteLine("The default member name is: "
+ memberInfoObj.ToString());
}
}
else {
Console.WriteLine("No default members are available.");
}
}
catch (InvalidOperationException e) {
Console.WriteLine("InvalidOperationException: " + e.get_Message());
}
catch (IOException e) {
Console.WriteLine("IOException: " + e.get_Message());
}
catch (System.Exception e) {
Console.WriteLine("Exception: " + e.get_Message());
}
} //main
} //MyClass
继承层次结构
System.Object
System.Attribute
System.Reflection.DefaultMemberAttribute
线程安全
此类型的任何公共静态(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