Assigning and Using Anonymous Arrays
I have an
object
that at runtime can be an array of unknown
Type
. I have a function that will convert an array to a
string
generic if I know its
Type
. But I don't always know the
Type
coming in. Here is a code snippet:
object val = getter.Invoke(obj, null);
string sval;
if (val == null)
sval = "[Null]";
else
{
Type t = val.GetType();
if (t.IsArray == true)
{
object[] array = val as object[];
sval = ArrayToStringGeneric(array, ", ");
}
else
sval = val.ToString();
}
When I try to assign
val
to array like above array come up
null
and my call to
ArrayToStringGeneric
fails. If I change the line:
object[] array = val as object[];
to:
int[] array = val as int[];
The assignment works and array has values that work, but I will not always know the
Type
. Is there a way to make this work for all unknown
Type
s at runtime?
Thanks,
E