Exemple #1
0
        public Expression?CreateExpression(IOvBuilderContext builderContext)
        {
            var items = builderContext.OfType <T>().ToArray();

            if (items.Any())
            {
                var inputType = builderContext.OfType <SourceObjectOvBuilderContextItem>()
                                .First()
                                .InputType;
                var extendType = builderContext.OfType <ExtendObjectOvBuilderContextItem>()
                                 .FirstOrDefault()
                                 ?.ExtendObjectType;
                var exps = CreateExpressions();
                if (items.Length == 1)
                {
                    return(exps.Single());
                }

                var pExp = Expression.Parameter(inputType, "s");
                if (extendType != null)
                {
                    var sourceExp       = Expression.Parameter(extendType, "e");
                    var blockItems      = exps.Select(x => Expression.Invoke(x, pExp, sourceExp));
                    var blockExpression = Expression.Block(blockItems);
                    var actionType      = Expression.GetActionType(inputType, extendType);
                    var finalExp        = Expression.Lambda(actionType, blockExpression, pExp, sourceExp);
                    return(finalExp);
                }
                else
                {
                    var blockItems      = exps.Select(x => Expression.Invoke(x, pExp));
                    var blockExpression = Expression.Block(blockItems);
                    var actionType      = Expression.GetActionType(inputType);
                    var finalExp        = Expression.Lambda(actionType, blockExpression, pExp);
                    return(finalExp);
                }

                IEnumerable <Expression> CreateExpressions()
                {
                    foreach (var item in items)
                    {
                        yield return(CreateCore(inputType, extendType, item));
                    }
                }
            }

            return(null);
        }
        public Expression?CreateExpression(IOvBuilderContext builderContext)
        {
            var item = builderContext.OfType <T>().LastOrDefault();

            if (item != null)
            {
                var inputType = builderContext.OfType <SourceObjectOvBuilderContextItem>()
                                .First()
                                .InputType;
                var extendType = builderContext.OfType <ExtendObjectOvBuilderContextItem>()
                                 .FirstOrDefault()
                                 ?.ExtendObjectType;
                return(CreateCore(inputType, extendType, item));
            }

            return(null);
        }
        internal static bool TryGetContextItem <TItem>(this IOvBuilderContext builderContext,
                                                       out TItem?obj)
            where TItem : class
        {
            var item = builderContext.OfType <TItem>().FirstOrDefault();

            if (item != null)
            {
                obj = item;
                return(true);
            }

            obj = default !;