我正在创建一个小的验证框架,我有一个可分配给方法的自定义Validation Attribute和ValidationCore类中的一个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);
}
}提前谢谢。
发布于 2012-07-14 21:08:41
的属性值。看起来,您需要修改方法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);
...
} 发布于 2012-07-14 09:50:42
对实际问的问题的回答
到目前为止,我没有任何问题,但是问题是,当我想要得到要验证的属性的值时,我没有任何类的实例来获得那个属性值。
如果它是一个静态属性,那很好--只需使用null作为第一个参数:
// 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并检索该属性。
基本上,我们还不清楚你想要做的东西到底是从哪里来的。属性位于方法上,而不是属性上,属性没有说明要验证哪些属性.
https://stackoverflow.com/questions/11482636
复制相似问题