Attribute 构造函数

定义

初始化 Attribute 类的新实例。

C#
protected Attribute();

示例

下面的代码示例演示自定义参数 Attribute 类及其构造函数的定义。

C#
// Define a custom parameter attribute that takes a single message argument.
[AttributeUsage( AttributeTargets.Parameter )]
public class ArgumentUsageAttribute : Attribute
{
    // This is the attribute constructor.
    public ArgumentUsageAttribute( string UsageMsg )
    {
        this.usageMsg = UsageMsg;
    }

    // usageMsg is storage for the attribute message.
    protected string usageMsg;

    // This is the Message property for the attribute.
    public string Message
    {
        get { return usageMsg; }
        set { usageMsg = value; }
    }
}
F#
// Define a custom parameter attribute that takes a single message argument.
[<AttributeUsage(AttributeTargets.Parameter); AllowNullLiteral>]
type ArgumentUsageAttribute(usageMsg) =
    inherit Attribute()
    
    // This is the Message property for the attribute.
    member val Message = usageMsg with get, set
VB
' Define a custom parameter attribute that takes a single message argument.
<AttributeUsage(AttributeTargets.Parameter)>  _
Public Class ArgumentUsageAttribute
    Inherits Attribute
       
    ' This is the attribute constructor.
    Public Sub New(UsageMsg As String)
        Me.usageMsg = UsageMsg
    End Sub

    ' usageMsg is storage for the attribute message.
    Protected usageMsg As String
       
    ' This is the Message property for the attribute.
    Public Property Message() As String
        Get
            Return usageMsg
        End Get
        Set
            usageMsg = value
        End Set
    End Property
End Class

注解

此构造函数仅由派生自 Attribute的类调用。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0