考虑以下简单的扩展方法:
public static class MyExtensions
{
public static int DemoLength(this string str, string toAdd)
{
return (str + toAdd).Length;
}
}
我不得不称它为:
随着扩展
int result1 = "hello".DemoLength("all");
从静态类
int result2 = MyExtensions.DemoLength("hello", "all");
现在考虑另一个例子:
IQueryable<int> source = Enumerable.Range(0, 10).AsQueryable();
Expression<Func<int, bool>> expr = t => (t & 1) == 0; // Even numbers
LambdaExpression lambda = expr;
随着扩展
IQueryable<int> result1 = source.Where((dynamic)lambda);
从静态类
IQueryable<int> result2 = Queryable.Where(source, (dynamic)lambda);
为什么这次扩展方法不能编译?