public static void GetTypeName_SetByString()
        {
            var typeName  = "type";
            var attribute = new AttributeProviderAttribute(typeName);

            Assert.Equal(typeName, attribute.TypeName);
        }
        public static void GetTypeName_SetByType()
        {
            var type      = typeof(AttributeProviderAttribute);
            var attribute = new AttributeProviderAttribute(type);

            Assert.Equal(type.AssemblyQualifiedName, attribute.TypeName);
        }
        public void Ctor_Type(Type type)
        {
            var attribute = new AttributeProviderAttribute(type);

            Assert.Equal(type.AssemblyQualifiedName, attribute.TypeName);
            Assert.Null(attribute.PropertyName);
        }
        public void Ctor_TypeName(string typeName)
        {
            var attribute = new AttributeProviderAttribute(typeName);

            Assert.Equal(typeName, attribute.TypeName);
            Assert.Null(attribute.PropertyName);
        }
        public static void GetTypeName_SetByType()
        {
            var type = typeof(AttributeProviderAttribute);
            var attribute = new AttributeProviderAttribute(type);

            Assert.Equal(type.AssemblyQualifiedName, attribute.TypeName);
        }
        public static void GetTypeName_SetByString()
        {
            var typeName = "type";
            var attribute = new AttributeProviderAttribute(typeName);

            Assert.Equal(typeName, attribute.TypeName);
        }
        public static void GetPropertyName()
        {
            var typeName     = "type";
            var propertyName = "property";
            var attribute    = new AttributeProviderAttribute(typeName, propertyName);

            Assert.Equal(propertyName, attribute.PropertyName);
        }
        public static void GetPropertyName()
        {
            var typeName = "type";
            var propertyName = "property";
            var attribute = new AttributeProviderAttribute(typeName, propertyName);

            Assert.Equal(propertyName, attribute.PropertyName);
        }
        public void CtorTest()
        {
            AttributeProviderAttribute a;

            a = new AttributeProviderAttribute(typeof(string));
            Assert.AreEqual(typeof(string).AssemblyQualifiedName, a.TypeName, "1");
            Assert.IsNull(a.PropertyName, "2");

            a = new AttributeProviderAttribute("typename");
            Assert.AreEqual("typename", a.TypeName, "3");
            Assert.AreEqual(null, a.PropertyName, "4");

            a = new AttributeProviderAttribute("typename", "propertyname");
            Assert.AreEqual("typename", a.TypeName, "5");
            Assert.AreEqual("propertyname", a.PropertyName, "6");
        }
예제 #10
0
        /// <summary>
        ///     Additional metadata attributes for attached properties
        ///     are taken from the "Get" method.
        /// </summary>
        private AttributeCollection GetAttachedPropertyAttributes()
        {
            MethodInfo mi = GetAttachedPropertyMethod(_dp);

            if (mi != null)
            {
                Type        attrType  = AttributeType;
                Attribute[] attrArray = (Attribute[])mi.GetCustomAttributes(attrType, true);

                Type        propertyReflectionType = TypeDescriptor.GetReflectionType(_dp.PropertyType);
                Attribute[] typeAttrArray          = (Attribute[])propertyReflectionType.GetCustomAttributes(attrType, true);
                if (typeAttrArray != null && typeAttrArray.Length > 0)
                {
                    // Merge attrArry and typeAttrArray
                    Attribute[] mergedAttrArray = new Attribute[attrArray.Length + typeAttrArray.Length];
                    Array.Copy(attrArray, mergedAttrArray, attrArray.Length);
                    Array.Copy(typeAttrArray, 0, mergedAttrArray, attrArray.Length, typeAttrArray.Length);
                    attrArray = mergedAttrArray;
                }

                // Look for and expand AttributeProvider attributes.  These are attributes
                // that allow a method to adopt attributes from another location.  This
                // allows generic properties, such as "public object DataSource {get; set;}",
                // to share a common set of attributes.

                Attribute[] addAttrs = null;

                foreach (Attribute attr in attrArray)
                {
                    AttributeProviderAttribute pa = attr as AttributeProviderAttribute;
                    if (pa != null)
                    {
                        Type providerType = Type.GetType(pa.TypeName);
                        if (providerType != null)
                        {
                            Attribute[] paAttrs = null;
                            if (!string.IsNullOrEmpty(pa.PropertyName))
                            {
                                MemberInfo[] milist = providerType.GetMember(pa.PropertyName);
                                if (milist.Length > 0 && milist[0] != null)
                                {
                                    paAttrs = (Attribute[])milist[0].GetCustomAttributes(typeof(Attribute), true);
                                }
                            }
                            else
                            {
                                paAttrs = (Attribute[])providerType.GetCustomAttributes(typeof(Attribute), true);
                            }

                            if (paAttrs != null)
                            {
                                if (addAttrs == null)
                                {
                                    addAttrs = paAttrs;
                                }
                                else
                                {
                                    Attribute[] newArray = new Attribute[addAttrs.Length + paAttrs.Length];
                                    addAttrs.CopyTo(newArray, 0);
                                    paAttrs.CopyTo(newArray, addAttrs.Length);
                                    addAttrs = newArray;
                                }
                            }
                        }
                    }
                }

                // See if we gathered additional attributes.  These are always lower priority
                // and therefore get tacked onto the end of the list
                if (addAttrs != null)
                {
                    Attribute[] newArray = new Attribute[addAttrs.Length + attrArray.Length];
                    attrArray.CopyTo(newArray, 0);
                    addAttrs.CopyTo(newArray, attrArray.Length);
                    attrArray = newArray;
                }

                return(new AttributeCollection(attrArray));
            }

            return(AttributeCollection.Empty);
        }