public static bool Parse(string str, Type type, ref object obj) { if (type.IsArray) { var reader = new StringStream(str); // iterate over string twice: // first get the count var count = 0; while (reader.HasNext) { string value; count++; if (!reader.NextString(out value)) { // no more terminator break; } } // start again reader.Position = 0; // then fill the array var elemType = type.GetElementType(); var arr = Array.CreateInstance(elemType, count); for (var i = 0; i < count; i++) { object value = null; string strVal; reader.NextString(out strVal); if (ParseSingleValue(strVal, elemType, ref value)) { arr.SetValue(value, i); } else { return(false); } } obj = arr; return(true); } else { return(ParseSingleValue(str, type, ref obj)); } }
public static bool Parse(string str, Type type, ref object obj) { if (!type.IsArray) { return(ParseSingleValue(str, type, ref obj)); } StringStream stringStream = new StringStream(str); int length = 0; while (stringStream.HasNext) { ++length; string result; if (!stringStream.NextString(out result, ",")) { break; } } stringStream.Position = 0; Type elementType = type.GetElementType(); Array instance = Array.CreateInstance(elementType, length); for (int index = 0; index < length; ++index) { object obj1 = null; string result; stringStream.NextString(out result, ","); if (!ParseSingleValue(result, elementType, ref obj1)) { return(false); } instance.SetValue(obj1, index); } obj = instance; return(true); }