469,117 Members | 1,309 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,117 developers. It's quick & easy.

RuntimeHelpers.GetObjectValue Discussion

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/tran...io_072903.aspx

"Host: Cameron (Microsoft)
Q: Why does the compiler emit a call to
System.Runtime.CompilerServices.RuntimeHelpers.Get ObjectValue 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
Oct 2 '06 #1
6 4384
"Ben R." <be**@newsgroup.nospamschrieb:
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/tran...io_072903.aspx

"Host: Cameron (Microsoft)
Q: Why does the compiler emit a call to
System.Runtime.CompilerServices.RuntimeHelpers.Get ObjectValue 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/>

Oct 3 '06 #2
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." <be**@newsgroup.nospamschreef in bericht
news:9E**********************************@microsof t.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/tran...io_072903.aspx

"Host: Cameron (Microsoft)
Q: Why does the compiler emit a call to
System.Runtime.CompilerServices.RuntimeHelpers.Get ObjectValue 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

Oct 3 '06 #3
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 3 '06 #4
Thanks Walter, this was a great example to show the point.

-Ben

"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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 3 '06 #5
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." <be**@newsgroup.nospamschreef in bericht
news:9E**********************************@microsof t.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/tran...io_072903.aspx

"Host: Cameron (Microsoft)
Q: Why does the compiler emit a call to
System.Runtime.CompilerServices.RuntimeHelpers.Get ObjectValue 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


Oct 3 '06 #6
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

"Ben R." <be**@newsgroup.nospamschreef in bericht
news:6A**********************************@microsof t.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." <be**@newsgroup.nospamschreef in bericht
news:9E**********************************@microso ft.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/tran...io_072903.aspx

"Host: Cameron (Microsoft)
Q: Why does the compiler emit a call to
System.Runtime.CompilerServices.RuntimeHelpers.Get ObjectValue 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



Oct 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by stenknz | last post: by
21 posts views Thread by Rune Steffensen | last post: by
1 post views Thread by Fuzzyman | last post: by
reply views Thread by Harry Smith | last post: by
3 posts views Thread by Duncan Smith | last post: by
10 posts views Thread by jacob navia | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.