System.ComponentModel.ReferenceConverter.GetStandardValues C# (CSharp) Method

GetStandardValues() public method

Gets a collection of standard values for the reference data type.

public GetStandardValues ( ITypeDescriptorContext context ) : StandardValuesCollection
context ITypeDescriptorContext
return StandardValuesCollection
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            object[] components = null;

            if (context != null)
            {
                ArrayList list = new ArrayList();
                list.Add(null);

                // Try the reference service first.
                //
                IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService));
                if (refSvc != null)
                {
                    object[] objs = refSvc.GetReferences(_type);
                    int count = objs.Length;

                    for (int i = 0; i < count; i++)
                    {
                        if (IsValueAllowed(context, objs[i]))
                            list.Add(objs[i]);
                    }
                }
                else
                {
                    // Now try IContainer.
                    //
                    IContainer cont = context.Container;
                    if (cont != null)
                    {
                        ComponentCollection objs = cont.Components;

                        foreach (IComponent obj in objs)
                        {
                            if (obj != null && _type.IsInstanceOfType(obj) &&
                                IsValueAllowed(context, obj))
                            {
                                list.Add(obj);
                            }
                        }
                    }
                }

                components = list.ToArray();
                Array.Sort(components, 0, components.Length, new ReferenceComparer(this));
            }

            return new StandardValuesCollection(components);
        }

Usage Example

Example #1
0
		public void GetStandardValues ()
		{
			ReferenceConverter converter = new ReferenceConverter (typeof(TestComponent));

			TestComponent component1 = new TestComponent();
			TestComponent component2 = new TestComponent();
			TestReferenceService referenceService = new TestReferenceService ();
			referenceService.AddReference ("reference name 1", component1);
			referenceService.AddReference ("reference name 2", component2);
			ITypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);

			TypeConverter.StandardValuesCollection values = converter.GetStandardValues (context);
			Assert.IsNotNull (values, "#1");
			// 2 components + 1 null value
			Assert.AreEqual (3, values.Count, "#2");
		}