Home All Groups Group Topic Archive Search About

RuntimeHelpers.GetObjectValue Discussion

Author
2 Oct 2006 10:55 PM
Ben R.
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

Author
3 Oct 2006 12:22 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Ben R." <benr@newsgroup.nospam> schrieb:
> 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>)
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
3 Oct 2006 3:31 AM
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

Show quoteHide quote
"Ben R." <benr@newsgroup.nospam> schreef in bericht
news:9EA5C12C-865A-4CC1-AE79-BC19BF8A17F4@microsoft.com...
> 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
Author
3 Oct 2006 11:08 PM
Ben R.
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

Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> 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
>
> "Ben R." <benr@newsgroup.nospam> schreef in bericht
> news:9EA5C12C-865A-4CC1-AE79-BC19BF8A17F4@microsoft.com...
> > 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
>
>
>
Author
4 Oct 2006 4:14 AM
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

Show quoteHide quote
"Ben R." <benr@newsgroup.nospam> schreef in bericht
news:6AB37B8F-9FDD-479D-8F4D-DABF6DF81E0D@microsoft.com...
> 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
>
> "Cor Ligthert [MVP]" wrote:
>
>> 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
>>
>> "Ben R." <benr@newsgroup.nospam> schreef in bericht
>> news:9EA5C12C-865A-4CC1-AE79-BC19BF8A17F4@microsoft.com...
>> > 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
>>
>>
>>
Author
3 Oct 2006 9:38 AM
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 (waw***@online.microsoft.com, 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.
Author
3 Oct 2006 11:06 PM
Ben R.
Thanks Walter, this was a great example to show the point.

-Ben

Show quoteHide quote
"Walter Wang [MSFT]" wrote:

> 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 (waw***@online.microsoft.com, 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.
>
>