Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. public static IEnumerable<object> PerformLinqOn(IEnumerable<object> source, ref Type type, string query)
  2. {
  3.     foreach (Match item in ExpressionTreeQueryRegex)
  4.     {
  5.         ParameterExpression parameterExpression = null;
  6.         LambdaExpression expression = null;
  7.         Delegate expressionDelegate = null;
  8.         if (method != "Take" && !string.IsNullOrWhiteSpace(lambda))
  9.         {
  10.             parameterExpression = Expression.Parameter(type, identifier);
  11.             expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { parameterExpression }, null, lambda);
  12.             expressionDelegate = expression.Compile();
  13.         }
  14.  
  15.         switch (method)
  16.         {
  17.             case [...]
  18.             case "Select":
  19.                 values = values?.Select(x => expressionDelegate?.DynamicInvoke(x)) ??
  20.                     source.Select(x => expressionDelegate?.DynamicInvoke(x));
  21.                 type = GetSelectTypeFromObject(values.FirstOrDefault());
  22.                 break;
  23.             case [...]
  24.             default:
  25.                 throw new NotSupportedException(method + " is not supported.");
  26.         }
  27.     }
  28.     return values;
  29. }
  30.  
  31.  
  32. private static Type GetSelectTypeFromObject(object value)
  33. {
  34.     if (value == null)
  35.     {
  36.         throw new ArgumentNullException();
  37.     }
  38.     // Default base types
  39.     Assembly mscorlib = typeof(string).Assembly;
  40.     foreach (Type type in mscorlib.GetTypes())
  41.     {
  42.         if (value.GetType() == type)
  43.         {
  44.             return type;
  45.         }
  46.     }
  47.  
  48.     // All runtime generated Types
  49.     Assembly types = ModelProvider.TypeLibrary.Assembly;
  50.     foreach (Type type in types.GetTypes())
  51.     {
  52.         if (value.GetType() == type)
  53.         {
  54.             return type;
  55.         }
  56.     }
  57.     throw new TargetInvocationException(new Exception("Could not find a type matching the object."));
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement