This would be pretty straight forward if I knew the types at compile time or if it was a generic parameter, because I could do something like myArray.Cast<T>() But what I actually have is essentially this. I do not have a known type or generic parameter. I have a System.Type variable.
// could actually be anything else
Type myType = typeof(string);
// i already know all the elements are the correct types
object[] myArray = new object[] { "foo", "bar" };
Is there some kind of reflection magic I can do to get a string[] reference containing the same data? (where string isn’t known at compile time)
It’s not really a cast as such (I’m allocating a new array and copying the original), but maybe this can help you out?
In this code,
destinationArraywill be an instance ofstring[](or an array of whatever typemyTypewas).