예제 #1
0
    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);
    }
예제 #2
0
    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");
    }
예제 #3
0
    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));
    }
예제 #4
0
    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);
    }