Virtu/Library/GCHandleHelpers.cs
Sean Fausett 365e5723c1 Deleted all svn:eol-style properties on files.
--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4050811
2010-08-28 11:34:14 +00:00

33 lines
806 B
C#

using System;
using System.Runtime.InteropServices;
using System.Security;
namespace Jellyfish.Library
{
public static class GCHandleHelpers
{
[SecurityCritical]
public static void Pin(object value, Action<IntPtr> action)
{
if (action == null)
{
throw new ArgumentNullException("action");
}
var gcHandle = new GCHandle();
try
{
gcHandle = GCHandle.Alloc(value, GCHandleType.Pinned);
action(gcHandle.AddrOfPinnedObject());
}
finally
{
if (gcHandle.IsAllocated)
{
gcHandle.Free();
}
}
}
}
}