Questions tagged [.net-attributes]

The tag `.Net attributes` describes anything related to using or creating attributes in .net source code. It is specifically meant for classes derived from the `System.Attribute` type.

.Net attributes describe the creation and usage of attribute classes in .Net program code (C#, VB.Net or otherwise). .Net attributes are derived of the System.Attribute class, and can be applied to methods, classes or assemblies.

Assembly version is described using Assembly attributes:

// Set version number for the assembly.
[assembly:AssemblyVersionAttribute("4.3.2.1")]

Classes can be decorated with attributes to denote special usage:

[TestClass]
public class MyTests 
{
    ..
}

Methods can be decorated using attributes:

[TestMethod]
public void MyFirstUnitTest() { .. }

Even parameters can be decorated using attributes.

33 questions
798
votes
32 answers

Most Useful Attributes

I know that attributes are extremely useful. There are some predefined ones such as [Browsable(false)] which allows you to hide properties in the properties tab. Here is a good question explaining attributes: What are attributes in .NET? What are…
wusher
  • 12,291
  • 22
  • 72
  • 95
571
votes
27 answers

Getting attributes of Enum's value

I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following enum: using System.ComponentModel; // for DescriptionAttribute enum FunkyAttributesEnum { …
Alex K
  • 10,835
  • 8
  • 29
  • 34
553
votes
9 answers

Why does C# forbid generic attribute types?

This causes a compile-time exception: public sealed class ValidatesAttribute : Attribute { } [Validates] public static class StringValidation { } I realize C# does not support generic attributes. However, after much Googling, I can't…
Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
355
votes
3 answers

What does [STAThread] do?

I am learning C# 3.5 and I want to know what [STAThread] does in our programs?
odiseh
  • 25,407
  • 33
  • 108
  • 151
275
votes
11 answers

Find a private field with Reflection?

Given this class class Foo { // Want to find _bar with reflection [SomeAttribute] private string _bar; public string BigBar { get { return this._bar; } } } I want to find the private item _bar that I will mark with…
David Basarab
  • 72,212
  • 42
  • 129
  • 156
209
votes
11 answers

What are attributes in .NET?

What are attributes in .NET, what are they good for, and how do I create my own attributes?
Corey
  • 14,101
  • 7
  • 38
  • 35
153
votes
4 answers

displayname attribute vs display attribute

What is difference between DisplayName attribute and Display attribute in ASP.NET MVC?
22
votes
1 answer

What does the [ApiController] attribute do?

I've noticed it is the same thing if this attribute is used or not. Am I wrong? As an example: [Route("[controller]")] [ApiController] public class DataTablesController: ControllerBase { [HttpGet] public IActionResult Test() { …
18
votes
5 answers

Enforce Attribute Decoration of Classes/Methods

Following on from my recent question on Large, Complex Objects as a Web Service Result. I have been thinking about how I can ensure all future child classes are serializable to XML. Now, obviously I could implement the IXmlSerializable interface and…
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
15
votes
3 answers

Can I fail to deserialize with XmlSerializer in C# if an element is not found?

I am using XmlSerializer to write and read an object to xml in C#. I currently use the attributes XmlElement and XmlIgnore to manipulate the serialization of the object. If my xml file is missing an xml element that I require, my object still…
Alex B
  • 24,678
  • 14
  • 64
  • 87
12
votes
3 answers

Curious ambiguity in attribute specification (two using directives)

Background: In an attribute specification, there is sometimes two valid ways to write the applied attribute. For example, if an attribute class has the name HorseAttribute, you can apply the attribute as either [HorseAttribute] or just [Horse].…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
11
votes
1 answer

ResolutionGroupName and multiple effects

I have created a folder called Effects, which should contain all the effects for my project. It has one effect in it with the following attribution: [assembly: ResolutionGroupName("Effects")] [assembly:…
testing
  • 19,681
  • 50
  • 236
  • 417
8
votes
2 answers

Attribute with params object[] constructor gives inconsistent compiler errors

I am getting the error An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type Notice the screenshot below: Notice that if I use the DataRow attribute with one or three…
Dan Friedman
  • 4,941
  • 2
  • 41
  • 65
8
votes
7 answers

Real world use of custom .NET attributes

What kind of things have you used custom .NET attributes for in the real world? I've read several articles about them, but I have never used custom attributes. I feel like I might be overlooking them when they could be useful. I am talking about…
TWA
  • 12,756
  • 13
  • 56
  • 92
6
votes
1 answer

What is the "module" keyword in C# .NET?

I am learning C# and came across the keyword module. I would like to know what this module keyword in C# is and how it is useful. For example, consider the below code: [module: Test] public class TestAttribute : Attribute { }
gnagesh
  • 83
  • 6
1
2 3