TypeConverterが必要です。ほとんどのプリミティブ型にはデフォルトのものがあります。これらはControlBuilderによって使用されます。 .NETはすでにSystem.Typeのために何かを持っているかもしれませんが、何もわかりません。だから、ここにあなたがあなた自身を追加することができるかです:
ページで:
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" MyProperty="System.Int32" />
ユーザーコントロールのコードが背後にある:
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;
}
}
ありがとう@Ruslan。私はTypeConverterを含む何かを疑っていましたが、どのようにか何かを知りませんでした。これは素晴らしい! – ProfK