C#. Attributes

Definition

Attributes – descendants of System.Attribute, some kind of annotations of code, applied for some object.

Standart attributes are

[CLSCompliant] – Common Language Specification

[DLLImport]

[Obsolete] – deprecated

[Serializable]

[NonSerialized]

[WebMethod]

Self made attributes also possible

Attribute consumers

Compiler,

some methods of .NET – for example BinaryFormatter,

CLR

SimpleExample

Writing like this will add errors to Error list because we used Obsolete

 

ShortNames of Attributes

[SerializableAttribute] and [Serializable] the same in C#

Attributes may recieve params in constructor

CustomAttributes

AttributedCarLibrary.cs

use of custom attributes

AttributeLimitations

we can use class AttributeTargets like this

Assembly level attributes

  1. Solution Explorer – > Properties – > AssemblyInfo.cs – fill attributes right in assembly
  2. Also you can write in code before namespace

Reflection Attributes with EarlyBinding

full example

Reflection Attributes with lateBinding

 

This entry was posted in C#. Bookmark the permalink.