Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static IEnumerable<object> PerformLinqOn(IEnumerable<object> source, ref Type type, string query)
- {
- foreach (Match item in ExpressionTreeQueryRegex)
- {
- ParameterExpression parameterExpression = null;
- LambdaExpression expression = null;
- Delegate expressionDelegate = null;
- if (method != "Take" && !string.IsNullOrWhiteSpace(lambda))
- {
- parameterExpression = Expression.Parameter(type, identifier);
- expression = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { parameterExpression }, null, lambda);
- expressionDelegate = expression.Compile();
- }
- switch (method)
- {
- case [...]
- case "Select":
- values = values?.Select(x => expressionDelegate?.DynamicInvoke(x)) ??
- source.Select(x => expressionDelegate?.DynamicInvoke(x));
- type = GetSelectTypeFromObject(values.FirstOrDefault());
- break;
- case [...]
- default:
- throw new NotSupportedException(method + " is not supported.");
- }
- }
- return values;
- }
- private static Type GetSelectTypeFromObject(object value)
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
- // Default base types
- Assembly mscorlib = typeof(string).Assembly;
- foreach (Type type in mscorlib.GetTypes())
- {
- if (value.GetType() == type)
- {
- return type;
- }
- }
- // All runtime generated Types
- Assembly types = ModelProvider.TypeLibrary.Assembly;
- foreach (Type type in types.GetTypes())
- {
- if (value.GetType() == type)
- {
- return type;
- }
- }
- throw new TargetInvocationException(new Exception("Could not find a type matching the object."));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement