更新
从 C# 6 开始,现在使用null 条件运算符将解决方案嵌入到语言中;?.
用于属性和?[n]
索引器。
空条件运算符允许您仅在接收者不为空时访问成员和元素,否则提供空结果:
int? length = customers?.Length; // null if customers is null
Customer first = customers?[0]; // null if customers is null
旧答案
我查看了那里的不同解决方案。他们中的一些人使用将多个扩展方法调用链接在一起,这是我不喜欢的,因为由于每个链添加的噪音量,它不是很可读。
我决定使用只涉及单个扩展方法调用的解决方案,因为它更具可读性。我没有测试过性能,但就我而言,可读性比性能更重要。
我创建了以下类,松散地基于此解决方案
public static class NullHandling
{
/// <summary>
/// Returns the value specified by the expression or Null or the default value of the expression's type if any of the items in the expression
/// return null. Use this method for handling long property chains where checking each intermdiate value for a null would be necessary.
/// </summary>
/// <typeparam name="TObject"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="instance"></param>
/// <param name="expression"></param>
/// <returns></returns>
public static TResult GetValueOrDefault<TObject, TResult>(this TObject instance, Expression<Func<TObject, TResult>> expression)
where TObject : class
{
var result = GetValue(instance, expression.Body);
return result == null ? default(TResult) : (TResult) result;
}
private static object GetValue(object value, Expression expression)
{
object result;
if (value == null) return null;
switch (expression.NodeType)
{
case ExpressionType.Parameter:
return value;
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression)expression;
result = GetValue(value, memberExpression.Expression);
return result == null ? null : GetValue(result, memberExpression.Member);
case ExpressionType.Call:
var methodCallExpression = (MethodCallExpression)expression;
if (!SupportsMethod(methodCallExpression))
throw new NotSupportedException(methodCallExpression.Method + " is not supported");
result = GetValue(value, methodCallExpression.Method.IsStatic
? methodCallExpression.Arguments[0]
: methodCallExpression.Object);
return result == null
? null
: GetValue(result, methodCallExpression.Method);
case ExpressionType.Convert:
var unaryExpression = (UnaryExpression) expression;
return Convert(GetValue(value, unaryExpression.Operand), unaryExpression.Type);
default:
throw new NotSupportedException("{0} not supported".FormatWith(expression.GetType()));
}
}
private static object Convert(object value, Type type)
{
return Expression.Lambda(Expression.Convert(Expression.Constant(value), type)).Compile().DynamicInvoke();
}
private static object GetValue(object instance, MemberInfo memberInfo)
{
switch (memberInfo.MemberType)
{
case MemberTypes.Field:
return ((FieldInfo)memberInfo).GetValue(instance);
case MemberTypes.Method:
return GetValue(instance, (MethodBase)memberInfo);
case MemberTypes.Property:
return GetValue(instance, (PropertyInfo)memberInfo);
default:
throw new NotSupportedException("{0} not supported".FormatWith(memberInfo.MemberType));
}
}
private static object GetValue(object instance, PropertyInfo propertyInfo)
{
return propertyInfo.GetGetMethod(true).Invoke(instance, null);
}
private static object GetValue(object instance, MethodBase method)
{
return method.IsStatic
? method.Invoke(null, new[] { instance })
: method.Invoke(instance, null);
}
private static bool SupportsMethod(MethodCallExpression methodCallExpression)
{
return (methodCallExpression.Method.IsStatic && methodCallExpression.Arguments.Count == 1) || (methodCallExpression.Arguments.Count == 0);
}
}
这使我可以编写以下内容:
var x = scholarshipApplication.GetValueOrDefault(sa => sa.Scholarship.CostScholarship.OfficialCurrentWorldRanking);
x
将包含的值scholarshipApplication.Scholarship.CostScholarship.OfficialCurrentWorldRanking
或null
如果链中的任何属性在此过程中返回 null。