コード例 #1
0
        public static ListBuilder <T, TRef> HasMany <T, TRef>(this Builder <T> builder)
            where T : ICollection <TRef>
        {
            var child = new ListBuilder <T, TRef>();

            child.Add = (o, v) => o.Add(v);
            builder.Builders.Add(child);
            return(child);
        }
コード例 #2
0
        public ListBuilder <T, TRef> HasMany <TColl, TRef>(Func <T, TColl> get, Action <T, TColl> set = null)
            where TColl : class, ICollection <TRef>
        {
            var builder = new ListBuilder <T, TRef>();

            builder.Add = (o, v) =>
            {
                var list = get(o);
                if (list == null)
                {
                    if (set == null)
                    {
                        throw new InvalidOperationException("Could not set new collection instance to property");
                    }
                    list = Activator.CreateInstance <TColl>();
                    set(o, list);
                }
                list.Add(v);
            };
            Builders.Add(builder);
            return(builder);
        }