If you want to use an actual TypeConverter
, you should be able to do something like this:
public class UserControlConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
Type userControlType = Type.GetType(value.ToString(), true, true);
return Activator.CreateInstance(userControlType);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(UserControl))
{
return destinationType.FullName;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
I haven't had a chance to test this, so please let me know if you have any problems. Also, please note that you will need to use the full name of the type, so something like this: ApplicationName.ProjectOrFolderNameIfApplicable.ControlName
. Please also note that this will just call the default (empty) constructor of the UserControl
.