Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 验证属性代码_C#_Validation_Mvvm_Mvvmcross - Fatal编程技术网

C# 验证属性代码

C# 验证属性代码,c#,validation,mvvm,mvvmcross,C#,Validation,Mvvm,Mvvmcross,大家好,我使用的是mvvmcross和portable类库,所以我不能使用prism或componentmodel数据注释来验证我的类。基本上我有一个模型库,我的所有模型都是从中继承的 我下面的验证代码坏得很厉害,基本上我在寻找数据注释用来迭代继承基类的类上所有属性的代码 我已经编写了各种属性,这些属性都有自己的验证器从validatorBase继承,后者从属性继承。我一辈子都搞不懂密码上面写着。。。好的,我是一个类,我将遍历我的所有属性,这些属性的类型为ValidatorBase,并运行验证器

大家好,我使用的是mvvmcross和portable类库,所以我不能使用prism或componentmodel数据注释来验证我的类。基本上我有一个模型库,我的所有模型都是从中继承的

我下面的验证代码坏得很厉害,基本上我在寻找数据注释用来迭代继承基类的类上所有属性的代码

我已经编写了各种属性,这些属性都有自己的验证器从validatorBase继承,后者从属性继承。我一辈子都搞不懂密码上面写着。。。好的,我是一个类,我将遍历我的所有属性,这些属性的类型为ValidatorBase,并运行验证器。我的代码在底部

public class ModelBase
{

    private Dictionary<string, IEnumerable<string>> _errors;
    public Dictionary<string, IEnumerable<string>> Errors
    {
        get
        {
            return _errors;
        }

    }

    protected  virtual bool Validate()
    {
        var propertiesWithChangedErrors = new List<string>();

        // Get all the properties decorated with the ValidationAttribute attribute.
        var propertiesToValidate = this.GetType().GetRuntimeProperties()
                                                    .Where(c => c.GetCustomAttributes(typeof(ValidatorBase)).Any());

        foreach (PropertyInfo propertyInfo in propertiesToValidate)
        {
            var propertyErrors = new List<string>();
            TryValidateProperty(propertyInfo, propertyErrors);

            // If the errors have changed, save the property name to notify the update at the end of this method.
            bool errorsChanged = SetPropertyErrors(propertyInfo.Name, propertyErrors);
            if (errorsChanged && !propertiesWithChangedErrors.Contains(propertyInfo.Name))
            {
                propertiesWithChangedErrors.Add(propertyInfo.Name);
            }
        }

        // Notify each property whose set of errors has changed since the last validation.  
        foreach (string propertyName in propertiesWithChangedErrors)
        {
            OnErrorsChanged(propertyName);
            OnPropertyChanged(string.Format(CultureInfo.CurrentCulture, "Item[{0}]", propertyName));
        }

        return _errors.Values.Count == 0;

    }
}
下面是它的用法示例

  [Required(ErrorMessage = "Please enter the Amount")]
    public decimal Amount
    {
        get { return _amount; }
        set { _amount = value;  }//SetProperty(ref _amount, value); }
    }

为什么不使用IDataErrorInfo?我们不想这样做的主要原因是,我们目前正在Xamarin中进行POC,并且希望在一个非常大的应用程序上重用我们现有的验证。如果我们改为使用errordatainfo,那么我们将不得不对所有应用程序重新进行验证,我们也非常确信microsoft不久将重新包含使用数据批注。。并将影响我们POC的成功
  [Required(ErrorMessage = "Please enter the Amount")]
    public decimal Amount
    {
        get { return _amount; }
        set { _amount = value;  }//SetProperty(ref _amount, value); }
    }