24.3 Attribute instances

本文详细介绍了在编译阶段和运行阶段如何处理属性实例的过程。包括如何定义属性类,初始化属性实例,并在运行时获取这些实例。还讨论了在编译阶段处理对象创建表达式的方法以及运行时如何实例化属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
24.3.1 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 constructor on T
that can be invoked at run-time. Call this instance constructor C.
? If C does not have public accessibility, then a compile-time error occurs.
? For each named-argument Arg in N:
Let Name be the identifier of the named-argument Arg.
Name must identify a non-static read-write public field or property on T.
If T has no such field or property,
then a compile-time error occurs.
? Keep the following information for run-time instantiation of the
attribute: the attribute class T, the
instance constructor C on T, the positional-argument-list P and the
named-argument-list N.
24.3.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-argumentlist
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 constructor C as determined at compile-time.
These steps either result in
an exception, or produce an instance of T. Call this instance O.
? For each named-argument Arg in N, in order:
C# LANGUAGE SPECIFICATION
312
Let Name be the identifier of the named-argument Arg. If Name does not
identify a non-static public readwrite
field or property on O, then an exception is thrown.
Let Value be the result of evaluating the attribute-argument-expression of
Arg.
If Name identifies a field on O, then set this field to the value Value.
Otherwise, Name identifies a property on O. Set this property to the value
Value.
The result is O, an instance of the attribute class T that has been
initialized with the positional-argumentlist
P and the named-argument-list N.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值