默认属性列表应该出现在c#.net中我的类定义中
我有一个雇员类:默认属性列表应该出现在c#.net中我的类定义中,.net,attributes,propertydescriptor,.net,Attributes,Propertydescriptor,我有一个雇员类: public class Employee { public string Name { get; set; } public Int32 Salary { get; set; } public DateTime DateOfBirth { get; set; } } 我创建了一个自定义属性CustomeAttributeas public class CustomAttributes : Attribute {
public class Employee
{
public string Name { get; set; }
public Int32 Salary { get; set; }
public DateTime DateOfBirth { get; set; }
}
我创建了一个自定义属性CustomeAttribute
as
public class CustomAttributes : Attribute
{
public int Sequence {set;get;}
public int Length { set; get; }
}
我正在使用CustomAttribute
作为Employee
as中的属性
public class Employee
{
public string Name { get; set; }
public Int32 Salary { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class Employee
{
[CustomAttributes(Sequence=1,Length=10)]
public string Name { get; set; }
[CustomAttributes(Sequence = 2, Length= 6)]
public Int32 Salary { get; set; }
[CustomAttributes(Sequence =3, Length = 8)]
public DateTime DateOfBirth { get; set; }
}
我想验证属性集合是否应该存在于每个属性定义中。
如果我将新属性Age
添加到“员工”as
public class Employee
{
[CustomAttributes(Sequence=1,Length=10)]
public string Name { get; set; }
[CustomAttributes(Sequence = 2, Length= 6)]
public Int32 Salary { get; set; }
[CustomAttributes(Sequence =3, Length = 8)]
public DateTime DateOfBirth { get; set; }
public int Age {get;set;}
}
我应该得到编译时错误,因为属性丢失。这将确保写作
该类的每个属性以及来自同一程序集的类的属性值
我应该为没有属性值的属性获取编译时错误。无法为此生成编译时错误。但是,您可以在单元测试或
#debug
构建中使用反射进行操作-只需迭代属性,检查属性.IsDefined(property,typeof(CustomAttributes))