gpt4 book ai didi

asp.net - 在 Web 用户控件中将 int 数组作为参数传递

转载 作者:行者123 更新时间:2023-12-03 11:15:52 25 4
gpt4 key购买 nike

我有一个 int 数组作为 Web 用户控件的属性。如果可能,我想使用以下语法内联设置该属性:

<uc1:mycontrol runat="server" myintarray="1,2,3" />

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

最佳答案

实现一个类型转换器,这里是一个,警告:quick&dirty,不用于生产等:

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 数组作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/116797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com