asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net – 在web用户控件中传递int数组作为参数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个int数组作为Web用户控件的属性。如果可能,我想使用以下语法内联设置该属性:
<uc1:mycontrol runat="server" myintarray="1,2,3" />

这将在运行时失败,因为它会期待一个实际的int数组,而是传递一个字符串。我可以使myintarray一个字符串,并在setter中解析,但我想知道是否有一个更优雅的解决方案。

解决方法

实施类型转换器,这里是一个,警告:快速和脏,不用于生产使用等:
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; }
}

大佬总结

以上是大佬教程为你收集整理的asp.net – 在web用户控件中传递int数组作为参数全部内容,希望文章能够帮你解决asp.net – 在web用户控件中传递int数组作为参数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。