DefaultMemberAttribute Class
Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface)] public sealed class DefaultMemberAttribute : 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
A property is imported as an indexer (default indexed property in Visual Basic) if the property has arguments and if the name of the property or one of its accessors matches the name specified by the DefaultMemberAttribute. If the DefaultMemberAttribute is not present on the containing type, then the type does not have an indexer. The C# compiler emits the DefaultMemberAttribute on any type containing an indexer. In C# it is an error to manually attribute a type with the DefaultMemberAttribute if the type also declares an indexer.
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); } } }
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
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.