OptionalAttribute Klasa

Definicja

Wskazuje, że parametr jest opcjonalny.

public ref class OptionalAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)]
public sealed class OptionalAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class OptionalAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)>]
type OptionalAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type OptionalAttribute = class
    inherit Attribute
Public NotInheritable Class OptionalAttribute
Inherits Attribute
Dziedziczenie
OptionalAttribute
Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano, jak zastosować OptionalAttribute atrybut do parametru metody napisanej w języku C#.

using System;
using System.Runtime.InteropServices;

public class Program
{
    public static void MethodWithOptionalAttribute([Optional] string str)
    {
        Console.WriteLine($"str is null: {str == null}");
    }

    public static void Main()
    {
        MethodWithOptionalAttribute(); // str is null: True
        MethodWithOptionalAttribute("abc"); // str is null: False
    }
}

Uwagi

Ten atrybut można zastosować do parametrów.

Parametry opcjonalne nie są obsługiwane przez wszystkie języki.

OptionalAttribute jest czasami używany w połączeniu z DefaultParameterValueAttribute.

Konstruktory

OptionalAttribute()

Inicjuje nowe wystąpienie klasy OptionalAttribute z domyślnymi wartościami.

Właściwości

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego Attributeelementu .

(Odziedziczone po Attribute)

Metody

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

_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)

Dotyczy