Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GetSortingInfo<User>(u => u.UserId);
- public static RouteValueDictionary GetInfo<T>(this HtmlHelper html,
- Expression<Func<T, object>> action) where T : class
- {
- var expression = GetMemberInfo(action);
- string name = expression.Member.Name;
- return GetInfo(html, name);
- }
- private static MemberExpression GetMemberInfo(Expression method)
- {
- LambdaExpression lambda = method as LambdaExpression;
- if (lambda == null)
- throw new ArgumentNullException("method");
- MemberExpression memberExpr = null;
- if (lambda.Body.NodeType == ExpressionType.Convert)
- {
- memberExpr =
- ((UnaryExpression)lambda.Body).Operand as MemberExpression;
- }
- else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
- {
- memberExpr = lambda.Body as MemberExpression;
- }
- if (memberExpr == null)
- throw new ArgumentException("method");
- return memberExpr;
- }
- public PropertyInfo GetPropertyInfo<TSource, TProperty>(
- TSource source,
- Expression<Func<TSource, TProperty>> propertyLambda)
- {
- Type type = typeof(TSource);
- MemberExpression member = propertyLambda.Body as MemberExpression;
- if (member == null)
- throw new ArgumentException(string.Format(
- "Expression '{0}' refers to a method, not a property.",
- propertyLambda.ToString()));
- PropertyInfo propInfo = member.Member as PropertyInfo;
- if (propInfo == null)
- throw new ArgumentException(string.Format(
- "Expression '{0}' refers to a field, not a property.",
- propertyLambda.ToString()));
- if (type != propInfo.ReflectedType &&
- !type.IsSubclassOf(propInfo.ReflectedType))
- throw new ArgumentException(string.Format(
- "Expresion '{0}' refers to a property that is not from type {1}.",
- propertyLambda.ToString(),
- type));
- return propInfo;
- }
- var propertyInfo = GetPropertyInfo(someUserObject, u => u.UserID);
- public static RouteValueDictionary GetInfo<T,P>(this HtmlHelper html, Expression<Func<T, P>> action) where T : class
- {
- var expression = (MemberExpression)action.Body;
- string name = expression.Member.Name;
- return GetInfo(html, name);
- }
- GetInfo((User u) => u.UserId);
- public static void PrintName(Expression<Func<object>> exp)
- {
- string name = "";
- string val = "";
- MemberExpression body = exp.Body as MemberExpression;
- if (body == null) {
- UnaryExpression ubody = (UnaryExpression)exp.Body;
- body = ubody.Operand as MemberExpression;
- name = body.Member.Name;
- } else {
- name = body.Member.Name;
- }
- }
- public static void SetModifiedProperty<T>(this System.Data.Objects.ObjectStateEntry state, Expression<Func<T>> action)
- {
- var body = (MemberExpression)action.Body;
- string propertyName = body.Member.Name;
- state.SetModifiedProperty(propertyName);
- }
Add Comment
Please, Sign In to add comment