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;
} 2 1998 er***********@gmail.com wrote: 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.
This behaviour of the .NET runtime, where T and S are types and S is a
subtype of T, where S[] can be cast to T[], seems to have been
introduced solely for compatibility with the Java language. It's
important to point out that it involves polymorphism. That is, S[]
stored in a variable of type T[] is *still*, at runtime, of type S[].
This becomes apparent when a third type, U, also derived from T, is
stored at runtime into an S[] which is being referred, polymorphically,
through a value of type T[]. (It causes a runtime exception.)
Basically, every store and load to and from an array of a reference type
is actually a virtual method call which uses dynamic dispatch based on
the runtime type of the array value.
For example:
---8<---
class App
{
static void Main()
{
string[] foo = { "a", "b" };
object[] bar = foo;
bar[0] = new App(); // Throws exception at runtime,
// because the object referred to by bar is not an object[]
// but is in fact a string[], which can't store App instances.
// That's why you can't cast an object[] value to string[],
// even if it only contains strings. It has to be a string[]
// value to begin with, when it was constructed.
}
}
--->8---
Why?
Your code tries to cast an object[] to a testClass[], but an object[] is
*not* a testClass[], even if all it contains is values
assignment-compatible with variables of type testClass[].
-- Barry
-- http://barrkel.blogspot.com/
> Your code tries to cast an object[] to a testClass[], but an object[] is *not* a testClass[], even if all it contains is values assignment-compatible with variables of type testClass[].
Thanks for the insight, Barry.
Looks to me like the way out is through Array.CreateInstance, where I
can generate the correct type of array at runtime (which is
fundamentally what I need to do), then drag the whole thing around as
an object reference. Had to read your answer through several times
before I shifted my thinking in the right direction.
-Eric This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ghostdog |
last post by:
hi,
i got this opengl/c++ code:
<code>
void render(CMesh *mesh){
...
float *pVertices;
int *pIndices;
|
by: Dominique Léger |
last post by:
Hello guys,
I'm kinda new to C, and I'm having a hard time with strings. What I'm trying
to do is a simple function that trims spaces & tabs at the beginning of a
given string. For example, I...
|
by: yufufi |
last post by:
lets say we have a 'shape' class which doesn't implement IComparable
interface..
compiler doesn't give you error for the lines below..
shape b= new shape();
IComparable h;
h=(IComparable)b;...
|
by: Barry Mossman |
last post by:
Hi,
I get the feeling that I am missing something with regards to casting.
The CopyTo method allows me to copy the contents of a collection into an
array. My collection is a MatchCollection...
|
by: Carlo Marchesoni |
last post by:
I have an ASP.NET/C# solution, where I can perfectly cast something I stored
in the session object to a class of mine (BackEnd), as this:
->be = (BackEnd)Session;<-
But if I try to do the same:...
|
by: Ken Allen |
last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years.
One of the drawbacks with VB6 and earlier was the difficulty in casting a
'record' to a different 'shape' so one could...
|
by: Enrique Bustamante |
last post by:
Casting arrays that works on watch and command window but not in code.
My application is casting arrays in a way it should work. To test if I
was doing something invalid, I wrote a test code that...
|
by: Trev |
last post by:
Hi,
I have a series of functions which do the following:
ValidateData( args ); //args if of type ArrayList
// Work out if the data is valid or not, and work out the type of
args - int, string,...
|
by: Tinkertim |
last post by:
Hi,
I have often wondered if casting the return value of malloc() (or
friends) actually helps anything, recent threads here suggest that it
does not .. so I hope to find out.
For instance :
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |