I have a function that is passed an object that may or not be array. If it is an array, I want to iterate through it. I was able to write some code that does this, but it involves first creating an explicitly declared array and copying in the contents of the object. It seems like I should be able to skip this step and iterate through the passed object directly, but I'm not sure how. Any guidance you could provide would be helpful.
Below is the code segment that works, i.e. where I copy to another array first.
if (arg is object[,])
{
object[,] myArray = new object[((object[,])arg).GetLength(0), ((object[,])arg).GetLength(1)];
Array.Copy((object[,])arg, myArray, myArray.Length);
for (int i = 0; i <= myArray.GetUpperBound(1); i++)
{
for (int j = 0; j <= myArray.GetUpperBound(0); j++)
{
total = total + dbConnector.lookup(Int32.Parse(myArray[j, i].ToString()));
}
}

VulpesRank #55 · 29.2K reputationPosted Mar 20, 2012, 6:31 AM EDT
Christopher Yellick10 reputationPosted Mar 20, 2012, 6:43 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 20, 2012, 7:00 AM EDT
This will then defer any compile time checking until runtime.
However, as it will affect both performance and memory (the Dynamic Language Runtime needs to be loaded), it's not something I'd normally recommend.
Christopher Yellick10 reputationPosted Mar 20, 2012, 7:01 AM EDT