jeudi 18 juin 2020

Get DateTime value From FieldInfo

I am tryning to retrieve a DateTime via reflection. The value is not inside a class or a static class, but it is from a lambda expression...

I have this really simple lambda expression: ATest => ATest.MyDate == DateTime.Now

Now, via an ExpressionVisitor I am trying getting datetime value:

public class ExpressionComparionsVisitor : ExpressionVisitor
{
    public Expression Modify(Expression expression)
    {
        return base.Visit(expression);
    }


    protected override Expression VisitMember(MemberExpression node)
    {
        if (node.NodeType == ExpressionType.MemberAccess)
        {
            if (node.Member.MemberType == System.Reflection.MemberTypes.Field)
            {
                if (node.Type ==  typeof(DateTime))
                {
                    System.Reflection.FieldInfo fieldInfo = node.Member as System.Reflection.FieldInfo;

                    if (fieldInfo != null)
                    {
                        //Here what I have tried, but no success

                        //var n = (DateTime)fieldInfo.GetValue(fieldInfo);
                        //var n = (DateTime)fieldInfo.GetValue(typeof(DateTime));
                        //var n = (DateTime)fieldInfo.GetValue(node.Member);
                        //var n = (DateTime)fieldInfo.GetValue(null);
                    }
                }
            }
        }
            return base.VisitMember(node);
    }
}

How can I get the DateTime value of that field? Thank you





Aucun commentaire:

Enregistrer un commentaire