默认属性列表应该出现在我的 c# .net 类定义中
我有一个员工类:
public class Employee
{
public string Name { get; set; }
public Int32 Salary { get; set; }
public DateTime DateOfBirth { get; set; }
}
并且我创建了一个自定义属性 CustomeAttribute
因为
public class CustomAttributes : Attribute
{
public int Sequence {set;get;}
public int Length { set; get; }
}
我使用 CustomAttribute
作为 Employee
中的属性,因为
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
添加到“Employee”,因为
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;}
}
缺少属性,我应该会收到编译时错误。这将确保写作 该类的每个属性的属性值以及来自同一程序集的类的属性值。
对于没有属性值的属性,我应该得到编译时错误。
I have a employee class:
public class Employee
{
public string Name { get; set; }
public Int32 Salary { get; set; }
public DateTime DateOfBirth { get; set; }
}
and I have created a custom Attribute CustomeAttribute
as
public class CustomAttributes : Attribute
{
public int Sequence {set;get;}
public int Length { set; get; }
}
I am using CustomAttribute
as attribute in 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; }
}
I want to validate attribute collection should be there for each property definition.
If I add new property Age
to ´Employee` 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;}
}
I should get compile time error as attributes are missing. Which will ensure writing
Attribute values to each of property of a that class as well to a class which is from same assembly.??
I should get compile time error for a property without attribute values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有办法为此生成编译时错误。但是,您可以在单元测试或
#debug
构建中通过反射执行某些操作 - 只需迭代属性,检查Attribute.IsDefined(property, typeof(CustomAttributes))
There is no way of generating a compile-time error for this. However, you could do something with reflection, in either your unit tests or the
#debug
build - simply iterate over the properties, checkingAttribute.IsDefined(property, typeof(CustomAttributes))