1

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?

  • 1
    C# doesn't know what attributes mean, it just adds them as metadata to the generated IL. It's other class that inspect this metadata, and act accordingly. – CodeCaster Jul 14 '19 at 13:13
  • those weren't my question but thanks – Mehrad Sharifi Jul 14 '19 at 14:11
  • How MVC deals with data annotations is explained in the duplicates I linked. If those don't answer your question, then please [edit] your question to explain what you want to know. – CodeCaster Jul 15 '19 at 07:32

0 Answers0