/// <summary>Converts the given value object to the reference type using the specified context and arguments.</summary> /// <returns>The converted object.</returns> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param> /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo" /> that specifies the culture used to represent the font. </param> /// <param name="value">The <see cref="T:System.Object" /> to convert. </param> /// <param name="destinationType">The type to convert the object to. </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="destinationType" /> is null. </exception> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType != typeof(string)) { return(base.ConvertTo(context, culture, value, destinationType)); } if (value == null) { return("(none)"); } string text = string.Empty; if (context != null) { System.ComponentModel.Design.IReferenceService referenceService = context.GetService(typeof(System.ComponentModel.Design.IReferenceService)) as System.ComponentModel.Design.IReferenceService; if (referenceService != null) { text = referenceService.GetName(value); } if ((text == null || text.Length == 0) && value is IComponent) { IComponent component = (IComponent)value; if (component.Site != null && component.Site.Name != null) { text = component.Site.Name; } } } return(text); }
/// <summary>Gets a collection of standard values for the reference data type.</summary> /// <returns>A <see cref="T:System.ComponentModel.TypeConverter.StandardValuesCollection" /> that holds a standard set of valid values, or null if the data type does not support a standard set of values.</returns> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param> public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { ArrayList arrayList = new ArrayList(); if (context != null) { System.ComponentModel.Design.IReferenceService referenceService = context.GetService(typeof(System.ComponentModel.Design.IReferenceService)) as System.ComponentModel.Design.IReferenceService; if (referenceService != null) { foreach (object value in referenceService.GetReferences(this.reference_type)) { if (this.IsValueAllowed(context, value)) { arrayList.Add(value); } } } else if (context.Container != null && context.Container.Components != null) { foreach (object obj in context.Container.Components) { if (obj != null && this.IsValueAllowed(context, obj) && this.reference_type.IsInstanceOfType(obj)) { arrayList.Add(obj); } } } arrayList.Add(null); } return(new TypeConverter.StandardValuesCollection(arrayList)); }
/// <summary>Converts the given object to the reference type.</summary> /// <returns>An <see cref="T:System.Object" /> that represents the converted <paramref name="value" />.</returns> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param> /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo" /> that specifies the culture used to represent the font. </param> /// <param name="value">The <see cref="T:System.Object" /> to convert. </param> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (!(value is string)) { return(base.ConvertFrom(context, culture, value)); } if (context != null) { object obj = null; System.ComponentModel.Design.IReferenceService referenceService = context.GetService(typeof(System.ComponentModel.Design.IReferenceService)) as System.ComponentModel.Design.IReferenceService; if (referenceService != null) { obj = referenceService.GetReference((string)value); } if (obj == null && context.Container != null && context.Container.Components != null) { obj = context.Container.Components[(string)value]; } return(obj); } return(null); }