1

I've an extension method on DateTime:

public static DateTime? Year(this DateTime? datetime)
{
    return datetime;
}

And antoher one which is trying to build a CallExpression to this extension method:

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;
}

Why do I unable to get DateTime?.Year extension method?

Both methods are implemented inside the same compilation unit:

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)
        {
        //.......
        }
    }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Jordi
  • 20,868
  • 39
  • 149
  • 333

0 Answers0