Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Runtime.CompilerServices;
- using System.Text;
- namespace MSIExport
- {
- static class ExtensionMethod
- {
- public static string GetPropertySymbol<T, TResult>(Expression<Func<T, TResult>> expression)
- {
- return String.Join(".",
- GetMembersOnPath(expression)
- .Select(m => GetMemberName(m))
- .Except(new List<String> { null })
- .Reverse()
- .ToArray());
- }
- private static IEnumerable<Expression> GetMembersOnPath(Expression expression)
- {
- while (expression != null)
- {
- yield return expression;
- expression = getChild(expression);
- }
- }
- /* private static dynamic Cast(dynamic obj, Type castTo)
- {
- return Convert.ChangeType(obj, castTo);
- } // */ // requires .NET 4.0
- private static string GetMemberName(Expression expression)
- {
- switch (expression.NodeType)
- {
- case ExpressionType.MemberAccess:
- return GetMemberName((MemberExpression)expression);
- case ExpressionType.Call:
- return GetMemberName((MethodCallExpression)expression);
- case ExpressionType.Lambda: // starting point
- case ExpressionType.Parameter: // x => x
- case ExpressionType.Convert: // if transtypage
- case ExpressionType.Constant: // for static method
- return null;
- default:
- throw new NotImplementedException();
- }
- }
- private static String GetMemberName(MethodCallExpression expression)
- {
- String name = String.Empty;
- if (expression.Method.IsStatic && !expression.Method.IsDefined(typeof(ExtensionAttribute), true))
- name += expression.Method.DeclaringType.Name + "->";
- name += expression.Method.Name + "()";
- return name;
- }
- private static String GetMemberName(MemberExpression expression)
- {
- return expression.Member.Name;
- }
- public static Expression getChild(Expression expression)
- {
- switch (expression.NodeType)
- {
- case ExpressionType.MemberAccess:
- return ((MemberExpression)expression).Expression;
- case ExpressionType.Call:
- if (((MethodCallExpression)expression).Method.IsStatic)
- return ((MethodCallExpression)expression).Arguments
- .SkipWhile(x => x.NodeType == ExpressionType.Constant)
- // .First(x => x.NodeType == ExpressionType.Parameter || x.NodeType == ExpressionType.Call)
- .First()
- ;
- else
- return ((MethodCallExpression)expression).Object;
- case ExpressionType.Lambda:
- return ((LambdaExpression)expression).Body;
- case ExpressionType.Convert:
- return ((UnaryExpression)expression).Operand;
- case ExpressionType.Parameter:
- return null;
- case ExpressionType.Constant:
- return null;
- default:
- throw new NotImplementedException();
- }
- }
- public static Expression<Func<T, Object>> RemoveLast<T>(Expression<Func<T, Object>> expression) where T : class
- {
- if (expression.NodeType == ExpressionType.Lambda)
- {
- Expression body = ((LambdaExpression)expression).Body;
- body = getChild(body);
- return Expression.Lambda<Func<T, Object>>(body, expression.Parameters);
- }
- else throw new NotImplementedException();
- }
- public static void Add<T>(this IDictionary<String, String> dict, T obj, Expression<Func<T, Object>> getValue) where T : class
- {
- String key = GetPropertySymbol(getValue);
- Object value = null;
- try
- {
- value = getValue.Compile().Invoke(obj);
- }
- catch (NullReferenceException)
- {
- // we (probably) tried to call ToString() or other on a null Object
- Expression<Func<T, object>> getValue2 = RemoveLast(getValue);
- key = GetPropertySymbol(getValue2);
- value = getValue2.Compile().Invoke(obj);
- }
- dict.Add(key, value?.ToString());
- }
- public static IEnumerable<string> GraphemeClusters(this string s)
- {
- var enumerator = StringInfo.GetTextElementEnumerator(s);
- while (enumerator.MoveNext())
- {
- yield return (string)enumerator.Current;
- }
- }
- public static string ReverseGraphemeClusters(this string s)
- {
- return string.Join(string.Empty, s.GraphemeClusters().Reverse().ToArray());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement