|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Boxed Value TypesI read this article : http://msdn.microsoft.com/security/securecode/bestpractices/default.aspx?pull=/library/en-us/dnnetsec/html/seccodeguide.asp and there is a section named Boxed Value Types. I checked an example from this article and I noticed that described problem exists only in Framework 1.1. class bug { public object m_Property; public Object Property { get { return m_Property; } } public static void m1(ref int j) { j = Int32.MaxValue; } public static void m2(ref ArrayList j) { j = new ArrayList(); } } public static void Main(String [] args) { { bug b = new bug(); b.m_Property = 4; Object[] objArr = new Object[] { b.Property }; Object[] objArr = new Object[] { b.Property }; typeof(bug).GetMethod("m1").Invoke(null, objArr); Console.WriteLine(b.m_Property); Console.WriteLine(objArr[0]); } { bug b = new bug(); ArrayList al = new ArrayList(); al.Add("element"); b.m_Property = al; Object[] objArr = new Object[] { b.Property }; Console.WriteLine( ((ArrayList)(b.m_Property)).Count); typeof(bug).GetMethod("m2").Invoke(null, objArr); Console.WriteLine( ((ArrayList)(b.m_Property)).Count); Console.WriteLine( ((ArrayList)(objArr[0])).Count); } Output from .NET 1.1: 4 2147483647 2147483647 1 1 0 Output form .NET 2.0 4 4 <- !! 2147483647 1 1 0 What is the difference in boxing between vesion 1.1 and 2.0 of Framework? Darek <Da***@discussions.microsoft.com> wrote:
> What has been changed in version 2.0 ? <snip>> > I read this article : > http://msdn.microsoft.com/security/securecode/bestpractices/ > default.aspx?pull=/library/en-us/dnnetsec/html/seccodeguide.asp > > and there is a section named Boxed Value Types. I checked an example from > this article and I noticed that described problem exists only in Framework > 1.1. The code you've written doesn't quite compile. However, it's an interesting problem. Here's a smaller short but complete example which demonstrates the problem (getting rid of the ArrayList stuff which isn't needed): using System; class Bug { public static void SetRefParameter(ref int j) { j = int.MaxValue; } public static void Main(String [] args) { { object o = 4; object[] parameters = new Object[] { o }; typeof(Bug).GetMethod("SetRefParameter") .Invoke(null, parameters); Console.WriteLine(o); Console.WriteLine(parameters[0]); } } } Under 1.1, that produces 2147483647 2147483647 Under 2.0, it produces 4 2147483647 What's happening is that the boxed value is being treated as a reference parameter, and under 1.1, when the new value is being assigned, it's changing the boxed value rather than creating a new boxed value. This will only happen (I believe) when using reflection, but it is a potentially tricky to track down change. I've posted a bug report (because if nothing else, it should be listed in the breaking changes page). You can follow it at http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx? feedbackId=FDBK42857 -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
|||||||||||||||||||||||