Exemplo n.º 1
0
        public static object[] GetAttributes([NotNull] MemberInfo member)
        {
            object[] attributes;
            if (AttributesByMember.TryGetValue(member, out attributes))
            {
                return(attributes);
            }

            attributes = member.GetCustomAttributes(InheritMemberAttributes);
            PluginAttributeConverterProvider.ConvertAll(ref attributes);
                        #if THREAD_SAFE
            AttributesByMember[member] = attributes;
                        #else
            AttributesByMember.Add(member, attributes);
                        #endif
            return(attributes);
        }
Exemplo n.º 2
0
        public static object[] GetAttributes([NotNull] Type classType)
        {
            object[] attributes;
            if (AttributesByClass.TryGetValue(classType, out attributes))
            {
                return(attributes);
            }

            attributes = classType.GetCustomAttributes(InheritClassAttributes);
            PluginAttributeConverterProvider.ConvertAll(ref attributes);
                        #if THREAD_SAFE
            AttributesByClass[classType] = attributes;
                        #else
            AttributesByClass.Add(classType, attributes);
                        #endif
            return(attributes);
        }
Exemplo n.º 3
0
        public static IDictionary <string, object> ObjectToDictionaryCached(object obj, MemberTypes memberTypes = MemberTypes.Property,
                                                                            ObjectToDictionaryFlags flags       = ObjectToDictionaryFlags.None)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if ((memberTypes & ~(MemberTypes.Field | MemberTypes.Property)) != 0)
            {
                throw new ArgumentException(Resources.FieldOrPropertyAllowedOnly, nameof(memberTypes));
            }

            Type type = obj.GetType();

#pragma warning disable IDE0018 // Inline variable declaration
            MemberDescriptor[] memberDescriptors;
#pragma warning restore IDE0018 // Inline variable declaration

#if !NETSTANDARD1_0
            LazyInitializer.EnsureInitialized(ref s_memberDescriptorCache, () => new System.Collections.Concurrent.ConcurrentDictionary <Type, MemberDescriptor[]>());
#else
            LazyInitializer.EnsureInitialized(ref s_memberDescriptorCache, () => new Dictionary <Type, MemberDescriptor[]>());
            lock (s_memberDescriptorCache)
#endif
            if (!s_memberDescriptorCache.TryGetValue(type, out memberDescriptors))
            {
                memberDescriptors = GetObjectToDictionaryMembers(type).Select(MemberDescriptor.Create).ToArray();
#if !NETSTANDARD1_0
                s_memberDescriptorCache.TryAdd(type, memberDescriptors);
#else
                s_memberDescriptorCache.Add(type, memberDescriptors);
#endif
            }

            var excludeReadOnly = (flags & ObjectToDictionaryFlags.ExcludeReadOnlyProperties) != 0;

            var dictionary = new Dictionary <string, object>((flags & ObjectToDictionaryFlags.IgnoreCase) == 0 ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);

            MemberDescriptor memberDescriptor;
            for (int i = 0, n = memberDescriptors.Length; i < n; i++)
            {
                if (((memberDescriptor = memberDescriptors[i]).Member.MemberType() & memberTypes) != 0 &&
                    (!excludeReadOnly || !memberDescriptor.IsReadOnly))
                {
                    dictionary.Add(memberDescriptor.Member.Name, memberDescriptor.ValueAccessor(obj));
                }
            }

            return(dictionary);
        }