2

I want to be able to write

 MyObject.IsNull(p => p.MyObjectProperty)

I think it is achievable with expression. I thied to implement it this way:

public static void IsNull<T>(this T root, Expression<Func<T, object>> expression)
{
    if (CheckIfNull<T>(expression))
    {
        throw new ArgumentNullException(GetName(expression));
    }
}

private static string GetName<T>(Expression<Func<T, object>> expression)
{
    return ((MemberExpression)expression.Body).Member.Name;
}

public static bool CheckIfNull<T>(Expression<Func<T, object>> expression)
{
    Expression<Func<T, object>> obj = o => expression.Compile().Invoke(o);
    return obj == null;
}

But it is seams to be not working. How can I fix that?

4
  • you are comparing obj with null - which it never is, since one line above you are assigning a lambda-function to it. Instread check obj(root) == null. Other than that, none of your functions ever return true or false and they should not compile. Please check the code you provided Commented Aug 31, 2015 at 15:14
  • Please tell us what the behavior of MyObject.IsNull(p => p.MyObjectProperty) should be. It looks like you want an exception to be thrown if MyObjectProperty it is null, but then there is also the bool return value which is not ever used. Commented Aug 31, 2015 at 15:45
  • Just fixed the bool with void. Yes I want exception to be thorown in case CheckIfNull<T>(expression) == true Commented Aug 31, 2015 at 16:22
  • alright, now that that makes sense: did you ever try my answer? Does that fix it? Commented Aug 31, 2015 at 16:24

1 Answer 1

1

You have a bug:

The comparison obj == null should be obj(root) == null - of course you have to pass root as an argument to CheckIfNull.

The former comparison will always evaluate to false, since you are effectively comparing o => expression.Compile().Invoke(o) with null - they are never equal. You rather want to compare the result of the call to Invoke with null.

All my suggestions combined:

public static bool CheckIfNull<T>(this T root, Expression<Func<T, object>> expression)
{
    return expression.Compile()(root) == null;
}

public static void IsNull<T>(this T root, Expression<Func<T, object>> expression)
{
    if (root.CheckIfNull<T>(expression))
    {
        throw new ArgumentNullException(GetName(expression));
    }
}

private static string GetName<T>(Expression<Func<T, object>> expression)
{
    return ((MemberExpression)expression.Body).Member.Name;
}

Further comments:

  • I'm not sure ArgumentNullException is the right exception for this situation. But without knowledge about your scenario, it is hard to suggest something better. Actually:
  • it seems weird to write an extension method that merely throws an exception if a member is null, especially for a method named IsNull, which is why
  • I would rename IsNull to ThrowIfNull and rename CheckIfNull to IsNull
Sign up to request clarification or add additional context in comments.

2 Comments

just clerified my the question also initially I modify my code in notepad so I got it a bit wrong.
you are still comparing obj == null which will always be false! Also, you are still not returning a bool in IsNull.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.