string[] txt1 = new string[]{"12","13"};
this.SetValue(txt1, v => Convert.ChangeType(v, typeof(decimal[]), null));
它抛出一个错误 – 对象必须实现IConvertible.
我还想要一个代码来转换string [] To Decimal [],int [],float [] .double []
解决方法:
您无法将字符串[]直接转换为十进制[],所有元素都必须单独转换为新类型.相反,你可以使用Array.ConvertAll
string[] txt1 = new string[]{"12","13"};
decimal[] dec1 = Array.ConvertAll<string, decimal>(txt1, Convert.ToDecimal);
类似地使用Convert.ToInt32,Convert.Toingle,Convert.ToDouble作为转换器< TInput,TOutput>生成int [],float [],double []的参数,用ConvertAll的正确类型参数替换
编辑:当你使用没有ConvertAll的silverlight时,你必须手动完成:
decimal[] dec1 = new decimal[txt1.Length];
for (int i=0; i<txt1.Length; i++) {
dec1[i] = Convert.ToDecimal(txt1[i]);
}