[Foo(bla)]
is the syntax for the attribute β additional metadata about a certain type or member (or even the assembly itself, or even parameters). You can write your own attributes, for example, something like:
public class ConfigurationPropertyAttribute : Attribute { public ConfigurationPropertyAttribute(string something) {...} }
the name Attribute
is displayed, so only [ConfigurationProperty]
is required. The string "providers"
used as an argument to the constructor, and you can also use property assignments, for example:
[Foo(123, "abc", Bar = 123)]
looks for the type FooAttribute
or Foo
, with a constructor that accepts int
and string
, and has the Bar
property, which can be assigned an int
.
Most attributes do nothing directly , but you can write code that checks attribute types (through reflection), which is a very convenient way of library code, knowing how to work with the type.
For instance:
[XmlType("abc"), XmlRoot("abc")] public class MyType { [XmlAttribute("name")] public string UserName {get;set;} }
this will reconfigure the XmlSerializer
(which checks the above attributes) to serialize the type:
<abc name="blah"/>
where without attributes it would be:
<MyType><UserName>blah</UserName></MyType>
source share