public static List <object> SelectFx <TSource>(this IEnumerable <TSource> source, string selectorExpression) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(selectorExpression)) { throw new ArgumentException("Argument can't be null nor empty.", "selectorExpression"); } selectorExpression = CodeUtils.PreprocessCode(selectorExpression); Session session = RoslynUtils.CreateSession(); Exception exception; Func <TSource, object> selector = TryExecute <Func <TSource, object> >(selectorExpression, session, out exception); if (selector != null) { return(source.Select(selector).ToList()); } throw new ArgumentException(string.Format("Couldn't parse selector expression ('{0}') as Func<T, object>. TSource: '{1}'.", selectorExpression, typeof(TSource)), "selectorExpression", exception); }
private static IEnumerable <TSource> DoOrderByFx <TSource>(IEnumerable <TSource> source, string keySelectorExpression, Type keyType) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(keySelectorExpression)) { throw new ArgumentException("Argument can't be null nor empty.", "keySelectorExpression"); } if (keyType == null) { throw new ArgumentNullException("keyType"); } keySelectorExpression = CodeUtils.PreprocessCode(keySelectorExpression); Session session = RoslynUtils.CreateSession(); Type keySelectorType = ReflectionUtils.CreateFuncType2(typeof(TSource), keyType); Exception exception; object keySelector = TryExecute(keySelectorExpression, session, keySelectorType, out exception); if (keySelector != null && keySelectorType.IsInstanceOfType(keySelector)) { return(InvokeOrderBy(keyType, source, keySelector)); } throw new ArgumentException(string.Format("Couldn't parse key selector expression ('{0}') as Func<TSource, TKey>. TSource: '{1}'. TKey: '{2}'.", keySelectorExpression, typeof(TSource), keyType), "keySelectorExpression"); }
public static object Run(string code) { if (code == null) { throw new ArgumentNullException("code"); } code = CodeUtils.PreprocessCode(code); Session session = RoslynUtils.CreateSession(); return(session.Execute(code)); }
public static List <TSource> WhereFx <TSource>(this IEnumerable <TSource> source, string predicateExpression) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(predicateExpression)) { throw new ArgumentException("Argument can't be null nor empty.", "predicateExpression"); } predicateExpression = CodeUtils.PreprocessCode(predicateExpression); Session session = RoslynUtils.CreateSession(); Exception exception; Func <TSource, bool> predicate = TryExecute <Func <TSource, bool> >(predicateExpression, session, out exception); if (predicate != null) { return(source.Where(predicate).ToList()); } Func <TSource, int, bool> predicateWithIndex = TryExecute <Func <TSource, int, bool> >(predicateExpression, session, out exception); if (predicateWithIndex != null) { return(source.Where(predicateWithIndex).ToList()); } throw new ArgumentException(string.Format("Couldn't parse predicate expression ('{0}') as neither Func<T, bool> nor Func<T, int, bool>. TSource: '{1}'.", predicateExpression, typeof(TSource)), "predicateExpression", exception); }