Exemple #1
0
        private Dictionary <string, TSMemberDescription> GetMembers(Members members, ref string enumerableType, string typename)
        {
            var membersList = members.Cast().ToList();
            TSMemberDescription defaultProperty = null;
            var ret = membersList.Where(x => !x.IsRestricted() && x.Name != "_NewEnum").ToLookup(x => x.Name).Select(grp => {
                var memberKVP     = KVP(grp.Key, GetMemberDescriptionForName(grp, typename));
                var defaultMember = grp.Where(x => x.MemberId == 0).ToList();
                if (!memberKVP.Value.IsProperty && defaultMember.Any())
                {
                    if (grp.Count() != defaultMember.Count)
                    {
                        throw new Exception("Default and non-default properties on the same name");
                    }
                    if (defaultProperty != null)
                    {
                        throw new Exception("Multiple default properties in 'members'");
                    }
                    defaultProperty = memberKVP.Value;
                }
                return(memberKVP);
            }).ToDictionary();

            if (defaultProperty != null)
            {
                ret.Add("", defaultProperty);
            }

            var enumerableType1 = enumerableType; //because ref parameters cannot be used within lambda expressions

            membersList.ToLookup(x => x.Name).IfContainsKey("_NewEnum", mi => {
                ret.IfContainsKey("Item", itemMI => enumerableType1 = ((TSSimpleType)itemMI.ReturnType).FullName);
            });

            // there is an overload of EnumeratorConstructor that accepts anything with an Item member, and resolves the enumerator type to the return type of Item
            var lookup = ret.WhereKVP((name, descr) => name == "Item").ToLookup(kvp => kvp.Value.ReturnType);

            if (lookup.Count == 1)
            {
                enumerableType1 = null;
            }
            enumerableType = enumerableType1;

            return(ret);
        }