验证c#中的属性

时间:2011-02-09 14:33:08

标签: c# validation inheritance interface automatic-properties

让我们建议我有一个接口并从中继承类,

internal interface IPersonInfo
{
    String FirstName { get; set; }
    String LastName { get; set; }
}
internal interface IRecruitmentInfo
{
    DateTime RecruitmentDate { get; set; }
}

public abstract class Collaborator : IPersonInfo, IRecruitmentInfo
{
    public DateTime RecruitmentDate
    {
        get;
        set;
    }
    public String FirstName
    {
        get;
        set;
    }
    public String LastName
    {
        get;
        set;
    }
    public abstract Decimal Salary
    {
        get;
    }
}

然后如何在协作者类中验证字符串?是否可以实现内部属性?

9 个答案:

答案 0 :(得分:5)

是的,但不使用自动属性。您需要使用支持字段手动实现属性:

private string firstName;

public String FirstName
{
    get
    {
        return firstName;
    }
    set
    {
        // validate the input
        if (string.IsNullOrEmpty(value))
        {
            // throw exception, or do whatever
        }
        firstName = value;
    }
}

答案 1 :(得分:4)

像这样......

private string _firstName;
public string FirstName
{
    get
    {
        return _firstName;
    }
    set
    {
        if (value != "Bob")
          throw new ArgumentException("Only Bobs are allowed here!");
        _firstName = value;
    }
}

基本上,你用于属性的是语法糖版本。在编译时,他们创建一个私有成员变量并连接属性以使用该变量,因为我在这里手动完成。这样做的确切原因是,如果你想添加逻辑,你可以将它转换成手动的,就像我在这里一样,而不会破坏界面的实现。

答案 2 :(得分:3)

如果你有点复杂,还应该提一下验证框架。它们可以使验证规则更易于管理,并且还会向UI显示错误,同时保持规则与模型相关联,因此您不必具有任何重复的样板验证代码。根据您的框架版本,一个选项是DataAnnotations

答案 3 :(得分:2)

据我所知,如果使用自动属性语法,则无法访问支持字段。根据文档(http://msdn.microsoft.com/en-us/library/bb384054.aspx):

  

在C#3.0及更高版本中,自动实现   属性使属性声明   没有额外逻辑时更简洁   属性访问者需要。   它们还使客户端代码能够创建   对象。声明属性时   如下例所示,   编译器创建一个私有的,匿名的   支持领域,只能是   通过物业获得   并设置访问者。

     

允许使用属性   自动实现的属性但是   显然不在支持领域   因为那些是无法访问的   你的源代码。如果你必须使用   属性在一个支持领域   财产,只是创建一个常规   属性。

因此,您唯一的解决方案是创建常规属性。

答案 4 :(得分:2)

答案 5 :(得分:0)

是。您可以为属性创建一个私有支持字段,如下所示:

private String _firstName;

public String FirstName
{
     get
     {
          return _firstName;
     }
     set
     {
          //Check value for correctness here:
          _firstName = value;
     }
}

答案 6 :(得分:0)

private DateTime recruitmentDate;    
public DateTime RecruitmentDate
{
    get { return recruitmentDate; }
    set
    {
        validate(value);
        recruitmentDate = value;
    }
}

答案 7 :(得分:0)

如果你的意思是你可以在C#中获取/设置属性期间执行自定义逻辑,答案是肯定的。

您使用所谓的自动属性,其中后备存储和逻辑默认为您。

你只需要自己提供

private int backingStoreVariable;
public property MyProperty
{
    get
    {
        return this.backingStoreVariable;
    }
    set
    {
        this.backingStoreVariable=value;
    }
}

现在,您可以在get和set块中运行自定义验证代码。

答案 8 :(得分:0)

或者,您不能为字段使用值类型。例如,您可以使用以下实现创建“FirstName”类:

public class FirstName
{
    private string _Value;
    public string Value
    {
        get
        {
            return _Value;
        }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentNullException("Value cannot be null");
            if (value.Length > 128)
                throw new ArgumentOutOfRangeException("Value cannot be longer than 128 characters");
            _Value  = value;
        }
    }

    public FirstName(string initialValue)
    {
        Value   = initialValue; //does validation check even in constructor
    }
}

最后,在上面的代码示例中,您只需:

public interface IPersonInfo
{
    FirstName FirstName { get; set; }
    String LastName { get; set; }
}

以及其他属性等等。 然后,要在您的codel中使用该属性,您将拥有:

public FirstName MyFirstName;
var x = MyFirstName.Value;

如果您想要验证很多字段,这可能最终会成为一种繁琐的方法。但是,您可以将其概括为处理某些类型的数字 - 例如正数(ints > 0)或计数(int >= 0),度量等。

字符串更难,因为它们除了值类型之外通常还有长度限制(例如没有特殊字符,没有数字等。这可能通过在构造函数中设置的只读长度属性来适应。继承类。

相关问题