I'm using reflection to read in an xml file and keep coming across an error telling me that I cannot convert a string to a string[] (which I don't want to do!) I think the cause of my problem is I'm unable to tell if the type of the object is an array or not. Below is what I'm currently using (doesn't work right) but I've also tried to use if(mi[i].GetType() == typeof(string[]))
which also doesnt work..
MemberInfo[] mi = objType.GetProperties();
for (int i = 0; i < mi.Length; i++)
{
if (mi[i].GetType().IsArray)
{
}
else
{
//Code path is running through here
}
The file is read in correctly..
EDIT: I thought I'd better add the structure to my objType
to better explain..
objType
is a class that contains a string[] variable that in this case is referred to as mi[i]
You need to use PropertyType
rather than GetType()
on the MemberInfo
to get the underlying type of the property.
var mi = objType.GetProperties();
for (int i = 0; i < mi.Length; i++)
{
var type = mi[i].PropertyType;
//Check for string array
if (type.IsArray && type.GetElementType() == typeof(string))
{
}
}
Or you can do
if(type == typeof(string[]))
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With