Пример #1
0
        public static Func <dynamic, dynamic> CompileMapExpression(string expression, string method, IEnumerable <KeyValuePair <string, string> > supportLambdas, Func <dynamic, string, dynamic> logger = null, string[] referencesIn = null)
        {
            try {
                if (IsLambda(expression))
                {
                    return(CompileLambda <dynamic, dynamic>(expression, supportLambdas, logger, referencesIn));
                }
                else if (IsTemplate(expression))
                {
                    return(CompileTemplate(expression));
                }
                else if (expression.EndsWith(".cs"))
                {
                    var asm = CSharpUtility.CompileAssembly(new string[] { File.ReadAllText($"{System.AppDomain.CurrentDomain.BaseDirectory}{expression}") },
                                                            referencesIn?.Select(r => PortableExecutableReference.CreateFromFile(r)).ToArray()
                                                            );

                    var path = Regex.Split(expression, "[.]");
                    return(RetrieveDelegate <dynamic, dynamic>(asm, path.First(), path.ElementAt(1), method));
                }
                else if (expression.EndsWith(".dll"))
                {
                    var path = Regex.Split(expression, "[.]");
                    var asm  = Assembly.LoadFrom(expression);
                    return(RetrieveDelegate <dynamic, dynamic>(asm, path.First(), path.ElementAt(1), method));
                }
                else
                {
                    return((i) => expression);
                }
            }
            catch (Exception e) {
                throw new Exception($"Unable to compile expression: '{expression}' {Environment.NewLine} {e.Message}", e);
            }

            throw new Exception($"Invalid expression provided: '{expression}'");
        }
Пример #2
0
        /// <summary>
        /// Complies a string template in the format accepted c# 4.5
        ///
        /// Example:
        /// "Full Name: {first} {last}"
        ///
        /// </summary>
        /// <param name="templateIn"></param>
        /// <returns></returns>
        public static Func <dynamic, dynamic> CompileTemplate(string templateIn)
        {
            var source = $"i => $\"{templateIn.Replace("{", "{i.")}\"";

            return(CSharpUtility.CompileLambda <dynamic, dynamic>(source));
        }