我试图制定一种通用的方法,这种方法将回头,以在XML文件中找到内容。
基本如此:
private static Func<XElement, bool> GetPredicate<T>(Criterion criterion)
{
switch (criterion.CriteriaOperator)
{
case CriteriaOperator.Equal:
return x => (T)x.Attribute(criterion.PropertyName) ==
(T)(criterion.PropertyValue);
case CriteriaOperator.GreaterThan:
return x => (T)x.Attribute(criterion.PropertyName) >
(T)(criterion.PropertyValue);
case CriteriaOperator.GreaterThanOrEqual:
return x => (T)x.Attribute(criterion.PropertyName) >=
(T)(criterion.PropertyValue);
case CriteriaOperator.LessThan:
return x => (T)x.Attribute(criterion.PropertyName) <
(T)(criterion.PropertyValue);
case CriteriaOperator.LessThanOrEqual:
return x => (T)x.Attribute(criterion.PropertyName) <=
(T)(criterion.PropertyValue);
case CriteriaOperator.NotEqual:
return x => (T)x.Attribute(criterion.PropertyName) !=
(T)(criterion.PropertyValue);
default:
throw new ArgumentException("Criteria Operator not supported.");
}
}
只有这样才汇编。 问题载于<代码>x。 沥青。 PropertyName) part where the codificationer indicated:
Cannot cast expression of type System.Xml.Linq.XAttribute to type T
目前,我有两种方法相同,只有两种方法可以翻一番,另一种方法可以ci。 我真的不想重复。