I don’t claim that this page contains anything novel or anything you can’t find better explained elsewhere. However, as I post code on other topics which contain off-topic extension methods, I’ll add the methods here for completeness sake.
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<TObject> (this Expression<Func<TObject, object>> propertyReference) { return GetPropertyNameReferencedByExpression(propertyReference); } public static string GetReferencedPropertyName<TObject, TProperty> (tExpression<Func<TObject, TProperty>> 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 => x.property'."); return memberExpression.Member.Name; } } |
Some Better ToString Methods for Nullable Types
The idea that I cannot call someDate.ToString(“mm/dd/yyyy”) or someDecimal.ToString(“C”) on 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.