DefaultMemberAttribute Klasa
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Definiuje element członkowski typu, który jest domyślnym elementem członkowskim używanym przez InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])program .
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
- Dziedziczenie
- Atrybuty
Przykłady
W poniższym przykładzie użyto atrybutu DefaultMemberAttribute , aby właściwość był Age domyślnym elementem członkowskim MyClass klasy.
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
Uwagi
Właściwość jest importowana jako indeksator (domyślna właściwość indeksowana w Visual Basic), jeśli właściwość ma argumenty, a nazwa właściwości lub jednej z jej metod dostępu jest zgodna z nazwą określoną przez DefaultMemberAttributeelement . Jeśli element DefaultMemberAttribute nie znajduje się w typie zawierającym, typ nie ma indeksatora. Kompilator języka C# emituje element DefaultMemberAttribute na dowolnym typie zawierającym indeksator. W języku C# występuje błąd podczas ręcznego przypisywania typu , DefaultMemberAttribute jeśli typ deklaruje również indeksator.
Konstruktory
| Nazwa | Opis |
|---|---|
| DefaultMemberAttribute(String) |
Inicjuje nowe wystąpienie klasy DefaultMemberAttribute. |
Właściwości
| Nazwa | Opis |
|---|---|
| MemberName |
Pobiera nazwę z atrybutu . |
| TypeId |
Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego Attributeelementu . (Odziedziczone po Attribute) |
Metody
| Nazwa | Opis |
|---|---|
| Equals(Object) |
Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi. (Odziedziczone po Attribute) |
| GetHashCode() |
Zwraca wartość skrótu dla tego wystąpienia. (Odziedziczone po Attribute) |
| GetType() |
Type Pobiera wartość bieżącego wystąpienia. (Odziedziczone po Object) |
| IsDefaultAttribute() |
Podczas zastępowania w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną dla klasy pochodnej. (Odziedziczone po Attribute) |
| Match(Object) |
Po przesłonięciu w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi. (Odziedziczone po Attribute) |
| MemberwiseClone() |
Tworzy płytkią kopię bieżącego Objectelementu . (Odziedziczone po Object) |
| ToString() |
Zwraca ciąg reprezentujący bieżący obiekt. (Odziedziczone po Object) |
Jawne implementacje interfejsu
| Nazwa | Opis |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania. (Odziedziczone po Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Pobiera informacje o typie dla obiektu, który może służyć do pobierania informacji o typie dla interfejsu. (Odziedziczone po Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1). (Odziedziczone po Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt. (Odziedziczone po Attribute) |