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
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