Skip to content

Instantly share code, notes, and snippets.

@krcourville
Last active December 24, 2015 13:59
Show Gist options
  • Select an option

  • Save krcourville/6809075 to your computer and use it in GitHub Desktop.

Select an option

Save krcourville/6809075 to your computer and use it in GitHub Desktop.
Functional Fluent Code Sample : To increase code readability and code refactorability (new word), I find myself using fluent functions more and more. As a result, here is a code sample that might be used to toggle visibility of properties for a child view model.
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System;
public interface ISelectiveDisplay {
bool IsVisible { get; set; }
ICollection<string> PropertiesToHide { get; }
}
public abstract class SelectiveDisplay<T> : ISelectiveDisplay {
public SelectiveDisplay() {
this.PropertiesToHide = new List<string>();
}
[ScaffoldColumn(false)]
public bool IsVisible { get; set; }
[ScaffoldColumn(false)]
public ICollection<string> PropertiesToHide {
get;
private set;
}
private static string GetPropertyName(Expression<Func<T, object>> property) {
PropertyInfo propertyInfo = null;
if (property.Body is MemberExpression) {
propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
} else {
propertyInfo =
(((UnaryExpression)property.Body).Operand as MemberExpression)
.Member as PropertyInfo;
}
return propertyInfo.Name;
}
public void HideProperty(Expression<Func<T, object>> prop) {
this.PropertiesToHide.Add(GetPropertyName(prop));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment