Beispiel #1
0
        public static IEnumerable <CustomAttributeBuilder> GetNonInheritableAttributes(this IMemberInfo member)
        {
            Debug.Assert(member != null, "member != null");
            var attributes =
#if SILVERLIGHT
                member.GetCustomAttributes(false);
#else
                CustomAttributeData.GetCustomAttributes(member);
#endif

            foreach (var attribute in attributes)
            {
                var attributeType =
#if SILVERLIGHT
                    attribute.GetType();
#else
                    attribute.Constructor.DeclaringType;
#endif
                if (ShouldSkipAttributeReplication(attributeType))
                {
                    continue;
                }

                CustomAttributeBuilder builder;
                try
                {
                    builder = CreateBuilder(attribute
#if SILVERLIGHT
                                            as Attribute
#endif
                                            );
                }
                catch (ArgumentException e)
                {
                    var message =
                        string.Format(
                            "Due to limitations in CLR, DynamicProxy was unable to successfully replicate non-inheritable attribute {0} on {1}{2}. To avoid this error you can chose not to replicate this attribute type by calling '{3}.Add(typeof({0}))'.",
                            attributeType.FullName, (member.ReflectedType() == null) ? "" : member.ReflectedType().FullName,
#if !NETFX_CORE
                            (member is Type) ? "" : ("." + member.Name), typeof(AttributesToAvoidReplicating).FullName
#else
                            // @mbrit - 2012-05-30 - thought a ToString might be OK here?
                            member
#endif
                            );
                    throw new ProxyGenerationException(message, e);
                }
                if (builder != null)
                {
                    yield return(builder);
                }
            }
        }