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

ConvertTo() public method

Converts the given value object to the reference type using the specified context and arguments.

public ConvertTo ( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType ) : object
context ITypeDescriptorContext
culture System.Globalization.CultureInfo
value object
destinationType System.Type
return object
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            if (destinationType == typeof(string))
            {
                if (value != null)
                {
                    // Try the reference service first.
                    //
                    if (context != null)
                    {
                        IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService));
                        if (refSvc != null)
                        {
                            string name = refSvc.GetName(value);
                            if (name != null)
                            {
                                return name;
                            }
                        }
                    }

                    // Now see if this is an IComponent.
                    //
                    if (!Marshal.IsComObject(value) && value is IComponent)
                    {
                        IComponent comp = (IComponent)value;
                        ISite site = comp.Site;
                        if (site != null)
                        {
                            string name = site.Name;
                            if (name != null)
                            {
                                return name;
                            }
                        }
                    }

                    // Couldn't find it.
                    return String.Empty;
                }
                return s_none;
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

Usage Example

Example #1
0
		public void ConvertTo ()
		{
			ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
			string referenceName = "reference name";

			Assert.AreEqual ("(none)", (string)converter.ConvertTo (null, null, null, typeof(string)), "#1");

			TestComponent component = new TestComponent();

			// no context
			Assert.AreEqual (String.Empty, (string)converter.ConvertTo (null, null, component, typeof(string)), "#2");

			// context with IReferenceService
			TestReferenceService referenceService = new TestReferenceService ();
			referenceService.AddReference (referenceName, component);
			TestTypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);
			Assert.AreEqual (referenceName, (string)converter.ConvertTo (context, null, component, typeof(string)), "#3");
			
			// context with Component without IReferenceService
			Container container = new Container ();
			container.Add (component, referenceName);
			context = new TestTypeDescriptorContext ();
			context.Container = container;
			Assert.AreEqual (referenceName, (string)converter.ConvertTo (context, null, component, typeof(string)), "#4");
		}