在c#中将string []转换为Decimal [],int [],float [] .double []

Mal*_*olm 3 c# silverlight

string[] txt1 = new string[]{"12","13"};

this.SetValue(txt1, v => Convert.ChangeType(v, typeof(decimal[]), null));
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误 - 对象必须实现IConvertible.

我还想要一个代码来转换string [] To Decimal [],int [],float [] .double []

the*_*oop 12

您无法将字符串[]直接转换为十进制[],所有元素都必须单独转换为新类型.相反,您可以使用Array.ConvertAll

string[] txt1 = new string[]{"12","13"};
decimal[] dec1 = Array.ConvertAll<string, decimal>(txt1, Convert.ToDecimal);
Run Code Online (Sandbox Code Playgroud)

并且类似地使用Convert.ToInt32,Convert.ToSingle,Convert.ToDoubleConverter<TInput,TOutput>参数,以产生INT [],浮[],双[],在正确的类型参数的ConvertAll代

编辑:当你使用没有ConvertAll的silverlight时,你必须手动完成:

decimal[] dec1 = new decimal[txt1.Length];
for (int i=0; i<txt1.Length; i++) {
    dec1[i] = Convert.ToDecimal(txt1[i]);
}
Run Code Online (Sandbox Code Playgroud)