As a web developer, I know what data annotations are and how they work in ASP.NET, but my question is how do they work behind the scenes? What happens in code when we use them?
For example:
public class C
{
[Required]
public string Name {get; set;}
}
Compiles to:
.class private auto ansi '<Module>'
{
} // end of class <Module>
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Fields
.field private string '<Name>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Methods
.method public hidebysig specialname
instance string get_Name () cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2050
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld string C::'<Name>k__BackingField'
IL_0006: ret
} // end of method C::get_Name
.method public hidebysig specialname
instance void set_Name (
string 'value'
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2058
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld string C::'<Name>k__BackingField'
IL_0007: ret
} // end of method C::set_Name
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2061
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method C::.ctor
// Properties
.property instance string Name()
{
.custom instance void [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.RequiredAttribute::.ctor() = (
01 00 00 00
)
.get instance string C::get_Name()
.set instance void C::set_Name(string)
}
}
// end of class C
First of all what is .custom instance void......??
Second of all how c# works with this .custom.. in order to know that property is required in this case or not?