访问默认成员
任何类型都可以具有默认成员,即在未给定任何成员名称时调用的成员。 可通过调用 Type.InvokeMember 方法(以 String.Empty ("") 作为成员名称)来调用默认成员。 InvokeMember 将从类型中检索 System.Reflection.DefaultMemberAttribute 特性,然后调用它。 下面的示例调用 Class1 的默认成员,并将它返回的值赋给 o。
Dim c As New Class1()
Dim o As Object
o = c.GetType().InvokeMember("", BindingFlags.InvokeMethod, Nothing, c, New Object(){})
Console.WriteLine("Default member result: {0}", o)
Class1 c = new Class1();
object o;
o = c.GetType().InvokeMember("", BindingFlags.InvokeMethod, null, c, new object[0]);
Console.WriteLine("Default member result: {0}", o);
Class1^ c = gcnew Class1();
Object^ o;
o = c->GetType()->InvokeMember("", BindingFlags::InvokeMethod, nullptr, c, gcnew array<Object^>(0));
Console::WriteLine("Default member result: {0}", o);
默认成员由声明类型上的 DefaultMemberAttribute 特性表示。 下面示例中显示的类手动添加了 DefaultMemberAttribute。 如果类声明了索引器,请勿手动添加 DefaultMemberAttribute;在该情况下,编译器会自动添加该特性。
<DefaultMember("GetIVal")> _
Public Class Class1
Private ival As Integer
Private sval As String
Public Sub New()
ival = 5050
sval = "6040"
End Sub
Public Function GetIVal() As Integer
Return ival
End Function
Public Function GetSVal() As String
Return sval
End Function
End Class
[DefaultMember("GetIVal")]
public class Class1
{
private int ival;
private string sval;
public Class1()
{
ival = 5050;
sval = "6040";
}
public int GetIVal()
{
return ival;
}
public string GetSVal()
{
return sval;
}
}
[DefaultMember("GetIVal")]
public ref class Class1
{
private:
int ival;
String^ sval;
public:
Class1()
{
ival = 5050;
sval = "6040";
}
int GetIVal()
{
return ival;
}
String^ GetSVal()
{
return sval;
}
};
下面的示例显示如何通过检索默认成员的自定义特性来检索默认成员。
Dim classType As Type = GetType(Class1)
Dim attribType As Type = GetType(DefaultMemberAttribute)
Dim defMem As DefaultMemberAttribute = _
CType(Attribute.GetCustomAttribute(CType(classType, MemberInfo), attribType), _
DefaultMemberAttribute)
Dim memInfo() As MemberInfo = classType.GetMember(defMem.MemberName)
Type classType = typeof(Class1);
Type attribType = typeof(DefaultMemberAttribute);
DefaultMemberAttribute defMem =
(DefaultMemberAttribute)Attribute.GetCustomAttribute((MemberInfo)classType, attribType);
MemberInfo[] memInfo = classType.GetMember(defMem.MemberName);
Type^ classType = Class1::typeid;
Type^ attribType = DefaultMemberAttribute::typeid;
DefaultMemberAttribute^ defMem =
(DefaultMemberAttribute^)Attribute::GetCustomAttribute((MemberInfo^)classType, attribType);
array<MemberInfo^>^ memInfo = classType->GetMember(defMem->MemberName);
使用 Type.GetDefaultMembers 方法会生成相同的结果,但可能更简单一些。 但是,如果在类型上定义了多个默认成员,则 GetDefaultMembers 会引发 InvalidOperationException。 下面的示例演示 GetDefaultMembers 的语法。
Dim t As Type = GetType(Class1)
Dim memInfo() As MemberInfo = t.GetDefaultMembers()
Type t = typeof(Class1);
MemberInfo[] memInfo = t.GetDefaultMembers();
Type^ t = Class1::typeid;
array<MemberInfo^>^ memInfo = t->GetDefaultMembers();
您还可以使用 GetCustomAttributes 方法获得类型的自定义特性,并仅选择 DefaultMemberAttribute。 下面的示例演示这一技术。
Dim t As Type = GetType(Class1)
Dim customAttribs() As Object _
= t.GetCustomAttributes(GetType(DefaultMemberAttribute), False)
If customAttribs.Length > 0 Then
Dim defMem As DefaultMemberAttribute = CType(customAttribs(0), DefaultMemberAttribute)
Dim memInfo() As MemberInfo = t.GetMember(defMem.MemberName)
If memInfo.Length > 0 Then
Console.WriteLine("Default Member: {0}", memInfo(0).Name)
End If
End If
Type t = typeof(Class1);
object[] customAttribs = t.GetCustomAttributes(typeof(DefaultMemberAttribute), false);
if (customAttribs.Length > 0)
{
DefaultMemberAttribute defMem = (DefaultMemberAttribute)customAttribs[0];
MemberInfo[] memInfo = t.GetMember(defMem.MemberName);
if (memInfo.Length > 0)
{
Console.WriteLine("Default Member: {0}", memInfo[0].Name);
}
}
Type^ t = Class1::typeid;
array<Object^>^ customAttribs = t->GetCustomAttributes(DefaultMemberAttribute::typeid, false);
if (customAttribs->Length > 0)
{
DefaultMemberAttribute^ defMem = (DefaultMemberAttribute^)customAttribs[0];
array<MemberInfo^>^ memInfo = t->GetMember(defMem->MemberName);
if (memInfo->Length > 0)
{
Console::WriteLine("Default Member: {0}", memInfo[0]->Name);
}
}