using System;
// Attribute on a class
[Author("Benjamin Franklin")]
public class Attributes
{
// Attribute on a field
[Author(AuthorName = "Benjamin Franklin")]
string book;
// Attribute on a property
[Author("Benjamin Franklin")]
string TheBook
{
get => book;
// Attribute on a property accessor
[Author("Benjamin Franklin")]
set => book = value;
}
// Attribute on a method and argument
[Author("Benjamin Franklin")]
string GetAnotherBook([Author("Benjamin Franklin")] string arg1)
{
return book;
}
}
// Attribute declaration
public class AuthorAttribute : Attribute
{
public string AuthorName { get; set; }
public AuthorAttribute() { }
public AuthorAttribute(string authorName)
{
AuthorName = authorName;
}
}
|
*> Attribute on a class
class-id Attributes
attribute Author(name AuthorName = "Benjamin Franklin").
*> Attribute on a field
01 book string attribute Author(name AuthorName = "Benjamin Franklin").
*> Attribute on a property
01 bookProperty string property
attribute Author(name AuthorName = "Benjamin Franklin").
*> Attribute on a method and argument
method-id GetAnotherBook
attribute Author(name AuthorName = "Benjamin Franklin")
(arg1 as string
attribute Author(name AuthorName = "Benjamin Franklin"))
returning return-value as string.
set return-value to book
end method.
end class.
*> Attribute definition
attribute-id AuthorAttribute public.
01 AuthorName string property.
end attribute.
|
Imports System
' Attribute on a class
<Author("Benjamin Franklin")>
public class Attributes
' Attribute on a field
<Author(AuthorName := "Benjamin Franklin")>
Private book As String
' Attribute on a property
<Author("Benjamin Franklin")>
Property TheBook As String
Get
return book
End Get
' Attribute on a property accessor
<Author("Benjamin Franklin")>
Set(value as String)
book = value
End Set
End Property
' Attribute on a method and argument
<Author("Benjamin Franklin")>
Function GetAnotherBook(
<Author("Benjamin Franklin")> arg1 As String) As String
return book
End Function
End Class
' Attribute declaration
Public Class AuthorAttribute
Inherits Attribute
Public Property AuthorName As String
Public Sub New()
End Sub
Public Sub New(authorName As String)
AuthorName = authorName
End Sub
End Class
|