Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static T GetStruct<T>(byte[] structure)
- {
- int num = Marshal.SizeOf(typeof(T));
- using (DisposableGCHandle disposableGCHandle = DisposableGCHandle.Alloc(structure, GCHandleType.Pinned))
- {
- IntPtr intPtr = IntPtr.Add(disposableGCHandle.AddrOfPinnedObject(), 0);
- return (T)Marshal.PtrToStructure(intPtr, typeof(T));
- }
- }
- public sealed class DisposableGCHandle : IDisposable
- {
- private GCHandle handle;
- public bool IsAllocated
- {
- get
- {
- return this.handle.IsAllocated;
- }
- }
- public object Target
- {
- get
- {
- return this.handle.Target;
- }
- }
- public DisposableGCHandle()
- {
- }
- public IntPtr AddrOfPinnedObject()
- {
- return this.handle.AddrOfPinnedObject();
- }
- public static DisposableGCHandle Alloc(object target, GCHandleType handleType = GCHandleType.Normal)
- {
- if (target == null)
- {
- throw new ArgumentNullException("target");
- }
- DisposableGCHandle disposableGCHandle = new DisposableGCHandle()
- {
- handle = GCHandle.Alloc(target, handleType)
- };
- return disposableGCHandle;
- }
- public void Dispose()
- {
- if (this.IsAllocated)
- {
- this.Free();
- }
- }
- public void Free()
- {
- this.handle.Free();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement