Skip to Content
C# 8.0 编程
book

C# 8.0 编程

by Ian Griffiths
May 2025
Intermediate to advanced
800 pages
10h 6m
Chinese
O'Reilly Media, Inc.
Content preview from C# 8.0 编程

第 14 章 属性 属性

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在 .NET 中,您可以用属性来注解组件、类型及其成员。属性的作用是控制或修改框架、工具、编译器或 CLR 的行为。例如,在第 1 章中,我展示了一个使用[TestClass] 属性注释的类。这就告诉了单元测试框架,被注解的类包含了一些要作为测试套件的一部分运行的测试。

属性是信息的被动容器,本身不起任何作用。打个比方,在物理世界中,如果你打印出一张包含目的地和跟踪信息的运输标签,并把它贴在包裹上,那么这张标签本身并不会使包裹到达目的地。只有当包裹到了运输公司手中,这种标签才会发挥作用。当运输公司取走你的包裹时,就会发现标签的存在,并以此来确定包裹的运输路线。因此,标签很重要,但归根结底,它的唯一作用是提供一些系统需要的信息。.NET 属性的工作方式与此相同--只有当某些东西去寻找它们时,它们才会产生作用。有些属性由 CLR 或编译器处理,但这只是少数。大多数属性由框架、库、工具(如单元测试运行程序)或您自己的代码消耗。

应用属性

为了避免在类型系统中引入额外的概念集,.NET 将属性作为 .NET 类型的实例进行建模。要用作属性,类型必须派生自System.Attribute 类,但也可以是完全普通的类型。要应用属性,需要将类型的名称放在方括号中,通常直接放在属性目标的前面。例 14-1展示了微软测试框架中的一些属性。我在类上应用了一个属性,以表明该类包含我想运行的测试,我还在单个方法上应用了属性,告诉测试框架哪些方法代表测试,哪些方法包含在每个测试前运行的初始化代码。

例 14-1. 单元测试类中的属性
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ImageManagement.Tests
{
    [TestClass]
    public class WhenPropertiesRetrieved
    {
        private ImageMetadataReader _reader;

        [TestInitialize]
        public void Initialize()
        {
            _reader = new ImageMetadataReader(TestFiles.GetImage());
        }

        [TestMethod]
        public void ReportsCameraMaker()
        {
            Assert.AreEqual(_reader.CameraManufacturer, "Fabrikam");
        }

        [TestMethod]
        public void ReportsCameraModel()
        {
            Assert.AreEqual(_reader.CameraModel, "Fabrikam F450D");
        }
    }
}

如果您查看大多数属性的文档,您会发现它们的真实名称都以Attribute 结尾。如果没有您在括号中指定名称的类,C# 编译器会尝试追加Attribute ,因此例 14-1中的[TestClass] 属性指的是TestClassAttribute 类。如果确实需要,也可以将类名全拼出来,例如[TestClassAttribute],但更常见的是使用较短的版本。

如果要应用多个属性,有两种选择。您可以提供多组括号,或者将多个属性放在一对括号内,中间用逗号隔开。

某些属性类型可以使用构造函数参数。例如,微软的测试框架包含一个 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C# 编程 10

C# 编程 10

Ian Griffiths
CSS:权威指南,第 5 版

CSS:权威指南,第 5 版

Eric Meyer, Estelle Weyl
学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck

Publisher Resources

ISBN: 9798341659575