有没有简单的方法将HTML转换为纯文本?
Expression<Func<TBase,bool>>
为了
Expression<Func<T,bool>>
T是从TBase继承而来的吗?
Expression<Func<TBase,bool>>
为了
Expression<Func<T,bool>>
T是从TBase继承而来的吗?
Expression<Func<object, bool>> x = o => o != null;
Expression<Func<string, bool>> y = Expression.Lambda<Func<string, bool>>(x.Body, x.Parameters);
T
都是 TBase
,但并非所有的 TBase
都是 T
。TBase
(当然要捕获任何类型安全问题)。public class ConverterExpressionVisitor<TDest> : ExpressionVisitor
{
protected override Expression VisitLambda<T>(Expression<T> node)
{
var readOnlyCollection = node.Parameters.Select(a => Expression.Parameter(typeof(TDest), a.Name));
return Expression.Lambda(node.Body, node.Name, readOnlyCollection);
}
protected override Expression VisitParameter(ParameterExpression node)
{
return Expression.Parameter(typeof(TDest), node.Name);
}
}
public class A { public string S { get; set; } }
public class B : A { }
static void Main(string[] args)
{
Expression<Func<A, bool>> ExpForA = a => a.S.StartsWith("Foo");
Console.WriteLine(ExpForA); // a => a.S.StartsWith("Foo");
var converter = new ConverterExpressionVisitor<B>();
Expression<Func<B, bool>> ExpForB = (Expression<Func<B, bool>>)converter.Visit(ExpForA);
Console.WriteLine(ExpForB); // a => a.S.StartsWith("Foo"); - same as for A but for B
Console.ReadLine();
}