Exemple #1
0
        private static void IterateObject(BindingContext context,
                                          ObjectDescriptor descriptor,
                                          BlockParamsValueProvider blockParamsValueProvider,
                                          object target,
                                          IEnumerable <object> properties,
                                          Type targetType,
                                          Action <BindingContext, TextWriter, object> template,
                                          Action <BindingContext, TextWriter, object> ifEmpty)
        {
            using (var iterator = ObjectEnumeratorValueProvider.Create(context.Configuration))
            {
                blockParamsValueProvider?.Configure(BlockParamsObjectEnumeratorConfiguration, iterator);

                iterator.Index = 0;
                var  accessor   = descriptor.MemberAccessor;
                var  enumerable = new ExtendedEnumerable <object>(properties);
                bool enumerated = false;

                foreach (var enumerableValue in enumerable)
                {
                    enumerated   = true;
                    iterator.Key = enumerableValue.Value.ToString();
                    var key = iterator.Key.Intern();
                    iterator.Value = accessor.TryGetValue(target, targetType, key, out var value) ? value : null;
                    iterator.First = enumerableValue.IsFirst;
                    iterator.Last  = enumerableValue.IsLast;
                    iterator.Index = enumerableValue.Index;

                    using (var innerContext = context.CreateChildContext(iterator.Value))
                    {
                        innerContext.RegisterValueProvider(iterator);
                        template(context, context.TextWriter, innerContext);
                        innerContext.UnregisterValueProvider(iterator);
                    }
                }

                if (iterator.Index == 0 && !enumerated)
                {
                    ifEmpty(context, context.TextWriter, context.Value);
                }
            }
        }
Exemple #2
0
        private static void IterateObjectWithStaticProperties(BindingContext context,
                                                              ObjectDescriptor descriptor,
                                                              BlockParamsValueProvider blockParamsValueProvider,
                                                              object target,
                                                              IList properties,
                                                              Type targetType,
                                                              Action <BindingContext, TextWriter, object> template,
                                                              Action <BindingContext, TextWriter, object> ifEmpty)
        {
            using (var iterator = ObjectEnumeratorValueProvider.Create(context.Configuration))
            {
                blockParamsValueProvider?.Configure(BlockParamsObjectEnumeratorConfiguration, iterator);

                var accessor = descriptor.MemberAccessor;

                var count = properties.Count;
                for (iterator.Index = 0; iterator.Index < count; iterator.Index++)
                {
                    iterator.Key   = properties[iterator.Index].ToString();
                    iterator.Value = accessor.TryGetValue(target, targetType, iterator.Key, out var value) ? value : null;
                    iterator.First = iterator.Index == 0;
                    iterator.Last  = iterator.Index == count - 1;

                    using (var innerContext = context.CreateChildContext(iterator.Value))
                    {
                        innerContext.RegisterValueProvider(iterator);
                        template(context, context.TextWriter, innerContext);
                        innerContext.UnregisterValueProvider(iterator);
                    }
                }

                if (iterator.Index == 0)
                {
                    ifEmpty(context, context.TextWriter, context.Value);
                }
            }
        }