Created
March 20, 2009 15:11
-
-
Save agross/82401 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Quick hack! | |
internal static class DPRegistration | |
{ | |
public static DependencyProperty Register<T>(Expression<Func<T, object>> member) | |
{ | |
PropertyInfo property = GetProperty(member); | |
return DependencyProperty.Register(property.Name, | |
property.PropertyType, | |
property.DeclaringType); | |
} | |
public static PropertyInfo GetProperty(LambdaExpression expression) | |
{ | |
MemberExpression memberExpression = null; | |
if (expression.Body.NodeType == ExpressionType.Convert) | |
{ | |
UnaryExpression body = (UnaryExpression)expression.Body; | |
memberExpression = body.Operand as MemberExpression; | |
} | |
else if (expression.Body.NodeType == ExpressionType.MemberAccess) | |
{ | |
memberExpression = expression.Body as MemberExpression; | |
} | |
if (memberExpression == null) | |
{ | |
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, | |
"Unsupported expression type: {0}", | |
expression.Body.GetType()), | |
"expression"); | |
} | |
return (PropertyInfo) memberExpression.Member; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment