.NET 6 SDK Advanced Exploration: Attributes

.NET 6 SDK Advanced Exploration: Attributes

1h 29mAdvanced2022-05-18

Authors

Walt Ritscher

Walt Ritscher

Programmer, UI Designer, Staff Instructor at LinkedIn Learning

Course details

Attributes add metadata to your programs and allow you to annotate given types, assemblies, modules, and methods. They’re especially handy if you’re an advanced .NET developer, as they lend your code base more declarative power with the additional services of .NET tools and code libraries. In this course, senior LinkedIn Learning instructor Walt Ritscher shows you how to use predefined, standard .NET attributes as well as how to create custom attributes on your own.

Learn about the features of attributes, how they function, and why they’re such a vital part of .NET programs. Get quick tips and user tricks on annotating your code like a seasoned pro, from testing for object quality to marking your testing methods. Along the way, Walt teaches you how to create custom attributes that meet the specific needs of your projects, such as attributes for the JSON serializer, PropertyGrid and the category attribute, the BindProperty attribute, and more.

Skills covered

.NETSoftware Development ToolsMicrosoftSoftware DevelopmentOne-Off

Concepts

Introduction

  • Use declarative approach to code with .NET attributes
  • What you should know
  • My programming setup and configuration
  • How to access the sample code on GitHub

Overview

  • Metadata and .NET assemblies
  • Metadata consumers
  • What is an attribute
  • Examine the .NET attribute classes
  • Reading metadata and code with reflection

Use an Attribute

  • Use the Obsolete attribute
  • See the attribute metadata in the .NET assembly
  • Attribute constructors and properties
  • Apply multiple attributes

Create a Custom JSON Serializer Attribute

  • Examine the JSON serializer project
  • Create the attribute
  • Use the attribute in another project
  • Write reflection code to discover the attribute
  • Create a parameterized attribute
  • Find parameters with reflection

Create a Custom Unit Test Framework

  • Examine the zUnit projects
  • Explore the unit test attributes
  • Use the unit test attributes
  • Load attributes
  • Show the attribute metadata
  • Run the unit tests
  • Create an Assert class

Examples of .NET Attributes

  • Bit field enums and the flags attribute
  • PropertyGrid and the Category attribute
  • The BindProperty attribute in ASP.NET Razor Pages
  • Logging and the CallerMemberName attribute

Conclusion

  • Next steps
40,000 Toman