Пример #1
0
        public static TResult Evals <TResult>(string expressionStr, string name, Expression expression, object data = null)
        {
            if (string.IsNullOrEmpty(expressionStr))
            {
                throw new ArgumentNullException(nameof(expressionStr));
            }

            var infos  = Analyse(expressionStr);
            var format = expressionStr;

            if (infos.Any(i => i.Mode != ParamMode.Param))
            {
                var parser = UtilContainer.Resolve <ParamParser.Interface.IParamRegular>();
                format = parser.Regular(format, infos, expression, GLOBAL_STATIC_TYPES);
            }

            IPrecompilter precompilter = UtilContainer.Resolve <IPrecompilter>();

            if (precompilter.KeyWords.Any())
            {
                format = precompilter.Regular(format);
            }

            var excuterCreator = UtilContainer.Resolve <IExcuterCreator>();
            var context        = excuterCreator.BuildExcuter();

            RegisterBasicType(context, expression);
            return(context.Execute <TResult>(name, format, data, ParamConvert.GetParamInfos(data)));
        }
Пример #2
0
        public IEnumerable <ExpressionInfo> DoAnalyse(string expression)
        {
            IKeyWordProvider keyWordProvider = UtilContainer.Resolve <IKeyWordProvider>();
            var param = keyWordProvider.Param;

            if (expression.IndexOf(param) < 0)
            {
                return(Enumerable.Empty <ExpressionInfo>());
            }

            IExpressionInfoBuilder builder = UtilContainer.Resolve <IExpressionInfoBuilder>();

            ICollection <ExpressionInfo> expressionInfos = new List <ExpressionInfo>();
            var paramStrArray = expression.Split(keyWordProvider.SplitChar).Where(p => p.StartsWith(keyWordProvider.Param)).Select(p => p.Replace(keyWordProvider.Param, string.Empty)).ToArray();
            int index         = 0;

            foreach (var paramStr in paramStrArray)
            {
                var ei = builder.Build(expression, paramStr, keyWordProvider);
                ei.StartIndex = expression.IndexOf(paramStr, index);
                index         = paramStr.Length + ei.StartIndex;
                expressionInfos.Add(ei);
            }

            return(expressionInfos);
        }
Пример #3
0
        public static IEnumerable <ExpressionInfo> Analyse(string expression)
        {
            var analyse = UtilContainer.Resolve <IAnalyse>();
            var infos   = analyse.DoAnalyse(expression);

            return(infos);
        }
Пример #4
0
        public Type FetchType(string name)
        {
            var asses = Assemblies ?? UtilContainer.Resolve <IAssemblyLoader>().GetAssemblies(name);
            var ass   = asses.FirstOrDefault(a => name.StartsWith(a.FullName.Split(',')[0], StringComparison.OrdinalIgnoreCase));

            if (ass != null)
            {
                var type = ass.GetType(name);
                if (type != null)
                {
                    return(type);
                }
            }

            foreach (var item in asses)
            {
                if (!item.DefinedTypes.Any(dt => dt.FullName == name))
                {
                    continue;
                }

                var type = item.GetType(name);
                if (type != null)
                {
                    return(type);
                }
            }

            return(null);
        }
Пример #5
0
        private static IEnumerable <ParamInfo> GetData(object obj)
        {
            ITypeFetch typeFetch  = UtilContainer.Resolve <ITypeFetch>();
            var        paramInfos = typeFetch.GetParamInfos(obj, obj.GetType());

            SetParamTypeName(paramInfos);
            return(paramInfos);
        }
Пример #6
0
        public static void Init(string codePath)
        {
            TemplateCodePath = codePath;
            IExcuter func() => new CSharpExcuter();

            UtilContainer.UseExcuter(func);
            UtilContainer.Use <IAssemblyLoader>(new AssemblyLoader());
        }
Пример #7
0
        public static Type Build(string templeFileName, string expression, string className, IEnumerable <ParamInfo> paramInfos, IEnumerable <string> nameSpaces)
        {
            var     tempFile = BuildCode(className, templeFileName, expression, paramInfos, nameSpaces);
            ILogger logger   = UtilContainer.Resolve <ILogger>();

            logger.Info(string.Concat("code template:", tempFile));
            var type = BuildType(tempFile, className);

            return(type);
        }
Пример #8
0
        public string Regular(string expression, IEnumerable <ExpressionInfo> expressionInfos, Expression exps, IEnumerable <Type> staticTypes)
        {
            foreach (var expressionInfo in expressionInfos.OrderByDescending(e => e.StartIndex))
            {
                var regular = Create(expressionInfo);
                if (regular == null)
                {
                    continue;
                }

                expression = regular.Regular(expression, expressionInfo, exps, staticTypes);
            }

            IKeyWordProvider keyWordProvider = UtilContainer.Resolve <IKeyWordProvider>();

            return(expression.Replace(keyWordProvider.Param, string.Empty));
        }
Пример #9
0
        private static Type CompileType(string originalClassName, IEnumerable <SyntaxTree> syntaxTrees)
        {
            var assemblyName = $"{originalClassName}.g";
            var compilation  = CSharpCompilation.Create(assemblyName, syntaxTrees,
                                                        options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                               .AddReferences(locations.Select(x => MetadataReference.CreateFromFile(x)));

            // 编译到内存流中。
            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms);
                if (result.Success)
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    var assembly = Assembly.Load(ms.ToArray());
                    return(assembly.GetTypes().First(x => x.Name == originalClassName));
                }

                ILogger logger = UtilContainer.Resolve <ILogger>();
                logger.Warning(string.Join("\r\n", result.Diagnostics.Select(d => d.ToString())));

                throw new ArgumentException(string.Join(string.Intern(";"), result.Diagnostics.Select(d => d.ToString())));
            }
        }