我在DateTime
上有一个扩展方法:
public static DateTime? Year(this DateTime? datetime)
{
return datetime;
}
另一个试图为此扩展方法构建CallExpression
的人:
public static Expression<Func<TElementType, DateTime?>> SemanticYear<TElementType>(this Expression<Func<TElementType, DateTime?>> expr)
{
MethodInfo method = typeof(DateTime?).GetMethods().Where(m => m.Name.Equals("Year"));
//<<<<<< returns null!!
Expression<Func<TElementType, DateTime?>> lambdaExpression = Expression.Lambda<Func<TElementType, DateTime?>>(
Expression.Call(
Expression.MakeMemberAccess(expr.Body, typeof(DateTime?).GetProperty("Value")),
method
),
expr.Parameters
);
return lambdaExpression;
}
为什么我无法获得DateTime?.Year
扩展方法?
两种方法都在同一个编译单元中实现:
namespace Backend.Infrastructure.Linq
{
public static partial class LinqTreeExtensions
{
public static DateTime? Year(this DateTime datetime)
{
//....
}
public static Expression<Func<TElementType, DateTime?>> SemanticYear<TElementType>(this Expression<Func<TElementType, DateTime?>> expr)
{
//.......
}
}
}