473,410 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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 4827
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: stenknz | last post by:
Does any one know where there is a good introduction to Discussion Forum scripting using PHP and MySQL. (Apache WS) I just need the nuts and bolts to get an overview of whats required. Regards,...
21
by: Rune Steffensen | last post by:
I've been lurking c.l.p for a while now, and find the amount of messages a bit annoying. To solve this, I suggest the creation of the new group "comp.lang.pyhon.discussions". So when people post...
1
by: Fuzzyman | last post by:
There is a `web design` group over on google-groups. http://groups-beta.google.com/group/wd It's brief is for ``Discussion of web design (html, php, flash, wysiwig, cgi, perl, python, css,...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
0
by: Harry Smith | last post by:
This was posted in news.groups, but it was not posted to the list. REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.postgresql.admin unmoderated group...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
3
by: Duncan Smith | last post by:
Hello, Since moving to numpy I've had a few problems with my existing code. It basically revolves around the numpy scalar types. e.g. ------------------------------------------------ array(,...
10
by: jacob navia | last post by:
There is a very interesting thread in comp.lang.c++ about garbage collection. It is very instructive to compare the level of the discussion there with the discussion we just had here in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.