using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
using System.ComponentModel.Design;
using System.Globalization;
-
+using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;
Assert.AreEqual ("Length", pc [0].Name, "#2");
}
-#if NET_4_0
[Test]
public void InterfaceType ()
{
Assert.IsFalse (interface_type.IsInterface, "#A4");
Assert.IsFalse (interface_type.IsPublic, "#A5");
}
-#endif
+
+ [Test]
+ public void DynamicAttributesShouldBeReturnedForType()
+ {
+ var testType = typeof(AttrTestClass);
+ TypeDescriptor.AddAttributes(testType, new ProviderTestAttribute());
+ var registeredAttributeTypes = TypeDescriptor.GetAttributes(testType).Cast<Attribute>().Select(attr => attr.GetType()).ToArray();
+ Assert.AreEqual(2, registeredAttributeTypes.Length);
+ Assert.Contains(typeof(ProviderTestAttribute), registeredAttributeTypes);
+ Assert.Contains(typeof(SerializableAttribute), registeredAttributeTypes);
+ }
+
+ [Test]
+ public void DynamicAttributesShouldBeReturnedForInstance()
+ {
+ var testObj = new AttrTestClass();
+ TypeDescriptor.AddAttributes(testObj, new ProviderTestAttribute());
+ var registeredAttributeTypes = TypeDescriptor.GetAttributes(testObj).Cast<Attribute>().Select(attr => attr.GetType()).ToArray();
+ Assert.AreEqual(2, registeredAttributeTypes.Length);
+ Assert.Contains(typeof(ProviderTestAttribute), registeredAttributeTypes);
+ Assert.Contains(typeof(SerializableAttribute), registeredAttributeTypes);
+ }
+
+ [Test]
+ public void CustomTypeDescriptorsShouldBeUsedForType()
+ {
+ var testType = typeof(CustomDescriptorTestClass);
+ TypeDescriptor.AddProvider(new CustomDescriptionTestProvider(), testType);
+
+ var registeredAttributeTypes = TypeDescriptor.GetAttributes(testType).Cast<Attribute>().Select(attr => attr.GetType()).ToArray();
+ Assert.AreEqual(1, registeredAttributeTypes.Length);
+ Assert.Contains(typeof(ProviderTestAttribute), registeredAttributeTypes);
+
+ var registeredPropertyDescriptorTypes = TypeDescriptor.GetProperties(testType).Cast<PropertyDescriptor>().Select(prop => prop.GetType()).ToArray();
+ Assert.AreEqual(1, registeredPropertyDescriptorTypes.Length);
+ Assert.Contains(typeof(ProviderTestPropertyDescriptor), registeredPropertyDescriptorTypes);
+ }
+
+ [Test]
+ public void CustomTypeDescriptorsShouldBeUsedForInstance()
+ {
+ var testObj = new CustomDescriptorTestClass();
+ TypeDescriptor.AddProvider(new CustomDescriptionTestProvider(), testObj);
+
+ var registeredAttributeTypes = TypeDescriptor.GetAttributes(testObj).Cast<Attribute>().Select(attr => attr.GetType()).ToArray();
+ Assert.AreEqual(1, registeredAttributeTypes.Length);
+ Assert.Contains(typeof(ProviderTestAttribute), registeredAttributeTypes);
+
+ var registeredPropertyDescriptorTypes = TypeDescriptor.GetProperties(testObj).Cast<PropertyDescriptor>().Select(prop => prop.GetType()).ToArray();
+ Assert.AreEqual(1, registeredPropertyDescriptorTypes.Length);
+ Assert.Contains(typeof(ProviderTestPropertyDescriptor), registeredPropertyDescriptorTypes);
+ }
+
+ private class CustomDescriptionTestProvider : TypeDescriptionProvider
+ {
+ public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
+ {
+ return new CustomTestProvider();
+ }
+ }
+
+ private class CustomTestProvider : CustomTypeDescriptor
+ {
+ public CustomTestProvider()
+ {
+ }
+
+ public override AttributeCollection GetAttributes()
+ {
+ return new AttributeCollection(new ProviderTestAttribute());
+ }
+
+ public override PropertyDescriptorCollection GetProperties()
+ {
+ return new PropertyDescriptorCollection(new PropertyDescriptor[] { new ProviderTestPropertyDescriptor() });
+ }
+ }
+
+ private class ProviderTestAttribute : Attribute
+ {
+ }
+
+ private class ProviderTestPropertyDescriptor : PropertyDescriptor
+ {
+ public ProviderTestPropertyDescriptor()
+ : base("test", new Attribute[0])
+ {
+ }
+
+ public override bool CanResetValue(object component)
+ {
+ return false;
+ }
+
+ public override object GetValue(object component)
+ {
+ return null;
+ }
+
+ public override void ResetValue(object component)
+ {
+ }
+
+ public override void SetValue(object component, object value)
+ {
+ }
+
+ public override bool ShouldSerializeValue(object component)
+ {
+ return false;
+ }
+
+ public override Type ComponentType
+ {
+ get
+ {
+ return typeof(object);
+ }
+ }
+
+ public override bool IsReadOnly
+ {
+ get
+ {
+ return false;
+ }
+ }
+
+ public override Type PropertyType
+ {
+ get
+ {
+ return typeof(object);
+ }
+ }
+ }
+
+ [Serializable]
+ private class AttrTestClass
+ {
+ }
+
+ private class CustomDescriptorTestClass
+ {
+ }
}
}