RuntimeHelpers.GetObjectValue Discussion

G

Guest

Hi all,

I was hoping to start a dialog about this shared method which is used by the
VB.NET compiler. From what I understand, it's very similar to boxing, except
that it will always create a new box on the heap for an existing box. I can't
really understand the need for this. My thought is that it's to avoid the
situation where 2 objects point to the same value type (boxed) and then
accidentally modifying fields in one reflects in the other. However, I don't
think that this accidental behavior is possible to achieve. Therefore, I'm
not really sure what the advantage of this is.

See:

http://msdn.microsoft.com/chats/transcripts/vstudio/vstudio_072903.aspx

"Host: Cameron (Microsoft)
Q: Why does the compiler emit a call to
System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue whenever you
pass an Object reference to an Object parameter?

A: The reason is because we do not want valuetype aliasing to occur. For
example, assigning between Objects would copy only the _reference_ to a boxed
valuetype. Modifying what the user thinks is a unique instance would in fact
modify both copies, which we thought would be surprising and bad. Using
reflection and CallByName isn't impossible because you can use
System.ValueType. Assigning or passing System.ValueType's will NOT generate
calls to GetObjectValue. In other words, you can force aliasing by using
System.ValueType instead of Object to manipulate your valuetypes."

So "valuetype aliasing" seems to be when another valuetype points to the
same box if I understand correctly. Is this what others have gathered? Also,
if anyone is able to use system.valuetype to achieve some sort of differing
behavior, I'd like to hear about it as I had difficulty.

Thanks...

-Ben
 
H

Herfried K. Wagner [MVP]

Ben R. said:
I was hoping to start a dialog about this shared method which is used by
the
VB.NET compiler. From what I understand, it's very similar to boxing,
except
that it will always create a new box on the heap for an existing box. I
can't
really understand the need for this. My thought is that it's to avoid the
situation where 2 objects point to the same value type (boxed) and then
accidentally modifying fields in one reflects in the other. However, I
don't
think that this accidental behavior is possible to achieve. Therefore, I'm
not really sure what the advantage of this is.

See:

http://msdn.microsoft.com/chats/transcripts/vstudio/vstudio_072903.aspx

"Host: Cameron (Microsoft)
Q: Why does the compiler emit a call to
System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue whenever you
pass an Object reference to an Object parameter?

A: The reason is because we do not want valuetype aliasing to occur. For
example, assigning between Objects would copy only the _reference_ to a
boxed
valuetype. Modifying what the user thinks is a unique instance would in
fact
modify both copies, which we thought would be surprising and bad. Using
reflection and CallByName isn't impossible because you can use
System.ValueType. Assigning or passing System.ValueType's will NOT
generate
calls to GetObjectValue. In other words, you can force aliasing by using
System.ValueType instead of Object to manipulate your valuetypes."

So "valuetype aliasing" seems to be when another valuetype points to the
same box if I understand correctly. Is this what others have gathered?
Also,
if anyone is able to use system.valuetype to achieve some sort of
differing
behavior, I'd like to hear about it as I had difficulty.

Imagine you have to set a structure's field's value using reflection. If
you are passing the structure directly to the 'SetValue' method, only a copy
would be passed. The same occurs when boxing the value into an 'Object'
prior to passing it to the method. Instead you can use the trick shown
below:

\\\
Dim o as <structure type>
Dim x As ValueType = o
GetType(...).GetField(...).SetValue(x, <value>)
o = CType(x, <structure type>)
///
 
C

Cor Ligthert [MVP]

Ben,

What is wrong with boxing, there seems to be a C hype that it is wrong,
nobody could tell me what is wrong with it. (beside some two or three bytes
plus some time which is really not measurable and which is not even a
fraction from the time used by in my eyes more obscure methods as
reflection).

Cor
 
W

Walter Wang [MSFT]

Hi Ben,

You can use following code to test:

Public Structure MyStr
Public a As String
End Structure

Module Module1
Sub Main()
Dim s As New MyStr
Dim boxed As Object = s
Dim t As Type = boxed.GetType()
t.GetField("a").SetValue(boxed, "aa")
s = CType(boxed, MyStr)
Console.WriteLine(s.a) '<==== in C# you get "aa" in the output,
nothing in VB

Dim boxedVT As ValueType = s
t = boxedVT.GetType()
t.GetField("a").SetValue(boxedVT, "aa")
s = CType(boxedVT, MyStr)
Console.WriteLine(s.a) ' <==== this will print "aa", since
using ValueType explictly bypass the GetObjectValue()
End Sub
End Module

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Cor,

The only things I've heard are the things you mentioned (mainly the overhead
of extra allocation / dereferencing). I think the performance hit comes when
boxing / unboxing happens repeatedly in a loop.

-Ben
 
C

Cor Ligthert [MVP]

Ben,

There were some C# guys who were telling all the time that boxing was bad.
Than I made a test and asked them to do that. They are not telling it
anyore, you need billions of loops with unboxing to make it visible that it
takes time.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top