24.4 Reserved attributes

本文深入探讨了C#中三种关键属性:AttributeUsageAttribute用于描述属性类的使用方式;ConditionalAttribute定义了条件方法;ObsoleteAttribute标记已弃用的类型或成员。通过具体示例解释了这些属性如何工作。
A small number of attributes affect the language in some way. These
attributes include:
? System.AttributeUsageAttribute (§24.4.1), which is used to describe the
ways in which an
attribute class can be used.
? System.ConditionalAttribute (§24.4.2), which is used to define
conditional methods.
? System.ObsoleteAttribute (§24.4.3), which is used to mark a member as
obsolete.
24.4.1 The AttributeUsage attribute
The attribute AttributeUsage is used to describe the manner in which the
attribute class can be used.
A class that is decorated with the AttributeUsage attribute must derive
from System.Attribute, either
directly or indirectly. Otherwise, a compile-time error occurs.
[Note: For an example of using this attribute, see §24.1.1. end note]
24.4.2 The Conditional attribute
The attribute Conditional enables the definition of conditional methods.
The Conditional attribute
indicates a condition by testing a conditional compilation symbol. Calls to
a conditional method are either
included or omitted depending on whether this symbol is defined at the
point of the call. If the symbol is
defined, the call is included; otherwise, the call is omitted.
A conditional method is subject to the following restrictions:
? The conditional method must be a method in a class-declaration or
struct-declaration. A compile-time
error occurs if the Conditional attribute is specified on an interface
method.
? The conditional method must have a return type of void.
? The conditional method must not be marked with the override modifier. A
conditional method may be
marked with the virtual modifier, however. Overrides of such a method are
implicitly conditional,
and must not be explicitly marked with a Conditional attribute.
? The conditional method must not be an implementation of an interface
method. Otherwise, a compiletime
error occurs.
In addition, a compile-time error occurs if a conditional method is used in
a delegate-creation-expression.
[Example: The example
#define DEBUG
Chapter 24 Attributes
313
using System;
using System.Diagnostics;
class Class1
{
[Conditional("DEBUG")]
public static void M() {
Console.WriteLine("Executed Class1.M");
}
}
class Class2
{
public static void Test() {
Class1.M();
}
}
declares Class1.M as a conditional method. Class2’s Test method calls this
method. Since the
conditional compilation symbol DEBUG is defined, if Class2.Test is called,
it will call M. If the symbol
DEBUG had not been defined, then Class2.Test would not call Class1.M. end
example]
It is important to understand that the inclusion or exclusion of a call to
a conditional method is controlled by
the conditional compilation symbols at the point of the call. [Example: In
the example
// Begin class1.cs
using System;
using System.Diagnostics;
class Class1
{
[Conditional("DEBUG")]
public static void F() {
Console.WriteLine("Executed Class1.F");
}
}
// End class1.cs
// Begin class2.cs


#define DEBUG
class Class2
{
public static void G() {
Class1.F(); // F is called
}
}
// End class2.cs
// Begin class3.cs
#undef DEBUG
class Class3
{
public static void H() {
Class1.F(); // F is not called
}
}
// End class3.cs
the classes Class2 and Class3 each contain calls to the conditional method
Class1.F, which is
conditional based on whether or not DEBUG is defined. Since this symbol is
defined in the context of Class2
but not Class3, the call to F in Class2 is included, while the call to F in
Class3 is omitted. end example]
The use of conditional methods in an inheritance chain can be confusing.
Calls made to a conditional
method through base, of the form base.M, are subject to the normal
conditional method call rules.
[Example: In the example
C# LANGUAGE SPECIFICATION
314
// Begin class1.cs
using System;
using System.Diagnostics;
class Class1
{
[Conditional("DEBUG")]
public virtual void M() {
Console.WriteLine("Class1.M executed");
}
}
// End class1.cs
// Begin class2.cs
using System;
class Class2: Class1
{
public override void M() {
Console.WriteLine("Class2.M executed");
base.M(); // base.M is not called!
}
}
// End class2.cs
// Begin class3.cs
#define DEBUG
using System;
class Class3
{
public static void Test() {
Class2 c = new Class2();
c.M(); // M is called
}
}
// End class3.cs
Class2 includes a call to the M defined in its base class. This call is
omitted because the base method is
conditional based on the presence of the symbol DEBUG, which is undefined.
Thus, the method writes to the
console ?Class2.M executed? only. Judicious use of pp-declarations can
eliminate such problems. end
example]
24.4.3 The Obsolete attribute
The attribute Obsolete is used to mark types and members of types that
should no longer be used.
using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct |
AttributeTargets.Enum | AttributeTargets.Interface |
AttributeTargets.Delegate | AttributeTargets.Method |
AttributeTargets.Constructor | AttributeTargets.Property |
AttributeTargets.Field | AttributeTargets.Event)]
public class ObsoleteAttribute: Attribute
{
public ObsoleteAttribute() {?}
public ObsoleteAttribute(string message) {?}
public ObsoleteAttribute(string message, bool error) {?}
public string Message { get {?} }
public bool IsError{ get {?} }
}
If a program uses a type or member that is decorated with the Obsolete
attribute, then the compiler shall
issue a warning or error in order to alert the developer, so the offending
code can be fixed. Specifically, the
compiler shall issue a warning if no error parameter is provided, or if the
error parameter is provided and has
the value false. The compiler shall issue a compile-time error if the error
parameter is specified and has the
value true.
Chapter 24 Attributes
315
[Example: In the example
[Obsolete("This class is obsolete; use class B instead")]
class A


{
public void F() {}
}
class B
{
public void F() {}
}
class Test
{
static void Main() {
A a = new A(); // warning
a.F();
}
}
the class A is decorated with the Obsolete attribute. Each use of A in Main
results in a warning that
includes the specified message, ?This class is obsolete; use class B
instead.?
end example] 
 
已经博主授权,源码转载自 https://pan.quark.cn/s/053f1da40351 在计算机科学领域,MIPS(Microprocessor without Interlocked Pipeline Stages)被视作一种精简指令集计算机(RISC)的架构,其应用广泛存在于教学实践和嵌入式系统设计中。 本篇内容将深入阐释MIPS汇编语言中涉及数组处理的核心概念与实用操作技巧。 数组作为一种常见的数据结构,在编程中能够以有序化的形式储存及访问具有相同类型的数据元素集合。 在MIPS汇编语言环境下,数组通常借助内存地址与索引进行操作。 以下列举了运用MIPS汇编处理数组的关键要素:1. **数据存储**: - MIPS汇编架构采用32位地址系统,从而能够访问高达4GB的内存容量。 - 数组元素一般以连续方式存放在内存之中,且每个元素占据固定大小的字节空间。 例如,针对32位的整型数组,其每个元素将占用4字节的存储空间。 - 数组首元素的地址被称为基地址,而数组任一元素的地址可通过基地址加上元素索引乘以元素尺寸的方式计算得出。 2. **寄存器运用**: - MIPS汇编系统配备了32个通用寄存器,包括$zero, $t0, $s0等。 其中,$zero寄存器通常用于表示恒定的零值,$t0-$t9寄存器用于暂存临时数据,而$s0-$s7寄存器则用于保存子程序的静态变量或参数。 - 在数组处理过程中,基地址常被保存在$s0或$s1寄存器内,索引则存储在$t0或$t1寄存器中,运算结果通常保存在$v0或$v1寄存器。 3. **数组操作指令**: - **Load/Store指令**:这些指令用于在内存与寄存器之间进行数据传输,例如`lw`指令用于加载32位数据至寄存器,`sw`指令...
根据原作 https://pan.quark.cn/s/cb681ec34bd2 的源码改编 基于Python编程语言完成的飞机大战项目,作为一项期末学习任务,主要呈现了游戏开发的基本概念和技术方法。 该项目整体构成约500行代码,涵盖了游戏的核心运作机制、图形用户界面以及用户互动等关键构成部分。 该项目配套提供了完整的源代码文件、相关技术文档、项目介绍演示文稿以及运行效果展示视频,为学习者构建了一个实用的参考范例,有助于加深对Python在游戏开发领域实际应用的认识。 我们进一步研究Python编程技术在游戏开发中的具体运用。 Python作为一门高级编程语言,因其语法结构清晰易懂和拥有丰富的库函数支持,在开发者群体中获得了广泛的认可和使用。 在游戏开发过程中,Python经常与Pygame库协同工作,Pygame是Python语言下的一款开源工具包,它提供了构建2D游戏所需的基础功能模块,包括窗口系统管理、事件响应机制、图形渲染处理、音频播放控制等。 在"飞机大战"这一具体游戏实例中,开发者可能运用了以下核心知识点:1. **Pygame基础操作**:掌握如何初始化Pygame环境,设定窗口显示尺寸,加载图像和音频资源,以及如何启动和结束游戏的主循环流程。 2. **面向对象编程**:游戏中的飞机、子弹、敌人等游戏元素通常通过类的设计来实现,利用实例化机制来生成具体的游戏对象。 每个类都定义了自身的属性(例如位置坐标、移动速度、生命值状态)和方法(比如移动行为、碰撞响应、状态更新)。 3. **事件响应机制**:Pygame能够捕获键盘输入和鼠标操作事件,使得玩家可以通过按键指令来控制飞机的移动和射击行为。 游戏会根据这些事件的发生来实时更新游戏场景状态。 4. **图形显示与刷新**:...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值