当通用不可用时尝试获取通用
我正在尝试开发一些通用的自定义 ValidationAttributes。 无法创建属性的通用子类这一事实让我抓狂。
以下是我在 ValidationAttribute 中重写 IsValid 的代码,用于验证属性的值是否唯一:
public override bool IsValid(object value)
{
SomeDataContext context = SomeDataContext.GetNewDataContext();
Type tableType = typeof(Table<>).MakeGenericType(new Type[] { _EntityType });
var table = Activator.CreateInstance(tableType);
//ITable table = context.GetTable(_EntityType);
var codeProp = (from member in context.Mapping.GetMetaType(_EntityType).DataMembers
where member.Name == _PropertyName
select member.Member).Single();
ParameterExpression param = Expression.Parameter(_EntityType, "x");
MemberExpression memberExp = Expression.Property(param, (PropertyInfo)codeProp);
Expression body = Expression.Equal(memberExp, Expression.Constant(value, typeof(char)));
//var predicate = Expression.Lambda<Func<TEntityType, bool>>(body, param);
Type lambdaType = typeof(Func<,>).MakeGenericType(_EntityType, typeof(bool));
var predicate = Expression.Lambda(lambdaType, body, param);
object code = table.FirstOrDefault(predicate);
if (code != null)
{
return false;
}
return true;
}
此行:
object code = table.FirstOrDefault(predicate);
错误:
“object”不包含“FirstOrDefault”的定义,并且没有扩展名......等。
如何定义、转换或以其他方式让编译器将“表”识别为公开 .FirstOrDefault 方法的内容?
谢谢
I am trying to develop some general purpose custom ValidationAttributes. The fact that one cannot create a generic subclass of an attribute is making me nuts.
Here is my code for the IsValid override in a ValidationAttribute to verify that the value of a property is unique:
public override bool IsValid(object value)
{
SomeDataContext context = SomeDataContext.GetNewDataContext();
Type tableType = typeof(Table<>).MakeGenericType(new Type[] { _EntityType });
var table = Activator.CreateInstance(tableType);
//ITable table = context.GetTable(_EntityType);
var codeProp = (from member in context.Mapping.GetMetaType(_EntityType).DataMembers
where member.Name == _PropertyName
select member.Member).Single();
ParameterExpression param = Expression.Parameter(_EntityType, "x");
MemberExpression memberExp = Expression.Property(param, (PropertyInfo)codeProp);
Expression body = Expression.Equal(memberExp, Expression.Constant(value, typeof(char)));
//var predicate = Expression.Lambda<Func<TEntityType, bool>>(body, param);
Type lambdaType = typeof(Func<,>).MakeGenericType(_EntityType, typeof(bool));
var predicate = Expression.Lambda(lambdaType, body, param);
object code = table.FirstOrDefault(predicate);
if (code != null)
{
return false;
}
return true;
}
This line:
object code = table.FirstOrDefault(predicate);
errors out:
'object' does not contain a definition for 'FirstOrDefault' and no extension ......etc.
How do I define, cast or otherwise get the compiler to recognize "table" as something that exposes a .FirstOrDefault method?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将使您获得 System.Linq.Enumerable 可以使用的类型:
三个问题:
This will get you to a type that System.Linq.Enumerable can use:
Three concerns:
通过使用显式转换为某些内容的
IEnumerable
。 由于您不知道您的情况中的“某些内容”,因此您实际上无法使用FirstOrDefault
。 自己简单实现一下逻辑:By using an explicit cast to
IEnumerable<>
of something. Since you don't know the “something” in your case, you effectively cannot useFirstOrDefault
. Simply implement the logic yourself: