我有一个int数组作为Web用户控件的属性.如果可能,我想使用以下语法设置该属性内联:
<uc1:mycontrol runat="server" myintarray="1,2,3" />
Run Code Online (Sandbox Code Playgroud)
这将在运行时失败,因为它将期望一个实际的int数组,但正在传递一个字符串.我可以创建myintarray一个字符串并在setter中解析它,但我想知道是否有更优雅的解决方案.
类似的问题:将int list作为参数传递给Web用户控件
是否有类似enum类型的示例?
我正在创建一个asp.net自定义控件,我想在其中将逗号分隔的枚举列表作为属性传递.
我正在写一个TypeConverter用于将逗号分隔的字符串值转换为枚举列表.
在ConvertTo方法中,
如何为InstanceDescriptor枚举列表创建对象?
我目前的代码如下:
//enum
public enum MyEnum {Hello, World}
//main method
List<MyEnum> list = new List<MyEnum>();
list.Add(MyEnum.Hello);
list.Add(MyEnum.World);
ConstructorInfo constructor = typeof(List<MyEnum>).GetConstructor( Type.EmptyTypes );
InstanceDescriptor idesc = new InstanceDescriptor(constructor, list);
Run Code Online (Sandbox Code Playgroud)
这与消息失败
Length mismatch
Run Code Online (Sandbox Code Playgroud)
我想知道为什么
我需要您的属性上的 ASP.Net 控件,您可以指定一个数组类型字符串。也就是说,在我的 ASP.Net 控件中会有类似的内容:
DefaultValue("")> Category("Misc"), <Bindable(True),
Public Overridable Property MyProperty () As String ()
Get
Return mMyProperty
End Get
Set (ByVal value As String ())
mMyProperty = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
从 ASP.Net 页面我需要传递值??,输入:
<WC:MyControl MyProperty="1,2,4" runat="server" />
Run Code Online (Sandbox Code Playgroud)
消息错误是: 无法从“MyProperty”属性的字符串表示形式“1,2,4”创建“System.String[]”类型的对象。