attributes attribute instances.md - brainchildservices/curriculum GitHub Wiki
Slide 1
Attribute instances
An attribute instance is an instance that represents an attribute at run-time. An attribute is defined with an attribute class, positional arguments, and named arguments. An attribute instance is an instance of the attribute class that is initialized with the positional and named arguments.
Retrieval of an attribute instance involves both compile-time and run-time processing, as described in the following sections.
Compilation of an attribute
The compilation of an attribute with attribute class T
, positional_argument_list P
and named_argument_list N
, consists of the following steps:
- Follow the compile-time processing steps for compiling an object_creation_expression of the form
new T(P)
. These steps either result in a compile-time error, or determine an instance constructorC
onT
that can be invoked at run-time. - If
C
does not have public accessibility, then a compile-time error occurs. - For each named_argument
Arg
inN
:- Let
Name
be the identifier of the named_argumentArg
. Name
must identify a non-static read-write public field or property onT
. IfT
has no such field or property, then a compile-time error occurs.
- Let
- Keep the following information for run-time instantiation of the attribute: the attribute class
T
, the instance constructorC
onT
, the positional_argument_listP
and the named_argument_listN
.
Slide 2
Run-time retrieval of an attribute instance
Compilation of an attribute yields an attribute class T
, an instance constructor C
on T
, a positional_argument_list P
, and a named_argument_list N
. Given this information, an attribute instance can be retrieved at run-time using the following steps:
- Follow the run-time processing steps for executing an object_creation_expression of the form
new T(P)
, using the instance constructorC
as determined at compile-time. These steps either result in an exception, or produce an instanceO
ofT
. - For each named_argument Arg in N, in order:
- Let
Name
be the identifier of the named_argumentArg
. IfName
does not identify a non-static public read-write field or property onO
, then an exception is thrown. - Let
Value
be the result of evaluating the attribute_argument_expression ofArg
. - If
Name
identifies a field onO
, then set this field toValue
. - Otherwise,
Name
identifies a property onO
. Set this property toValue
. - The result is
O
, an instance of the attribute classT
that has been initialized with the positional_argument_listP
and the named_argument_listN
.
- Let
Ref:- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes