c# - 获取 PropertyInfo 的值

标签 c# .net validation reflection

我正在创建一个小型验证框架,我有一个可分配给方法的自定义 Validation AttributeValidationCore 中的一个 IsValid 属性> 类。当 IsValid 在方法内部调用时,我的 ValidationCore 找到调用方方法并将其属性分配给方法。我的自定义 Validation 属性有一个名为 TypeToValidate 的属性名称。因此,当我找到验证属性时,我会在该类型的类范围内寻找任何类型。到现在为止我没有任何问题,但问题是当我想获取我必须验证的属性值时,我没有该类的任何实例来获取该属性值。我不知道该如何处理这种情况,请帮助我。

这是我的样本:

public class TestClass
{
    public static TestModel Model { get; set; }
    public static ModelValidator ModelState
    {
        get { return new ModelValidator(); }
    }

    [Validate(typeof(TestModel))]
    public static void DoSomething()
    {
        if (ModelState.IsValid)
        {
            // Do something else....
        }
    }
}

编辑:这是我的IsValid属性

    public virtual Boolean IsValid
    {
        get
        {
            // Get IsValid caller method
            var method = GetCallerMethod();

            // Get method attribute
            var Attrib = GetMethodAttribute(typeof(ValidateAttribute), method);

            // Get model to validate inside class scope
            var modelProperty = GetModelToValidateInClassScope(Attrib, method);

            if (modelProperty != null)
            {
                ValidateModel(modelProperty);
            }

            ....
        }
    }

这里是 ValidateModel 方法:

    protected virtual void ValidateModel(PropertyInfo modelProperty)
    {
        // Here I've model property
        // But I can't get its value
        var model = modelProperty.GetValue(null, null);
        var properties = model.GetType().GetProperties(
                        BindingFlags.FlattenHierarchy |
                        BindingFlags.Public |
                        BindingFlags.Instance |
                        BindingFlags.DeclaredOnly);


        foreach (var propertyInfo in properties)
        {
            // Add error to error list
            GetPropertyErrors(model, propertyInfo);
        }
    }

提前致谢。

最佳答案

您需要一个实例来获取其属性值。看起来您需要修改方法 GetModelToValidateInClassScope 以便它返回模型本身的实例以及 PropertyInfo。

如果更改此方法使其看起来像这样(注意新的 out 参数):

PropertyInfo GetModelToValidateInClassScope(Type attributeType, MethodInfo methodInfo, out object instance)
{
   // same implementation as before ...

   // assign the model to use, 
   // which is likely accessible from somewhere in the is method
   instance = theModelInstanceFromThisMethod;

   // .. or if the property is static, then uncomment the next line:
   // instance = null;

   // same return value as before ...
}

我只是猜测,因为您没有提供此方法的实现。如果 GetModelToValidateInClassScope 无权访问模型实例,则您必须从其他地方获取它。

获得模型实例后,可以像下面这样使用它。请注意,ValidateModel 已被修改,因此它接受模型实例作为第一个参数。

...   

    // Get model to validate inside class scope 
    object instance;  // <--- will hold the model instance
    var modelProperty = GetModelToValidateInClassScope(Attrib, method, out instance); 

    if (modelProperty != null) 
    { 
        ValidateModel(instance, modelProperty); // <--- make sure to pass the instance along!
    } 

...

protected virtual void ValidateModel(object instance, PropertyInfo modelProperty)         
{         
    // get value of instance property
    var model = modelProperty.GetValue(instance, null);    

    ...

}     

关于c# - 获取 PropertyInfo 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11482636/

相关文章:

c# - 无法重命名 Visual Studio 2010 项目

javascript - 由于空格反序列化 Json 问题

javascript - JQuery 验证 - 模式规则不起作用,并且还导致其他规则不起作用

java - Struts 1.2 intRange 验证失败

c# - 为什么在ToLookup之前多放一个ToArray会更快?

jquery - 按需绑定(bind) KendoUI 下拉列表

C# 在查询 MeasureString 时标签是否忽略空格?

validation - 确保 URI 有效

c# - DatagridView 选择最后一行

c# - WP7 XNA 显示 3D FBX 模型