默认属性列表应该出现在我的 c# .net 类定义中

发布于 2024-10-12 04:11:05 字数 1458 浏览 4 评论 0原文

我有一个员工类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2024-10-19 04:11:06

没有办法为此生成编译时错误。但是,您可以在单元测试或 #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, checking Attribute.IsDefined(property, typeof(CustomAttributes))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文