Vous avez besoin d'un TypeConverter. La plupart des types primitifs ont ceux par défaut. Ils sont utilisés par ControlBuilders. .NET peut déjà avoir quelque chose pour le System.Type, mais je n'en connais aucun. Alors, voici comment vous pouvez ajouter votre propre:
À la page:
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" MyProperty="System.Int32" />
code de contrôle de l'utilisateur derrière:
using System.ComponentModel;
public partial class WebUserControl1 : System.Web.UI.UserControl
{
[TypeConverter(typeof(MyTypeConverter))]
public System.Type MyProperty { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//dummy user web control, simply outputs property's value
Response.Write(MyProperty.ToString());
}
}
public class MyTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string str = ((string)value).Trim();
try
{
return Type.GetType(str);
}
catch (FormatException exception)
{
throw new FormatException(string.Format("ConvertInvalidPrimitive={0} is not a valid value for {1}.", value, "System.Type"), exception);
}
}
return base.ConvertFrom(context, culture, value);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return false;
}
}
Merci @Ruslan. Je soupçonnais quelque chose impliquant un TypeConverter, mais je n'avais aucune idée de comment ou quoi. C'est bien! – ProfK