December 2009


.Net Development27 Dec 2009 05:17 pm

When Microsoft added support for nullable types, why didn’t they add reasonable ToString() implementations as well? The idea that I cannot call someDate.ToString(“mm/dd/yyyy”) or someDecimal.ToString(“C”) and the like on Nullable<DateTime> and Nullable<decimal> respectively drives my bonkers. So I have written the following extension methods to restore my sanity.

using System;
 
public static class NullableExtensionMethods
{
    ///
    /// If null return "NULL" otherwise returns value.ToString();
    ///
    public static string ToStringOrNullText(this T? value) where T : struct
    {
        return value.HasValue ? value.Value.ToString() : "NULL";
    }
 
    ///
    /// If null return an empty string otherwise returns the value formated according to the format string;
    ///
    public static string ToString(this DateTime? value, string format)
    {
        return value.HasValue ? value.Value.ToString(format) : string.Empty;
    }
 
    ///
    /// If null return an empty string otherwise returns the value formated according to the format string;
    ///
    public static string ToString(this decimal? value, string format)
    {
        return value.HasValue ? value.Value.ToString(format) : string.Empty;
    }
}

Obviously, this approach can be extended to other types as well.

.Net Development20 Dec 2009 03:57 pm

This page does not contain anything you can’t find better explained elsewhere.  However, these methods show up in a lot of my code so I figure I had better post them.

Methods to get the Property Name referenced in a Lambda Expression

The below methods act on lambda expressions of the form “x => x.SomeProperty” and return the name of the referenced property (“SomeProperty” in this case).

The first method is for the typical case where one doesn’t know at compile time what the return type of the property is.  Therefore it is implemented using a return type of object.  The second method is for the occasional situation where the return type is known and one wants to use a strongly typed lambda.

For a fuller explanation of these methods see this excellent blog post from which I shamelessly lifted it.

For an example of how to use these methods, see this post.

using System;
using System.Linq.Expressions;
using System.Reflection;
 
static class ExpressionExtensionMethods
{
    public static string GetReferencedPropertyName
        (this Expression propertyReference)
    {
        return GetPropertyNameReferencedByExpression(propertyReference);
    }
 
    public static string GetReferencedPropertyName
        (this Expression propertyReference)
    {
        return GetPropertyNameReferencedByExpression(propertyReference);
    }
 
    private static string GetPropertyNameReferencedByExpression
        (LambdaExpression propertyReference)
    {
        MemberExpression memberExpression;
        var unary = propertyReference.Body as UnaryExpression;
        if (unary != null)
            //In this case the return type of the property was not object,
            //so .Net wrapped the expression inside of a unary Convert()
            //expression that casts it to type object. In this case, the
            //Operand of the Convert expression has the original expression.
            memberExpression = unary.Operand as MemberExpression;
        else
            //when the property is of type object the body itself is the
            //correct expression
            memberExpression = propertyReference.Body as MemberExpression;
 
        if (memberExpression == null
            || !(memberExpression.Member is PropertyInfo))
            throw new ArgumentException(
                "Expression was not of the form 'x =&gt; x.property'.");
 
        return memberExpression.Member.Name;
    }
}