예제 #1
0
        public void Bind(PropertyInfo property, IBindingContext context)
        {
            var type = property.PropertyType;
            var itemType = type.GetGenericArguments()[0];
            if (type.IsInterface)
            {
                type = _collectionTypeProvider.GetCollectionType(type, itemType);
            }

            object collection = Activator.CreateInstance(type);
            var collectionType = collection.GetType();

            Func<object, bool> addToCollection = obj =>
                {
                    if (obj != null)
                    {
                        var addMethod = _addMethods[collectionType];
                        addMethod.Invoke(collection, new[] {obj});
                        return true;
                    }
                    return false;
                };

            //TODO: work in the IElementNamingConvention somehow

            var formatString = property.Name + "[{0}]";

            int index = 0;
            string prefix;
            do
            {
                prefix = formatString.ToFormat(index);
                index++;
            } while (addToCollection(context.BindCollectionItem(prefix, itemType)));

            property.SetValue(context.Object, collection, null);
        }