mercredi 10 août 2016

Extension method not listed on GetMethods() [duplicate]

This question already has an answer here:

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)
        {
        //.......
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire