传递int数组作为参数,web用户控件 [英] Passing int array as parameter in web user control
本文介绍了传递int数组作为参数,web用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个int数组作为Web用户控件的属性。我想设置该属性,内联,如果可以使用的语法如下:
I have an int array as a property of a Web User Control. I'd like to set that property inline if possible using the following syntax:
<uc1:mycontrol runat="server" myintarray="1,2,3" />
这将在运行时失败,因为它会期待一个实际的int数组,而是一个字串被代替通过。我可以让 myintarray
字符串,并以二传手解析它,但我想知道是否有一个更好的解决方案。
This will fail at runtime because it will be expecting an actual int array, but a string is being passed instead. I can make myintarray
a string and parse it in the setter, but I was wondering if there was a more elegant solution.
推荐答案
实现一个类型转换器,这里是一,警告:快速和放大器;脏,不用于生产用途,等等:
Implement a type converter, here is one, warning : quick&dirty, not for production use, etc :
public class IntArrayConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
string val = value as string;
string[] vals = val.Split(',');
System.Collections.Generic.List<int> ints = new System.Collections.Generic.List<int>();
foreach (string s in vals)
ints.Add(Convert.ToInt32(s));
return ints.ToArray();
}
}
和标签控件的属性:
private int[] ints;
[TypeConverter(typeof(IntsConverter))]
public int[] Ints
{
get { return this.ints; }
set { this.ints = value; }
}
这篇关于传递int数组作为参数,web用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文