验证属性

发布于 2024-10-17 03:30:33 字数 697 浏览 5 评论 0原文

我有一个抽象类,它从某个接口继承属性。因此无法验证属性支持字段。那么您知道如何实现自定义属性来验证属性吗?

您获得了抽象类 Collaborator,

public abstract class Collaborator
{
}

然后从某些接口继承它:

interface IPersonInformation
{
    String FirstName { get; set; }
    String LastName { get; set; }
}
interface IRecruitmentInformation
{
    DateTime RecruitmentDate { get; set; }
}
///
public abstract class Collaborator : IPersonInformation, IRecruitmentInformation
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public DateTime RecruitmentDate { get; set; } 
}

因此您无法使用其支持字段来验证 Collaborator 类中的属性 - 它们是自动的。 那么有没有办法使用属性上的属性来验证名称呢?

I have an abstract class, which inherits a property from some interface. So it is impossible to validate the property backing field. So do you have any idea on how to implement custom attributes to validate properties?

You got abstract class Collaborator,

public abstract class Collaborator
{
}

Then you inherit it from some interfaces:

interface IPersonInformation
{
    String FirstName { get; set; }
    String LastName { get; set; }
}
interface IRecruitmentInformation
{
    DateTime RecruitmentDate { get; set; }
}
///
public abstract class Collaborator : IPersonInformation, IRecruitmentInformation
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public DateTime RecruitmentDate { get; set; } 
}

So you cannot validate properties in Collaborator class using their backing field - they are automatic.
So is there a way to use attributes on the properties to validate name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

枕头说它不想醒 2024-10-24 03:30:33
public abstract class Collaborator : IPersonInformation, IRecruitmentInformation
{
    private string firstName;
    public String FirstName
    { 
        get { return firstName; }
        set 
        {

            //validation code

            this.firstName = value;
        }
    }
    public String LastName { get; set; }
    public DateTime RecruitmentDate { get; set; } 
}

作为实现接口的一种方式应该可以很好地工作。我想不出使用属性来实现此目的的更好方法。

public abstract class Collaborator : IPersonInformation, IRecruitmentInformation
{
    private string firstName;
    public String FirstName
    { 
        get { return firstName; }
        set 
        {

            //validation code

            this.firstName = value;
        }
    }
    public String LastName { get; set; }
    public DateTime RecruitmentDate { get; set; } 
}

Should work fine as a way of implementing your interfaces. I cannot think of a better way of achieving this using attributes.

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