Hi Paul!
> When I compile the following code with the C++ compiler version
> 14.00.50215.44 (Microsoft Visual Studio 2005 beta 2) using the command line
> 'cl -W4 -clr:oldSyntax test.cpp' at run time I get a Null Reference Exception
> due to the dynamic_cast. With the C++ compiler version 13.10.3077 from Visual
> Studio 2003 using the command line 'cl -W4 -clr test.cpp' no such error is
> generated.
It does not matter if you use the command-line or the IDE.
> #using <mscorlib.dll>
>
[quoted text clipped - 41 lines]
>
> Is this a bug?
It seems to be!
The problem is that the VC2003 compiler generated the following IL-code:
<snipped>
obj1 = infoArray1[num1].GetValue(null);
int num1 = (int) obj1;
</snipped>
And the VC2005 B2 compiler /with /oldsyntax) generates:
<snipped>
obj1 = infoArray1[num1].GetValue(null);
int num1 = *(!(obj1 is int) ? 0 : ((int) obj1));
</snipped>
So please report a (detailed!) bug-report at:
http://lab.msdn.microsoft.com/productfeedback/
If you do not want to do this, please let me know, than I can do it for you.

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jochen Kalmbach [MVP] - 10 May 2005 13:07 GMT
>> Is this a bug?
>
> It seems to be!
But it looks like a bug in the VC2003 compiler!
Because "dynamic_cast" should excatly do what the VC2005 compiler is doing:
Check if the object has the correct type (or derived) and then cast it
to this type or return "null".
> The problem is that the VC2003 compiler generated the following IL-code:
>
> <snipped>
> obj1 = infoArray1[num1].GetValue(null);
> int num1 = (int) obj1;
> </snipped>
This is wrong...
> And the VC2005 B2 compiler /with /oldsyntax) generates:
>
> <snipped>
> obj1 = infoArray1[num1].GetValue(null);
> int num1 = *(!(obj1 is int) ? 0 : ((int) obj1));
> </snipped>
This is correct.
But it should be documented in the "breaking-changes"...

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
PaulW@community.nospam - 10 May 2005 13:44 GMT
Hi Jochen,
Thanks for the reply. Although I found the problem first in my own code I
based it on the sample code (and reproduced it in the sample code as well).
The sample code is at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemTypeClassGetFieldsTopic2.asp.
Therefore it looks like this sample code is wrong.
I have submitted a bug report anyway.
Regards,
Paul.
Jochen Kalmbach [MVP] - 10 May 2005 13:49 GMT
Hi Paul!
> I have submitted a bug report anyway.
Can you please post the link, so other people can easily find it...

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
PaulW@community.nospam - 10 May 2005 14:06 GMT
> Can you please post the link, so other people can easily find it...
The link is:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=fa4a6
137-dac8-48a7-9413-a6abd826a899
Regards,
Paul