E
eric.dennison
In the sample below:
testClass is derived from object.
We can cast object to testClass, no problem
We can cast testClass[] to object[] no problem
Compiler is ok with cast object[] to testClass[] but fails at runtime.
Why?
I understand this can be worked around using Array.Copy, but I actually
am trying to do the cast from object[] to testClass[] within
Type.InvokeMember and so can't get my hands on the destination
testClass[] member at compile time. Enough to make me head spin.
Heck, I don't even know how to do the cast within InvokeMember, so I'm
two levels away from a solution. Any ideas out there??
-Eric
Sample code:
public class testClass : object
{
public int testMember;
}
// this works great
public void testMethod1()
{
object[] objArray;
testClass[] testArray = new testClass[1];
objArray = testArray;
}
// this fails on the last line
public void testMethod2()
{
// first, we can cast happily from object to testClass
object objSingle = new object();
testClass testSingle;
testClass testTemp = new testClass();
testTemp.testMember = 55;
objSingle = testTemp;
testSingle = (testClass)objSingle;
// testSingle.testMember does indeed == 55
// on the other hand, if we're dealing in arrays...
object[] objArray = new object[1];
testClass[] testArray;
objArray[0] = new testClass();
// **** compiler is ok with this, but fails at runtime
****
testArray = (testClass[])objArray;
}
testClass is derived from object.
We can cast object to testClass, no problem
We can cast testClass[] to object[] no problem
Compiler is ok with cast object[] to testClass[] but fails at runtime.
Why?
I understand this can be worked around using Array.Copy, but I actually
am trying to do the cast from object[] to testClass[] within
Type.InvokeMember and so can't get my hands on the destination
testClass[] member at compile time. Enough to make me head spin.
Heck, I don't even know how to do the cast within InvokeMember, so I'm
two levels away from a solution. Any ideas out there??
-Eric
Sample code:
public class testClass : object
{
public int testMember;
}
// this works great
public void testMethod1()
{
object[] objArray;
testClass[] testArray = new testClass[1];
objArray = testArray;
}
// this fails on the last line
public void testMethod2()
{
// first, we can cast happily from object to testClass
object objSingle = new object();
testClass testSingle;
testClass testTemp = new testClass();
testTemp.testMember = 55;
objSingle = testTemp;
testSingle = (testClass)objSingle;
// testSingle.testMember does indeed == 55
// on the other hand, if we're dealing in arrays...
object[] objArray = new object[1];
testClass[] testArray;
objArray[0] = new testClass();
// **** compiler is ok with this, but fails at runtime
****
testArray = (testClass[])objArray;
}