DefaultMemberAttribute Třída

Definice

Definuje člena typu, který je výchozím členem, který InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])používá .

public ref class DefaultMemberAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
public sealed class DefaultMemberAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
[System.Serializable]
public sealed class DefaultMemberAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DefaultMemberAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
type DefaultMemberAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
[<System.Serializable>]
type DefaultMemberAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DefaultMemberAttribute = class
    inherit Attribute
Public NotInheritable Class DefaultMemberAttribute
Inherits Attribute
Dědičnost
DefaultMemberAttribute
Atributy

Příklady

Následující příklad používá DefaultMemberAttribute atribut k vytvoření Age vlastnosti jako výchozího člena MyClass třídy.

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 );
   }
}

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);
        }
    }
}
Imports System.Reflection
Imports System.IO

<DefaultMemberAttribute("Age")> Public Class [MyClass]

    Public Sub Name(ByVal s As String)
    End Sub

    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
End Class

Poznámky

Vlastnost se importuje jako indexer (výchozí indexovaná vlastnost v Visual Basic), pokud vlastnost obsahuje argumenty a pokud název vlastnosti nebo některého z jeho přístupových objektů odpovídá názvu zadanému parametrem DefaultMemberAttribute. DefaultMemberAttribute Pokud není k dispozici u typu obsahujícího, typ nemá indexer. Kompilátor jazyka C# vygeneruje DefaultMemberAttribute libovolný typ obsahující indexer. V jazyce C# je chyba ručního atributu DefaultMemberAttribute typu s typem, pokud typ také deklaruje indexer.

Konstruktory

DefaultMemberAttribute(String)

Inicializuje novou instanci DefaultMemberAttribute třídy.

Vlastnosti

MemberName

Získá název z atributu.

TypeId

Při implementaci v odvozené třídě získá jedinečný identifikátor pro tento Attribute.

(Zděděno od Attribute)

Metody

Equals(Object)

Vrací hodnotu, která určuje, zda je tato instance rovna zadanému objektu.

(Zděděno od Attribute)
GetHashCode()

Vrátí hodnotu hash pro tuto instanci.

(Zděděno od Attribute)
GetType()

Type Získá aktuální instanci.

(Zděděno od Object)
IsDefaultAttribute()

Při přepsání v odvozené třídě určuje, zda hodnota této instance je výchozí hodnotou odvozené třídy.

(Zděděno od Attribute)
Match(Object)

Při přepsání v odvozené třídě vrátí hodnotu, která označuje, zda se tato instance rovná zadanému objektu.

(Zděděno od Attribute)
MemberwiseClone()

Vytvoří použádnou kopii aktuálního souboru Object.

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Explicitní implementace rozhraní

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Mapuje sadu názvů na odpovídající sadu identifikátorů pro rozesílání.

(Zděděno od Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Načte informace o typu objektu, který lze použít k získání informací o typu rozhraní.

(Zděděno od Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Získá počet rozhraní typu informací, které objekt poskytuje (0 nebo 1).

(Zděděno od Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Poskytuje přístup k vlastnostem a metodám vystaveným objektem.

(Zděděno od Attribute)

Platí pro