
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to create custom attributes in C#?
Custom attributes that can be used to store declarative information and can be retrieved at run-time.
Let us see how to declare custom attribute.
[AttributeUsage ( AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class DeBugInfo : System.Attribute
For our example, let us construct a custom attribute named DeBugInfo, which stores the information obtained by debugging any program.
The DeBugInfo class has three private properties for storing the first three information and a public property for storing the message. Hence the bug number, developer's name, and date of review are the positional parameters of the DeBugInfo class and the message is an optional or named parameter.
Each attribute must have at least one constructor. Let us see how to create a custom attribute.
Example
//a custom attribute BugFix to be assigned to a class and its members [AttributeUsage ( AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class DeBugInfo : System.Attribute { private int bugNo; private string developer; private string lastReview; public string message; public DeBugInfo(int bg, string dev, string d) { this.bugNo = bg; this.developer = dev; this.lastReview = d; } public int BugNo { get { return bugNo; } } public string Developer { get { return developer; } } public string LastReview { get { return lastReview; } } public string Message { get { return message; } set { message = value; } } }
- Related Articles
- How to construct custom attributes in C#?
- Creating custom attributes with HTML5
- How to use jQuery selectors on custom data attributes using HTML5?
- C++ Program to Create Custom Exception
- How to create custom actionbar in android?
- How to create custom errors in Golang
- How to create a custom object in JavaScript?
- How to create a custom listview in android?
- How to create custom ratings bar in android?
- How to create custom directives in Angular 8?
- How to work with attributes in C#
- How do we embed custom data attributes on all HTML elements?
- How to create a custom jQuery Plugin?
- How to create Custom Cursor using CSS
- How to create a custom unchecked exception in Java?

Advertisements