首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >求出PropertyInfo值

求出PropertyInfo值
EN

Stack Overflow用户
提问于 2012-07-14 09:36:26
回答 2查看 7K关注 0票数 3

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

这是我的样本:

代码语言:javascript
复制
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属性

代码语言:javascript
复制
    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方法:

代码语言:javascript
复制
    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);
        }
    }

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-14 21:08:41

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

如果此方法被更改,则如下所示(注意新的out参数):

代码语言:javascript
复制
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已被修改,因此它接受模型实例作为第一个参数。

代码语言:javascript
复制
...   

    // 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);    

    ...

}     
票数 1
EN

Stack Overflow用户

发布于 2012-07-14 09:50:42

对实际问的问题的回答

到目前为止,我没有任何问题,但是问题是,当我想要得到要验证的属性的值时,我没有任何类的实例来获得那个属性值。

如果它是一个静态属性,那很好--只需使用null作为第一个参数:

代码语言:javascript
复制
// First argument is the instance: null as it's a static property
// Second argument is indexer arguments: null as we don't have any
var value = property.GetValue(null, null);

来自文献资料

因为静态属性属于类型,而不是单个对象,所以通过传递null作为对象参数来获得静态属性。

替代方法

如果您只是试图从TypeToValidate属性中获取Validate属性,那么您应该有该属性的一个实例,您可以直接转换为ValidateAttribute并检索该属性。

基本上,我们还不清楚你想要做的东西到底是从哪里来的。属性位于方法上,而不是属性上,属性没有说明要验证哪些属性.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11482636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档