Web Log di Adrian Florea

"You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
posts - 440, comments - 2715, trackbacks - 3944

My Links

Archives

Post Categories

Image Galleries

.RO Blogs

.RO People

.RO Sites

Blogs

Furls

Links

vinCitori

LOCAL array initializer?

Dopo il post di ieri sulla classe PrivateImplementationDetails e sugli array initializer in C#, ho continuato a giocare un po' e guardate cosa ho combinato...

using System;
using System.Reflection;
using System.Runtime.InteropServices;

class Foo
{
  static void ContainsLocalArrayInitializer()
  {
    int[] a = new int[3]{1, 2, 3};
  }

  static int[] GetLocalArrayInitializerOfSomebodyElse()
  {
    int[] a = new int[3];
    IntPtr buffer = Marshal.AllocHGlobal(12);
    Marshal.StructureToPtr(Assembly.GetExecutingAssembly().GetModules()[0].
      GetType("<PrivateImplementationDetails>").GetField("$$method0x6000001-1", BindingFlags.NonPublic | BindingFlags.Static).
      GetValue(null), buffer, true);
    Marshal.Copy(buffer, a, 0, a.Length);
    Marshal.FreeHGlobal(buffer);
    return a;
  }

  static void Main()
  {
    int[] stolenArray = GetLocalArrayInitializerOfSomebodyElse();
    foreach(int element in stolenArray)
    {
      Console.WriteLine(element);
    }
  }
}

Lo snippet stampa a console gli elementi con cui è stato inizializzato l'array locale di un altro metodo! :-)

1
2
3

Commenti?

Print | posted on domenica 3 aprile 2005 20:27 | Filed Under [ Carillon .NET ]

Powered by:
Powered By Subtext Powered By ASP.NET