Example #1
0
 /// <inheritdoc/>
 public void Add(MemberDescription member)
 {
     if (member.ParameterType == typeof(T) || member.PropertyType == typeof(T))
     {
         foreach (var name in member.Names)
         {
             if (!_members.TryGetValue(name, out var existing))
             {
                 _members.Add(name, member);
             }
             else if (!ReferenceEquals(member, existing))
             {
                 throw new ArgumentException(Properties.Resources.Reflection_SameName.FormatString(name, member.Member.Name, existing.Member.Name));
             }
         }
     }
 }
Example #2
0
        /// <summary>
        /// Gets all the members that are defined on a type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>All the members on a type.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> is <c>null</c>.</exception>
        public static IEnumerable <MemberDescription> GetMembers(Type type)
        {
            var info = type.ThrowIfNull(nameof(type)).GetTypeInfo();

            foreach (var member in info.GetMembers(BindingFlags.Instance | BindingFlags.Public))
            {
                var mt = member.MemberType;
                if (mt != MemberTypes.Property &&
                    mt != MemberTypes.Field &&
                    mt != MemberTypes.Method)
                {
                    continue;
                }
                var md = new MemberDescription(member);
                if (md.Names != null && md.Names.Count > 0)
                {
                    yield return(md);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParameterMap"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="comparer">The comparer used.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> is <c>null</c>.</exception>
        public ParameterMap(Type type, IEqualityComparer <string> comparer)
        {
            type.ThrowIfNull(nameof(type));

            foreach (var member in type.GetTypeInfo().GetMembers(BindingFlags.Instance | BindingFlags.Public))
            {
                // Ignore unsupported members
                var mt = member.MemberType;
                if (mt != MemberTypes.Property &&
                    mt != MemberTypes.Field &&
                    mt != MemberTypes.Method)
                {
                    continue;
                }
                var desc = new MemberDescription(member);
                if (desc.Names.Count == 0)
                {
                    continue;
                }
                var descType = desc.ParameterType;
                if (descType == typeof(void))
                {
                    descType = desc.PropertyType;
                }
                if (descType == typeof(void))
                {
                    continue;
                }

                // Add a map
                if (!_memberMaps.TryGetValue(descType, out var members))
                {
                    // Create a new instance
                    members = CreateMap(descType, comparer);
                    _memberMaps.Add(descType, members);
                }
                members.Add(desc);
            }
        }