作为一名Web开发人员,我知道什么是数据注释以及它们如何在ASP.NET中工作,但是我的问题是它们如何在后台工作?当我们使用它们时,代码会发生什么?
例如:
public class C
{
[Required]
public string Name {get; set;}
}
编译为:
.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
首先,什么是.custom实例void ...... ??
第二个c#如何与该.custom ..一起工作以了解在这种情况下是否需要该属性?