2 // System.ComponentModel.TypeDescriptorTests test cases
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (c) 2004 Novell, Inc. (http://www.ximian.com)
11 using System.Collections;
12 using System.ComponentModel;
13 using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
14 using System.ComponentModel.Design;
15 using System.Globalization;
17 using System.Collections.Generic;
19 using NUnit.Framework;
20 using System.Reflection;
22 namespace MonoTests.System.ComponentModel
26 event EventHandler Fired;
27 event EventHandler Closed;
36 event EventHandler Destroyed;
43 class MyDesigner: IDesigner
49 public IComponent Component {get{return null; }}
51 public DesignerVerbCollection Verbs {get{return null; }}
53 public void DoDefaultAction () { }
55 public void Initialize (IComponent component) { }
57 public void Dispose () { }
60 class MyOtherDesigner: IDesigner
62 public MyOtherDesigner()
66 public IComponent Component {get {return null; } }
67 public DesignerVerbCollection Verbs { get {return null; } }
68 public void DoDefaultAction () { }
69 public void Initialize (IComponent component) { }
70 public void Dispose () { }
75 public IComponent Component { get { return null; } }
77 public IContainer Container { get { return null; } }
79 public bool DesignMode { get { return true; } }
81 public string Name { get { return "TestName"; } set { } }
83 public object GetService (Type t)
85 if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
90 class MyFilter: ITypeDescriptorFilterService
92 public bool FilterAttributes (IComponent component,IDictionary attributes)
94 Attribute ea = new DefaultEventAttribute ("AnEvent");
95 attributes [ea.TypeId] = ea;
96 ea = new DefaultPropertyAttribute ("TestProperty");
97 attributes [ea.TypeId] = ea;
98 ea = new EditorAttribute ();
99 attributes [ea.TypeId] = ea;
103 public bool FilterEvents (IComponent component, IDictionary events)
105 events.Remove ("AnEvent");
109 public bool FilterProperties (IComponent component, IDictionary properties)
111 properties.Remove ("TestProperty");
116 class AnotherSite: ISite
118 public IComponent Component { get { return null; } }
120 public IContainer Container { get { return null; } }
122 public bool DesignMode { get { return true; } }
124 public string Name { get { return "TestName"; } set { } }
126 public object GetService (Type t)
128 if (t == typeof(ITypeDescriptorFilterService)) {
129 return new AnotherFilter ();
135 class NoFilterSite : ISite
137 public NoFilterSite () : this (null)
141 public NoFilterSite (IContainer container)
143 _container = container;
146 public IComponent Component {
150 public IContainer Container {
151 get { return _container; }
154 public bool DesignMode { get { return true; } }
156 public string Name { get { return "TestName"; } set { } }
158 public object GetService (Type t)
163 public IContainer _container;
166 class MyContainer : IContainer
168 public MyContainer ()
170 _components = new ComponentCollection (new IComponent [0]);
173 public ComponentCollection Components {
174 get { return _components; }
177 public void Add (IComponent component)
181 public void Add (IComponent component, string name)
185 public void Dispose ()
189 public void Remove (IComponent component)
193 private ComponentCollection _components;
196 class AnotherFilter: ITypeDescriptorFilterService
198 public bool FilterAttributes (IComponent component,IDictionary attributes) {
199 Attribute ea = new DefaultEventAttribute ("AnEvent");
200 attributes [ea.TypeId] = ea;
201 ea = new DefaultPropertyAttribute ("TestProperty");
202 attributes [ea.TypeId] = ea;
203 ea = new EditorAttribute ();
204 attributes [ea.TypeId] = ea;
208 public bool FilterEvents (IComponent component, IDictionary events) {
212 public bool FilterProperties (IComponent component, IDictionary properties) {
217 [DescriptionAttribute ("my test component")]
218 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
219 public class MyComponent: Component
223 [DescriptionAttribute ("test")]
224 public event EventHandler AnEvent;
226 public event EventHandler AnotherEvent;
228 public MyComponent ()
232 public MyComponent (ISite site)
237 public MyComponent (IContainer container)
239 container.Add (this);
242 [DescriptionAttribute ("test")]
243 public virtual string TestProperty
246 set { prop = value; }
249 public string AnotherProperty
252 set { prop = value; }
256 public string YetAnotherProperty
265 public string Address {
269 public string Country {
273 private string HairColor {
277 protected int Weight {
281 internal int Height {
285 public string WriteOnlyProperty {
286 set { prop = value; }
290 [DescriptionAttribute ("my test derived component")]
291 [DesignerAttribute (typeof(MyOtherDesigner))]
292 public class MyDerivedComponent: MyComponent
296 public MyDerivedComponent ()
300 public MyDerivedComponent (ISite site) : base (site)
304 [DescriptionAttribute ("test derived")]
305 public override string TestProperty
308 set { prop = value; }
312 [DescriptionAttribute ("test derived")]
313 public new string AnotherProperty
315 get { return base.AnotherProperty; }
316 set { base.AnotherProperty = value; }
319 public new object YetAnotherProperty
326 [DefaultProperty("AnotherProperty")]
327 [DefaultEvent("AnotherEvent")]
328 [DescriptionAttribute ("my test component")]
329 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
330 public class AnotherComponent: Component {
333 [DescriptionAttribute ("test")]
334 public event EventHandler AnEvent;
336 public event EventHandler AnotherEvent;
338 public AnotherComponent () {
341 public AnotherComponent (ISite site) {
345 [DescriptionAttribute ("test")]
346 public string TestProperty {
348 set { prop = value; }
351 public string AnotherProperty {
353 set { prop = value; }
358 public interface ITestInterface
360 void TestFunction ();
363 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
364 public class TestClass
373 [DescriptionAttribute ("bla")]
374 public class TestDerivedClass : TestClass, ITestInterface
376 public void TestFunction ()
380 public struct TestStruct
385 public class TestCustomTypeDescriptor : ICustomTypeDescriptor
387 public string methods_called = "";
389 public void ResetMethodsCalled ()
394 public TypeConverter GetConverter()
396 return new StringConverter ();
399 public EventDescriptorCollection GetEvents(Attribute[] attributes)
401 methods_called += "1";
405 public EventDescriptorCollection GetEvents()
407 methods_called += "2";
411 public string GetComponentName()
413 return "MyComponentnName";
416 public object GetPropertyOwner(PropertyDescriptor pd)
421 public AttributeCollection GetAttributes()
423 methods_called += "3";
427 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
429 methods_called += "4";
430 return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
433 public PropertyDescriptorCollection GetProperties()
435 methods_called += "5";
436 return new PropertyDescriptorCollection(new PropertyDescriptor[0]);
439 public object GetEditor(Type editorBaseType)
444 public PropertyDescriptor GetDefaultProperty()
446 methods_called += "6";
450 public EventDescriptor GetDefaultEvent()
452 methods_called += "7";
456 public string GetClassName()
458 return this.GetType ().Name;
462 class MyCustomTypeDescriptor : CustomTypeDescriptor
464 public MyTypeDescriptionProvider Provider { get; private set; }
466 public MyCustomTypeDescriptor (MyTypeDescriptionProvider provider)
471 public override string GetClassName ()
477 class MyTypeDescriptionProvider : TypeDescriptionProvider
479 public string Id { get; private set; }
480 public bool CreateInstanceCalled { get; private set; }
482 public MyTypeDescriptionProvider ()
487 public MyTypeDescriptionProvider (string id)
492 public override ICustomTypeDescriptor GetTypeDescriptor (Type objectType, object instance)
494 return new MyCustomTypeDescriptor (this);
497 public override object CreateInstance (IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
499 CreateInstanceCalled = true;
500 return base.CreateInstance (provider, objectType, argTypes, args);
505 public class TypeDescriptorTests
507 MyComponent com = new MyComponent ();
508 MyComponent sitedcom = new MyComponent (new MySite ());
509 MyComponent nfscom = new MyComponent (new NoFilterSite (new MyContainer ()));
510 AnotherComponent anothercom = new AnotherComponent ();
513 [ExpectedException (typeof (ArgumentNullException))]
514 public void TestAddAttributes_Type_Attributes_1 ()
516 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes ((Type) null, null);
520 [ExpectedException (typeof (ArgumentNullException))]
521 public void TestAddAttributes_Type_Attributes_2 ()
523 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes (typeof (string), null);
527 public void TestAddAttributes_Type_Attributes_3 ()
529 Attribute[] new_attributes = new Attribute[] {
530 new ReadOnlyAttribute (true),
531 new BindableAttribute (true)
534 TypeDescriptionProvider provider = null;
535 ICustomTypeDescriptor descriptor;
536 AttributeCollection attributes;
539 provider = TypeDescriptor.AddAttributes (typeof (string), new Attribute[] { });
540 Assert.IsNotNull (provider, "#A1");
542 descriptor = provider.GetTypeDescriptor (typeof (string));
543 Assert.IsNotNull (descriptor, "#A1-1");
545 attributes = descriptor.GetAttributes ();
546 Assert.IsNotNull (attributes, "#A1-2");
548 if (provider != null)
549 TypeDescriptor.RemoveProvider (provider, typeof (string));
554 provider = TypeDescriptor.AddAttributes (typeof (string), new_attributes);
555 Assert.IsNotNull (provider, "#B1");
557 descriptor = provider.GetTypeDescriptor (typeof (string));
558 Assert.IsNotNull (descriptor, "#B1-1");
560 attributes = descriptor.GetAttributes ();
561 Assert.IsNotNull (attributes, "#B1-2");
562 Assert.AreNotEqual (0, attributes.Count, "#B1-3");
563 Assert.IsTrue (attributes.Contains (new_attributes));
565 if (provider != null)
566 TypeDescriptor.RemoveProvider (provider, typeof (string));
571 [ExpectedException (typeof (ArgumentNullException))]
572 public void TestAddAttributes_Instance_Attributes_1 ()
574 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes ((object) null, null);
578 [ExpectedException (typeof (ArgumentNullException))]
579 public void TestAddAttributes_Instance_Attributes_2 ()
582 TypeDescriptionProvider provider = TypeDescriptor.AddAttributes (s, null);
586 public void TestAddAttributes_Instance_Attributes_3 ()
588 Attribute[] new_attributes = new Attribute[] {
589 new ReadOnlyAttribute (true),
590 new BindableAttribute (true)
593 TypeDescriptionProvider provider = null;
594 ICustomTypeDescriptor descriptor;
595 AttributeCollection attributes;
599 provider = TypeDescriptor.AddAttributes (s, new Attribute[] { });
600 Assert.IsNotNull (provider, "#A1");
602 descriptor = provider.GetTypeDescriptor (s);
603 Assert.IsNotNull (descriptor, "#A1-1");
605 attributes = descriptor.GetAttributes ();
606 Assert.IsNotNull (attributes, "#A1-2");
608 if (provider != null)
609 TypeDescriptor.RemoveProvider (provider, s);
614 provider = TypeDescriptor.AddAttributes (s, new_attributes);
615 Assert.IsNotNull (provider, "#B1");
617 descriptor = provider.GetTypeDescriptor (s);
618 Assert.IsNotNull (descriptor, "#B1-1");
620 attributes = descriptor.GetAttributes ();
621 Assert.IsNotNull (attributes, "#B1-2");
622 Assert.AreNotEqual (0, attributes.Count, "#B1-3");
623 Assert.IsTrue (attributes.Contains (new_attributes));
625 if (provider != null)
626 TypeDescriptor.RemoveProvider (provider, s);
631 [ExpectedException (typeof (ArgumentNullException))]
632 public void TestAddProvider_Provider_Instance_1 ()
634 TypeDescriptor.AddProvider (null, (object)null);
638 [ExpectedException (typeof (ArgumentNullException))]
639 public void TestAddProvider_Provider_Instance_2 ()
641 var provider = new MyTypeDescriptionProvider ();
642 TypeDescriptor.AddProvider (provider, (object) null);
646 public void TestAddProvider_Provider_Instance_3 ()
648 var instance = new MyComponent ();
649 var providers = new MyTypeDescriptionProvider[] {
650 new MyTypeDescriptionProvider ("One"),
651 new MyTypeDescriptionProvider ("Two"),
652 new MyTypeDescriptionProvider ("Three"),
653 new MyTypeDescriptionProvider ("Four")
657 TypeDescriptionProvider provider;
658 ICustomTypeDescriptor descriptor;
660 TypeDescriptor.AddProvider (providers[0], instance);
661 provider = TypeDescriptor.GetProvider (instance);
662 Assert.IsNotNull (provider, "#A1");
663 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
664 Assert.IsNotNull (descriptor, "#A1-1");
665 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2");
666 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3");
668 descriptor.GetProperties ();
670 TypeDescriptor.AddProvider (providers[1], instance);
671 provider = TypeDescriptor.GetProvider (instance);
672 Assert.IsNotNull (provider, "#B1");
673 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
674 Assert.IsNotNull (descriptor, "#B1-1");
675 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2");
677 // Providers are stored in a stack according to docs, but it's in reality
678 // a FIFO linked list
679 TypeDescriptor.AddProvider (providers[2], instance);
680 TypeDescriptor.AddProvider (providers[3], instance);
681 provider = TypeDescriptor.GetProvider (instance);
682 Assert.IsNotNull (provider, "#C1");
683 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
684 Assert.IsNotNull (descriptor, "#C1-1");
685 Assert.AreEqual ("Four", descriptor.GetClassName (), "#C1-2");
687 TypeDescriptor.RemoveProvider (providers[2], instance);
688 provider = TypeDescriptor.GetProvider (instance);
689 Assert.IsNotNull (provider, "#D1");
690 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
691 Assert.IsNotNull (descriptor, "#D1-1");
692 Assert.AreEqual ("Four", descriptor.GetClassName (), "#D1-2");
694 TypeDescriptor.RemoveProvider (providers[3], instance);
695 provider = TypeDescriptor.GetProvider (instance);
696 Assert.IsNotNull (provider, "#E1");
697 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
698 Assert.IsNotNull (descriptor, "#E1-1");
699 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2");
702 TypeDescriptor.RemoveProvider (providers[0], instance);
703 TypeDescriptor.RemoveProvider (providers[1], instance);
704 TypeDescriptor.RemoveProvider (providers[2], instance);
705 TypeDescriptor.RemoveProvider (providers[3], instance);
710 public void TestAddProvider_Provider_Instance_4 ()
712 var instance = new MyComponent ();
713 var providers = new MyTypeDescriptionProvider[] {
714 new MyTypeDescriptionProvider ("One"),
715 new MyTypeDescriptionProvider ("Two"),
716 new MyTypeDescriptionProvider ("Three"),
717 new MyTypeDescriptionProvider ("Four")
721 TypeDescriptionProvider provider;
722 ICustomTypeDescriptor descriptor;
724 TypeDescriptor.AddProvider (providers[0], instance);
725 provider = TypeDescriptor.GetProvider (instance);
726 Assert.IsNotNull (provider, "#A1");
727 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
728 Assert.IsNotNull (descriptor, "#A1-1");
729 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2");
730 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3");
732 descriptor.GetProperties ();
734 TypeDescriptor.AddProvider (providers[1], instance);
735 provider = TypeDescriptor.GetProvider (instance);
736 Assert.IsNotNull (provider, "#B1");
737 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
738 Assert.IsNotNull (descriptor, "#B1-1");
739 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2");
741 // Providers are stored in a stack according to docs, but it's in reality
742 // a FIFO linked list
743 TypeDescriptor.AddProvider (providers[0], instance);
744 TypeDescriptor.AddProvider (providers[0], instance);
745 provider = TypeDescriptor.GetProvider (instance);
746 Assert.IsNotNull (provider, "#C1");
747 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
748 Assert.IsNotNull (descriptor, "#C1-1");
749 Assert.AreEqual ("One", descriptor.GetClassName (), "#C1-2");
751 TypeDescriptor.RemoveProvider (providers[0], instance);
752 provider = TypeDescriptor.GetProvider (instance);
753 Assert.IsNotNull (provider, "#D1");
754 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
755 Assert.IsNotNull (descriptor, "#D1-1");
756 Assert.AreEqual ("One", descriptor.GetClassName (), "#D1-2");
758 TypeDescriptor.RemoveProvider (providers[0], instance);
759 provider = TypeDescriptor.GetProvider (instance);
760 Assert.IsNotNull (provider, "#E1");
761 descriptor = provider.GetTypeDescriptor (instance.GetType (), instance);
762 Assert.IsNotNull (descriptor, "#E1-1");
763 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2");
766 TypeDescriptor.RemoveProvider (providers[0], instance);
767 TypeDescriptor.RemoveProvider (providers[1], instance);
768 TypeDescriptor.RemoveProvider (providers[2], instance);
769 TypeDescriptor.RemoveProvider (providers[3], instance);
774 [ExpectedException (typeof (ArgumentNullException))]
775 public void TestAddProvider_Provider_Type_1 ()
777 TypeDescriptor.AddProvider (null, (Type) null);
781 [ExpectedException (typeof (ArgumentNullException))]
782 public void TestAddProvider_Provider_Type_2 ()
784 var provider = new MyTypeDescriptionProvider ();
785 TypeDescriptor.AddProvider (provider, (Type) null);
789 public void TestAddProvider_Provider_Type_3 ()
791 var type = typeof (MyComponent);
792 var providers = new MyTypeDescriptionProvider[] {
793 new MyTypeDescriptionProvider ("One"),
794 new MyTypeDescriptionProvider ("Two"),
795 new MyTypeDescriptionProvider ("Three"),
796 new MyTypeDescriptionProvider ("Four")
800 TypeDescriptionProvider provider;
801 ICustomTypeDescriptor descriptor;
803 TypeDescriptor.AddProvider (providers[0], type);
804 provider = TypeDescriptor.GetProvider (type);
805 Assert.IsNotNull (provider, "#A1");
806 descriptor = provider.GetTypeDescriptor (type);
807 Assert.IsNotNull (descriptor, "#A1-1");
808 Assert.AreEqual ("One", descriptor.GetClassName (), "#A1-2");
809 Assert.AreEqual (false, providers[0].CreateInstanceCalled, "#A1-3");
811 TypeDescriptor.AddProvider (providers[1], type);
812 provider = TypeDescriptor.GetProvider (type);
813 Assert.IsNotNull (provider, "#B1");
814 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
815 Assert.IsNotNull (descriptor, "#B1-1");
816 Assert.AreEqual ("Two", descriptor.GetClassName (), "#B1-2");
818 // Providers are stored in a stack according to docs, but it's in reality
819 // a FIFO linked list
820 TypeDescriptor.AddProvider (providers[2], type);
821 TypeDescriptor.AddProvider (providers[3], type);
822 provider = TypeDescriptor.GetProvider (type);
823 Assert.IsNotNull (provider, "#C1");
824 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
825 Assert.IsNotNull (descriptor, "#C1-1");
826 Assert.AreEqual ("Four", descriptor.GetClassName (), "#C1-2");
828 TypeDescriptor.RemoveProvider (providers[2], type);
829 provider = TypeDescriptor.GetProvider (type);
830 Assert.IsNotNull (provider, "#D1");
831 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
832 Assert.IsNotNull (descriptor, "#D1-1");
833 Assert.AreEqual ("Four", descriptor.GetClassName (), "#D1-2");
835 TypeDescriptor.RemoveProvider (providers[3], type);
836 provider = TypeDescriptor.GetProvider (type);
837 Assert.IsNotNull (provider, "#E1");
838 descriptor = provider.GetTypeDescriptor (type.GetType (), type);
839 Assert.IsNotNull (descriptor, "#E1-1");
840 Assert.AreEqual ("Two", descriptor.GetClassName (), "#E1-2");
843 TypeDescriptor.RemoveProvider (providers[0], type);
844 TypeDescriptor.RemoveProvider (providers[1], type);
845 TypeDescriptor.RemoveProvider (providers[2], type);
846 TypeDescriptor.RemoveProvider (providers[3], type);
851 [ExpectedException (typeof (ArgumentNullException))]
852 public void TestGetProvider_Type_1 ()
854 TypeDescriptor.GetProvider ((Type)null);
858 public void TestGetProvider_Type_2 ()
860 TypeDescriptionProvider provider = TypeDescriptor.GetProvider (typeof (string));
861 Assert.IsNotNull (provider, "#A1");
862 provider = new MyTypeDescriptionProvider ("One");
865 TypeDescriptor.AddProvider (provider, typeof (string));
866 ICustomTypeDescriptor descriptor = provider.GetTypeDescriptor (typeof (string));
867 Assert.IsNotNull (descriptor, "#B1");
868 Assert.AreEqual ("One", descriptor.GetClassName (), "#B1-1");
870 TypeDescriptor.RemoveProvider (provider, typeof (string));
875 [ExpectedException (typeof (ArgumentNullException))]
876 public void TestGetProvider_Instance_1 ()
878 TypeDescriptor.GetProvider ((object) null);
882 public void TestGetProvider_Instance_2 ()
884 var instance = new MyComponent ();
885 TypeDescriptionProvider provider = TypeDescriptor.GetProvider (instance);
886 Assert.IsNotNull (provider, "#A1");
887 provider = new MyTypeDescriptionProvider ("One");
890 TypeDescriptor.AddProvider (provider, instance);
891 ICustomTypeDescriptor descriptor = provider.GetTypeDescriptor (instance);
892 Assert.IsNotNull (descriptor, "#B1");
893 Assert.AreEqual ("One", descriptor.GetClassName (), "#B1-1");
895 TypeDescriptor.RemoveProvider (provider, instance);
900 [ExpectedException (typeof (ArgumentNullException))]
901 public void TestRemoveProvider_Provider_Type_1 ()
903 TypeDescriptor.RemoveProvider (null, (Type)null);
907 [ExpectedException (typeof (ArgumentNullException))]
908 public void TestRemoveProvider_Provider_Type_2 ()
910 var provider = new MyTypeDescriptionProvider ();
911 TypeDescriptor.RemoveProvider (provider, null);
915 public void TestRemoveProvider_Provider_Type_3 ()
917 var provider = new MyTypeDescriptionProvider ();
918 bool refreshedCalled = false;
919 bool refreshedCorrectComponentChanged = false;
920 bool refreshedCorrectTypeChanged = false;
922 RefreshEventHandler handler = (RefreshEventArgs args) => {
923 refreshedCalled = true;
924 refreshedCorrectComponentChanged = args.ComponentChanged == null;
925 refreshedCorrectTypeChanged = args.TypeChanged == typeof (string);
929 TypeDescriptor.Refreshed += handler;
931 TypeDescriptor.RemoveProvider (provider, typeof (string));
932 Assert.AreEqual (true, refreshedCalled, "#A1");
933 Assert.AreEqual (true, refreshedCorrectComponentChanged, "#A2");
934 Assert.AreEqual (true, refreshedCorrectTypeChanged, "#A3");
936 TypeDescriptor.Refreshed -= handler;
941 [ExpectedException (typeof (ArgumentNullException))]
942 public void TestRemoveProvider_Provider_Instance_1 ()
944 TypeDescriptor.RemoveProvider (null, (object)null);
948 [ExpectedException (typeof (ArgumentNullException))]
949 public void TestRemoveProvider_Provider_Instance_2 ()
951 var provider = new MyTypeDescriptionProvider ();
952 TypeDescriptor.RemoveProvider (provider, (object)null);
956 public void TestRemoveProvider_Provider_Instance_3 ()
958 var instance = new MyComponent ();
959 var provider = new MyTypeDescriptionProvider ();
960 bool refreshedCalled = false;
961 bool refreshedCorrectComponentChanged = false;
962 bool refreshedCorrectTypeChanged = false;
964 RefreshEventHandler handler = (RefreshEventArgs args) => {
965 refreshedCalled = true;
966 refreshedCorrectComponentChanged = args.ComponentChanged == instance;
967 refreshedCorrectTypeChanged = args.TypeChanged == typeof (MyComponent);
971 TypeDescriptor.Refreshed += handler;
973 TypeDescriptor.RemoveProvider (provider, instance);
974 Assert.AreEqual (true, refreshedCalled, "#A1");
975 Assert.AreEqual (true, refreshedCorrectComponentChanged, "#A2");
976 Assert.AreEqual (true, refreshedCorrectTypeChanged, "#A3");
978 TypeDescriptor.Refreshed -= handler;
983 [ExpectedException (typeof (ArgumentNullException))]
984 public void TestGetReflectionType_Type_1 ()
986 TypeDescriptor.GetReflectionType ((Type) null);
990 public void TestGetReflectionType_Type_2 ()
992 Type type = TypeDescriptor.GetReflectionType (typeof (string));
993 Assert.IsNotNull (type, "#A1");
994 Assert.AreEqual (typeof (string), type, "#A1-1");
996 type = TypeDescriptor.GetReflectionType (typeof (MyComponent));
997 Assert.IsNotNull (type, "#B1");
998 Assert.AreEqual (typeof (MyComponent), type, "#B1-1");
1000 type = TypeDescriptor.GetReflectionType (typeof (List<string>));
1001 Assert.IsNotNull (type, "#C1");
1002 Assert.AreEqual (typeof (List <string>), type, "#C1-1");
1004 type = TypeDescriptor.GetReflectionType (typeof (IList<>));
1005 Assert.IsNotNull (type, "#D1");
1006 Assert.AreEqual (typeof (IList<>), type, "#D1-1");
1008 type = TypeDescriptor.GetReflectionType (typeof (IDictionary<,>));
1009 Assert.IsNotNull (type, "#E1");
1010 Assert.AreEqual (typeof (IDictionary<,>), type, "#E1-1");
1014 [ExpectedException (typeof (ArgumentNullException))]
1015 public void TestGetReflectionType_Instance_1 ()
1017 TypeDescriptor.GetReflectionType ((object) null);
1021 public void TestGetReflectionType_Instance_2 ()
1023 string s = "string";
1024 Type type = TypeDescriptor.GetReflectionType (s);
1025 Assert.IsNotNull (type, "#A1");
1026 Assert.AreEqual (typeof (string), type, "#A1-1");
1028 var mc = new MyComponent ();
1029 type = TypeDescriptor.GetReflectionType (mc);
1030 Assert.IsNotNull (type, "#B1");
1031 Assert.AreEqual (typeof (MyComponent), type, "#B1-1");
1033 var l = new List<string> ();
1034 type = TypeDescriptor.GetReflectionType (l);
1035 Assert.IsNotNull (type, "#C1");
1036 Assert.AreEqual (typeof (List<string>), type, "#C1-1");
1038 IList il = new List<string> ();
1039 type = TypeDescriptor.GetReflectionType (il);
1040 Assert.IsNotNull (type, "#D1");
1041 Assert.AreEqual (typeof (List<string>), type, "#D1-1");
1043 IDictionary id = new Dictionary<string, object> ();
1044 type = TypeDescriptor.GetReflectionType (id);
1045 Assert.IsNotNull (type, "#E1");
1046 Assert.AreEqual (typeof (Dictionary<string,object>), type, "#E1-1");
1049 type = TypeDescriptor.GetReflectionType (o);
1050 Assert.IsNotNull (type, "#F1");
1051 Assert.AreEqual (typeof (int), type, "#F1-1");
1055 public void TestICustomTypeDescriptor ()
1057 TestCustomTypeDescriptor test = new TestCustomTypeDescriptor ();
1059 PropertyDescriptorCollection props;
1060 PropertyDescriptor prop;
1061 EventDescriptorCollection events;
1063 test.ResetMethodsCalled ();
1064 props = TypeDescriptor.GetProperties (test);
1065 Assert.AreEqual ("5", test.methods_called, "#1");
1067 test.ResetMethodsCalled ();
1068 props = TypeDescriptor.GetProperties (test, new Attribute[0]);
1069 Assert.AreEqual ("4", test.methods_called, "#2");
1071 test.ResetMethodsCalled ();
1072 props = TypeDescriptor.GetProperties (test, new Attribute[0], false);
1073 Assert.AreEqual ("4", test.methods_called, "#3");
1075 test.ResetMethodsCalled ();
1076 props = TypeDescriptor.GetProperties (test, false);
1077 Assert.AreEqual ("5", test.methods_called, "#4");
1079 test.ResetMethodsCalled ();
1080 prop = TypeDescriptor.GetDefaultProperty (test);
1081 Assert.AreEqual ("6", test.methods_called, "#5");
1083 test.ResetMethodsCalled ();
1084 events = TypeDescriptor.GetEvents (test);
1085 Assert.AreEqual ("2", test.methods_called, "#6");
1087 test.ResetMethodsCalled ();
1088 events = TypeDescriptor.GetEvents (test, new Attribute[0]);
1089 Assert.AreEqual ("1", test.methods_called, "#7");
1091 test.ResetMethodsCalled ();
1092 events = TypeDescriptor.GetEvents (test, false);
1093 Assert.AreEqual ("2", test.methods_called, "#8");
1097 public void TestCreateDesigner ()
1099 IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
1100 Assert.IsTrue (des is MyDesigner, "#1");
1102 des = TypeDescriptor.CreateDesigner (com, typeof(string));
1103 Assert.IsNull (des, "#2");
1107 public void TestCreateEvent ()
1109 EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
1110 Assert.AreEqual (typeof (MyComponent), ed.ComponentType, "#1");
1111 Assert.AreEqual (typeof (EventHandler), ed.EventType, "#2");
1112 Assert.IsTrue (ed.IsMulticast, "#3");
1113 Assert.AreEqual ("AnEvent", ed.Name, "#4");
1117 public void TestCreateProperty ()
1119 PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
1120 Assert.AreEqual (typeof (MyComponent), pd.ComponentType, "#1");
1121 Assert.AreEqual ("TestProperty", pd.Name, "#2");
1122 Assert.AreEqual (typeof (string), pd.PropertyType, "#3");
1123 Assert.IsFalse (pd.IsReadOnly, "#4");
1125 pd.SetValue (com, "hi");
1126 Assert.AreEqual ("hi", pd.GetValue (com), "#5");
1130 public void TestGetAttributes ()
1132 AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
1133 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#A1");
1134 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#A2");
1135 Assert.IsNull (col [typeof (EditorAttribute)], "#A3");
1137 col = TypeDescriptor.GetAttributes (com);
1138 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#B1");
1139 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#B2");
1140 Assert.IsNull (col [typeof (EditorAttribute)], "#B3");
1142 col = TypeDescriptor.GetAttributes (sitedcom);
1143 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#C1");
1144 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#C2");
1145 Assert.IsNotNull (col [typeof (EditorAttribute)], "#C3");
1147 col = TypeDescriptor.GetAttributes (nfscom);
1148 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#D1");
1149 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#D2");
1150 Assert.IsNull (col [typeof (EditorAttribute)], "#D3");
1152 col = TypeDescriptor.GetAttributes (typeof (MyDerivedComponent));
1153 Assert.IsNotNull (col [typeof (DesignerAttribute)], "#E1");
1154 Assert.IsNotNull (col [typeof (DescriptionAttribute)], "#E2");
1155 DesignerAttribute attribute = col[typeof(DesignerAttribute)] as DesignerAttribute;
1156 Assert.IsNotNull (attribute, "#E3");
1157 // there are multiple DesignerAttribute present and their order in the collection isn't deterministic
1159 for (int i = 0; i < col.Count; i++) {
1160 attribute = (col [i] as DesignerAttribute);
1161 if (attribute != null) {
1162 found = typeof(MyOtherDesigner).AssemblyQualifiedName == attribute.DesignerTypeName;
1167 Assert.IsTrue (found, "#E4");
1169 // Shows that attributes are retrieved from the current type, the base types
1170 // and the implemented by the type interfaces.
1171 Assert.AreEqual (3, TypeDescriptor.GetAttributes (typeof (TestDerivedClass)).Count, "#F1");
1175 public void TestGetClassName ()
1177 Assert.AreEqual (typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
1181 public void TestGetComponentName ()
1183 // in MS.NET 2.0, GetComponentName no longer returns
1184 // the type name if there's no custom typedescriptor
1186 Assert.IsNull (TypeDescriptor.GetComponentName (com), "#1");
1187 Assert.IsNull (TypeDescriptor.GetComponentName (com, false), "#2");
1188 Assert.IsNull (TypeDescriptor.GetComponentName (new Exception ()), "#3");
1189 Assert.IsNull (TypeDescriptor.GetComponentName (new Exception (), false), "#4");
1190 Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception)), "#4");
1191 Assert.IsNull (TypeDescriptor.GetComponentName (typeof (Exception), false), "#6");
1192 Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#7");
1193 Assert.AreEqual ("TestName", TypeDescriptor.GetComponentName (sitedcom), "#8");
1197 [ExpectedException (typeof (ArgumentNullException))]
1198 public void TestGetConverterNullParam ()
1200 TypeDescriptor.GetConverter (null);
1204 public void TestGetConverter ()
1206 Assert.AreEqual (typeof (BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType (), "#1");
1207 Assert.AreEqual (typeof (ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType (), "#2");
1208 Assert.AreEqual (typeof (SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType (), "#3");
1209 Assert.AreEqual (typeof (StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType (), "#4");
1210 Assert.AreEqual (typeof (CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType (), "#5");
1211 Assert.AreEqual (typeof (Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType (), "#6");
1212 Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType (), "#7");
1213 Assert.AreEqual (typeof (Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType (), "#8");
1214 Assert.AreEqual (typeof (UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType (), "#9");
1215 Assert.AreEqual (typeof (UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType (), "#10");
1216 Assert.AreEqual (typeof (UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType (), "#11");
1217 Assert.AreEqual (typeof (SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType (), "#12");
1218 Assert.AreEqual (typeof (DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType (), "#13");
1219 Assert.AreEqual (typeof (DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType (), "#14");
1220 Assert.AreEqual (typeof (ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType (), "#15");
1221 Assert.AreEqual (typeof (CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType (), "#16");
1222 Assert.AreEqual (typeof (DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType (), "#17");
1223 Assert.AreEqual (typeof (GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType (), "#18");
1224 Assert.AreEqual (typeof (TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType (), "#19");
1225 Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType (), "#20");
1227 // Tests from bug #71444
1228 Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType (), "#21");
1229 Assert.AreEqual (typeof (ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType (), "#22");
1230 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType (), "#23");
1231 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType (), "#24");
1233 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType (), "#25");
1234 Assert.AreEqual (typeof (TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType (), "#26");
1235 Assert.AreEqual (typeof (CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType (), "#27");
1237 // Test from bug #76686
1238 Assert.AreEqual (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType (), "#28");
1240 Assert.IsFalse (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29");
1241 Assert.IsFalse (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30");
1243 Assert.IsTrue (TypeDescriptor.GetConverter (typeof (Component)) is ComponentConverter, "#29");
1244 Assert.IsTrue (TypeDescriptor.GetConverter (new Component()) is ComponentConverter, "#30");
1246 Assert.AreEqual (typeof (NullableConverter), TypeDescriptor.GetConverter (typeof (int?)).GetType (), "#31");
1250 public void TestGetDefaultEvent ()
1252 EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
1253 Assert.IsNull ( des, "#A");
1255 des = TypeDescriptor.GetDefaultEvent (com);
1256 Assert.IsNull (des, "#B");
1258 des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
1259 Assert.IsNotNull (des, "#C1");
1260 Assert.AreEqual ("AnotherEvent", des.Name, "#C2");
1262 des = TypeDescriptor.GetDefaultEvent (anothercom);
1263 Assert.IsNotNull (des, "#D1");
1264 Assert.AreEqual ("AnotherEvent", des.Name, "#D2");
1266 des = TypeDescriptor.GetDefaultEvent (sitedcom);
1267 Assert.IsNull (des, "#E1");
1269 des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
1270 Assert.IsNotNull (des, "#F1");
1271 Assert.AreEqual ("AnEvent", des.Name, "#F2");
1273 des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
1274 Assert.IsNotNull (des, "#G1");
1275 Assert.AreEqual ("AnEvent", des.Name, "#G2");
1279 public void TestGetDefaultProperty ()
1281 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
1282 Assert.IsNull (des, "#A");
1284 des = TypeDescriptor.GetDefaultProperty (com);
1285 Assert.IsNull (des, "#B");
1287 des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
1288 Assert.IsNotNull (des, "#C1");
1289 Assert.AreEqual ("AnotherProperty", des.Name, "#C2");
1291 des = TypeDescriptor.GetDefaultProperty (anothercom);
1292 Assert.IsNotNull (des, "#D1");
1293 Assert.AreEqual ("AnotherProperty", des.Name, "#D2");
1297 public void TestGetDefaultProperty2 ()
1299 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
1300 Assert.IsNull (des, "#A");
1302 des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
1303 Assert.IsNotNull (des, "#B1");
1304 Assert.AreEqual ("TestProperty", des.Name, "#B2");
1306 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
1307 Assert.IsNotNull (des, "#C1");
1308 Assert.AreEqual ("TestProperty", des.Name, "#C2");
1310 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
1311 Assert.IsNull (des, "#D");
1315 public void TestGetEvents ()
1317 EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
1318 Assert.AreEqual (3, col.Count, "#A1");
1319 Assert.IsNotNull (col.Find ("AnEvent", true), "#A2");
1320 Assert.IsNotNull (col.Find ("AnotherEvent", true), "#A3");
1321 Assert.IsNotNull (col.Find ("Disposed", true), "#A4");
1323 col = TypeDescriptor.GetEvents (com);
1324 Assert.AreEqual (3, col.Count, "#B1");
1325 Assert.IsNotNull (col.Find ("AnEvent", true), "#B2");
1326 Assert.IsNotNull (col.Find ("AnotherEvent", true), "#B3");
1327 Assert.IsNotNull (col.Find ("Disposed", true), "#B4");
1329 col = TypeDescriptor.GetEvents (sitedcom);
1330 Assert.AreEqual (2, col.Count, "#C1");
1331 Assert.IsNotNull (col.Find ("AnotherEvent", true), "#C2");
1332 Assert.IsNotNull (col.Find ("Disposed", true), "#C3");
1334 col = TypeDescriptor.GetEvents (nfscom);
1335 Assert.AreEqual (3, col.Count, "#D1");
1336 Assert.IsNotNull (col.Find ("AnEvent", true), "#D2");
1337 Assert.IsNotNull ( col.Find ("AnotherEvent", true), "#D3");
1338 Assert.IsNotNull (col.Find ("Disposed", true), "#D4");
1340 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
1342 col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
1343 Assert.AreEqual (1, col.Count, "#E1");
1344 Assert.IsNotNull (col.Find ("AnEvent", true), "#E2");
1346 col = TypeDescriptor.GetEvents (com, filter);
1347 Assert.AreEqual (1, col.Count, "#F1");
1348 Assert.IsNotNull (col.Find ("AnEvent", true), "#F2");
1350 col = TypeDescriptor.GetEvents (sitedcom, filter);
1351 Assert.AreEqual (0, col.Count, "#G");
1353 col = TypeDescriptor.GetEvents (nfscom, filter);
1354 Assert.AreEqual (1, col.Count, "#H1");
1355 Assert.IsNotNull (col.Find ("AnEvent", true), "#H2");
1357 col = TypeDescriptor.GetEvents (typeof (IFoo));
1358 Assert.AreEqual (2, col.Count, "#I1");
1359 Assert.IsNotNull (col.Find ("Fired", true), "#I2");
1360 Assert.IsNotNull (col.Find ("Fired", false), "#I3");
1361 Assert.IsNotNull (col.Find ("fired", true), "#I4");
1362 Assert.IsNull (col.Find ("fired", false), "#I5");
1363 Assert.IsNotNull (col.Find ("Closed", true), "#I6");
1365 col = TypeDescriptor.GetEvents (typeof (IBar));
1366 Assert.AreEqual (1, col.Count, "#J1");
1367 Assert.IsNull (col.Find ("Fired", true), "#J2");
1368 Assert.IsNull (col.Find ("Closed", true), "#J3");
1369 Assert.IsNotNull (col.Find ("Destroyed", true), "#J4");
1373 public void TestGetProperties ()
1375 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
1376 Assert.IsNotNull (col.Find ("TestProperty", true), "#A1");
1377 Assert.IsNotNull ( col.Find ("AnotherProperty", true), "#A2");
1379 col = TypeDescriptor.GetProperties (com);
1380 Assert.IsNotNull (col.Find ("TestProperty", true), "#B1");
1381 Assert.IsNotNull (col.Find ("AnotherProperty", true), "#B2");
1383 col = TypeDescriptor.GetProperties (nfscom);
1384 Assert.IsNotNull (col.Find ("TestProperty", true), "#C1");
1385 Assert.IsNotNull (col.Find ("AnotherProperty", true), "#C2");
1387 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
1389 col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
1390 Assert.IsNotNull (col.Find ("TestProperty", true), "#D1");
1391 Assert.IsNull (col.Find ("AnotherProperty", true), "#D2");
1393 col = TypeDescriptor.GetProperties (com, filter);
1394 Assert.IsNotNull (col.Find ("TestProperty", true), "#E1");
1395 Assert.IsNull (col.Find ("AnotherProperty", true), "#E2");
1397 col = TypeDescriptor.GetProperties (nfscom, filter);
1398 Assert.IsNotNull (col.Find ("TestProperty", true), "#F1");
1399 Assert.IsNull (col.Find ("AnotherProperty", true), "#F2");
1402 // GetProperties should return only the last type's implementation of a
1403 // property with a matching name in the base types. E.g in the case where
1404 // the "new" keyword is used.
1406 PropertyDescriptorCollection derivedCol = TypeDescriptor.GetProperties (typeof(MyDerivedComponent));
1407 Assert.IsNotNull (derivedCol["AnotherProperty"].Attributes[typeof (DescriptionAttribute)], "#G1");
1408 int anotherPropsFound = 0;
1409 int yetAnotherPropsFound = 0;
1410 foreach (PropertyDescriptor props in derivedCol) {
1411 if (props.Name == "AnotherProperty")
1412 anotherPropsFound++;
1413 else if (props.Name == "YetAnotherProperty")
1414 yetAnotherPropsFound++;
1417 // GetProperties does not return the base type property in the case
1418 // where both the "new" keyword is used and also the Property type is different
1419 // (Type.GetProperties does return both properties)
1421 Assert.AreEqual (1, anotherPropsFound, "#G2");
1422 Assert.IsNotNull (derivedCol["AnotherProperty"].Attributes[typeof (DescriptionAttribute)], "#G3");
1423 Assert.AreEqual (1, yetAnotherPropsFound, "#G4");
1425 // Verify that we return the derived "new" property when
1426 // filters are applied that match the parent property
1428 PropertyDescriptorCollection filteredCol = TypeDescriptor.GetProperties (typeof(MyDerivedComponent),
1429 new Attribute[] { BrowsableAttribute.Yes });
1430 Assert.IsNotNull (filteredCol["YetAnotherProperty"], "#G5");
1432 // GetProperties does not return write-only properties (ones without a getter)
1434 Assert.IsNull (col.Find ("WriteOnlyProperty", true), "#H1");
1436 col = TypeDescriptor.GetProperties (typeof (IFoo));
1437 Assert.AreEqual (1, col.Count, "#I1");
1438 Assert.IsNotNull (col.Find ("HasFired", true), "#I1");
1439 Assert.IsNotNull (col.Find ("HasFired", false), "#I2");
1440 Assert.IsNotNull (col.Find ("hasFired", true), "#I3");
1441 Assert.IsNull (col.Find ("hasFired", false), "#I4");
1443 col = TypeDescriptor.GetProperties (typeof (IBar));
1444 Assert.AreEqual (1, col.Count, "#J1");
1445 Assert.IsNull (col.Find ("HasFired", true), "#J2");
1446 Assert.IsNotNull (col.Find ("IsDestroyed", true), "#J3");
1447 Assert.IsNotNull (col.Find ("IsDestroyed", false), "#J4");
1448 Assert.IsNotNull (col.Find ("isDestroyed", true), "#J5");
1449 Assert.IsNull (col.Find ("isDestroyed", false), "#J6");
1453 public void TestGetProperties2 ()
1455 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
1456 Assert.IsNull (col.Find ("TestProperty", true), "#A1");
1457 Assert.IsNotNull (col.Find ("AnotherProperty", true), "#A2");
1459 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
1460 col = TypeDescriptor.GetProperties (sitedcom, filter);
1461 Assert.IsNull (col.Find ("TestProperty", true), "#B1");
1462 Assert.IsNull (col.Find ("AnotherProperty", true), "#B2");
1466 public void GetProperties_Order ()
1468 MyComponent com = new MyComponent (new MyContainer ());
1470 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (com);
1471 Assert.AreEqual (8, col.Count, "#1");
1472 Assert.AreEqual ("TestProperty", col [0].Name, "#2");
1473 Assert.AreEqual ("AnotherProperty", col [1].Name, "#3");
1474 Assert.AreEqual ("YetAnotherProperty", col [2].Name, "#4");
1475 Assert.AreEqual ("Name", col [3].Name, "#5");
1476 Assert.AreEqual ("Address", col [4].Name, "#6");
1477 Assert.AreEqual ("Country", col [5].Name, "#7");
1478 Assert.AreEqual ("Site", col [6].Name, "#8");
1479 Assert.AreEqual ("Container", col [7].Name, "#9");
1482 [TypeConverter (typeof (TestConverter))]
1483 class TestConverterClass {
1486 class TestConverter : TypeConverter {
1489 public TestConverter (Type type)
1496 public void TestConverterCtorWithArgument ()
1498 TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass));
1499 Assert.IsNotNull (t.GetType (), "#A1");
1500 Assert.AreEqual (typeof (TestConverter), t.GetType (), "#A2");
1501 TestConverter converter = (TestConverter) t;
1502 Assert.AreEqual (typeof (TestConverterClass), converter.Type, "#B");
1506 public void GetPropertiesIgnoreIndexers ()
1508 PropertyDescriptorCollection pc =
1509 TypeDescriptor.GetProperties (typeof (string));
1510 // There are two string properties: Length and Chars.
1511 // Chars is an indexer.
1513 // Future version of CLI might contain some additional
1514 // properties. In that case simply increase the
1515 // number. (Also, it is fine to just remove #2.)
1516 Assert.AreEqual (1, pc.Count, "#1");
1517 Assert.AreEqual ("Length", pc [0].Name, "#2");
1522 public void InterfaceType ()
1524 Type interface_type = TypeDescriptor.InterfaceType;
1525 Assert.AreEqual ("TypeDescriptorInterface", interface_type.Name, "#A0");
1526 Assert.IsTrue (interface_type.IsClass, "#A1");
1527 Assert.IsTrue (interface_type.IsSealed, "#A2");
1528 Assert.AreEqual (typeof (object), interface_type.BaseType, "#A3");
1529 Assert.IsFalse (interface_type.IsInterface, "#A4");
1530 Assert.IsFalse (interface_type.IsPublic, "#A5");