The problem I'm having is trying to read the the string array from the object array that is produced from the FileHelpers class.
This is my File Helpers class code:
[DelimitedRecord(",")]
public class test
{
[FieldDelimiter(":")]
public string header;
public string[] phStrArray;
}
Then I try getting the fields from the object
string line="pie,cherry,berry:,cake,carrot,vanilla";
FileHelperEngine engine= new FileHelperEngine(className);
var test =engine.ReadString(line);
Object myObject = test[0];
FieldInfo[] fields = className.GetFields();
var objTempValue = fields[1].GetValue(myObject);
tempValue += Convert.ToString(objTempValue);
And that will work great for the regular string variable, but when it comes to the string array it will just return "System.String[]". Overall I'm just not sure how to read that string array into a string or make it an actual string array I can actually grab data from. I have a feeling I'm over thinking on how to get data from it.
Thanks for the help in advance.
Edit: Forgive me, I should of wrote that a little clearer(That's what I get for using var). The problem is the variable objTempValue turns into a type of Object{string[]}. So it's like the object is holding a string array and there doesn't seem to be a way to get to it. That's the variable I need to convert to string or get data from.
Update: Another way to do this is to cast it to a string array like so.
string[] arr = ((IEnumerable<object>)objTempValue).Cast<object>()
.Select(x => x.ToString())
.ToArray();
I'm not entirely sure what option is better, but both this and the answer seem to work quiet well.
You can use String.Join
in order to convert a string array into a single string:
string line = String.Join(",", phStrArray);
The first parameter is the separator you want to use.
Note: There reverse operation would be:
string[] arr = line.Split(',');
EDIT: If your variable is statically typed as object
, then you must cast it to its runtime type:
object objTempValue = new string[] { "pie", "cherry", "berry" };
string line = String.Join(",", (string[])objTempValue);
// line ==> "pie,cherry,berry"
In C# you cast to another type by placing the type in parentheses before the term:
object obj = 5; // System.Int32
int i = (int)obj;
Of course the type must be compatible to the actual value stored in the variable. In the case of a value type, assigning it to an object
variable involves a boxing, and casting back involves an unboxing. See Boxing and Unboxing (C# Programming Guide) and Casting and Type Conversions (C# Programming Guide)