مشاركة عبر


الوصول إلى الافتراضي الأعضاء

يمكن أن يكون لأي نوع عضو افتراضي، الذي هو عضو الذي هو استدعاؤه عند عدم وجود اسم العضو هو المعطى. The following مثال invokes the الافتراضي عضو of Class1, و the القيمة it إرجاع هو تعيين إلى i.

Dim i As Integer
Dim c As New Class1()
i = Convert.ToInt32(c)

الافتراضي الأعضاء are marked مع the System.Reflection.DefaultMemberAttribute. The following مثال shows how إلى retrieve the الافتراضي عضو بواسطة retrieving the مخصص سمة for the الافتراضي عضو.

Dim t As Type = GetType(DefaultMemberAttribute)
Dim defMem As DefaultMemberAttribute = CType(Attribute.GetCustomAttribute([Assembly].GetAssembly(t), t), DefaultMemberAttribute)
Dim memInfo As MemberInfo() = t.GetMember(defMem.MemberName)
Type t = typeof(DefaultMemberAttribute);
DefaultMemberAttribute defMem = (DefaultMemberAttribute)Attribute.GetCustomAttribute(Assembly.GetAssembly(t), t);
MemberInfo[] memInfo = t.GetMember(defMem.MemberName);

It might be simpler إلى استخدم Type.GetDefaultMembers, which yields exactly the same النتيجة. However, GetDefaultMembers throws an InvalidOperationException if there هو المزيد واحد الافتراضي عضو defined تشغيل the نوع. The following تعليمات برمجية مثال shows the بناء الجملة for GetDefaultMembers.

Dim memInfo As MemberInfo() = t.GetDefaultMembers()
MemberInfo[] memInfo = t.GetDefaultMembers();

You can invoke الافتراضي الأعضاء بواسطة calling Type.InvokeMember مع String.Empty ("") كـ the عضو اسم. InvokeMember retrieves the DefaultMemberAttribute من the نوع و then invokes it.

راجع أيضًا:

المرجع

DefaultMemberAttribute

Type.GetDefaultMembers

المبادئ

عرض معلومات نوع