첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ SomeClass.cs

namespace TestProject
{
    /// <summary>
    /// 샘플 클래스
    /// </summary>
    public class SampleClass
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 값 - Value

        /// <summary>
        /// 값
        /// </summary>
        public int Value { get; set; }

        #endregion
    }
}

 

728x90

 

▶ ReferenceHelper.cs

using System;
using System.Reflection.Emit;

namespace TestProject
{
    /// <summary>
    /// 참조 헬퍼
    /// </summary>
    public static class ReferenceHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 고정 포인터 구하기 액션
        /// </summary>
        public static readonly Action<object, Action<IntPtr>> GetPinnedPointer;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Static

        #region 생성자 - ReferenceHelper()

        /// <summary>
        /// 생성자
        /// </summary>
        static ReferenceHelper()
        {
            DynamicMethod dynamicMethod = new DynamicMethod
            (
                "GetPinnedPointer",
                typeof(void),
                new[] { typeof(object), typeof(Action<IntPtr>) },
                typeof(ReferenceHelper).Module
            );

            ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

            ilGenerator.DeclareLocal(typeof(object), true);

            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Stloc_0);
            ilGenerator.Emit(OpCodes.Ldarg_1);
            ilGenerator.Emit(OpCodes.Ldloc_0);
            ilGenerator.Emit(OpCodes.Conv_I );
            ilGenerator.Emit(OpCodes.Call, typeof(Action<IntPtr>).GetMethod("Invoke"));
            ilGenerator.Emit(OpCodes.Ret    );

            GetPinnedPointer = (Action<object, Action<IntPtr>>)dynamicMethod.CreateDelegate(typeof(Action<object, Action<IntPtr>>));
        }

        #endregion
    }
}

 

300x250

 

▶ Program.cs

using System;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static unsafe void Main()
        {
            SampleClass source = new SampleClass();

            ReferenceHelper.GetPinnedPointer
            (
                source,
                pointer =>
                {
                    Console.WriteLine(Marshal.ReadIntPtr(pointer));
                    Console.WriteLine(typeof(SampleClass).TypeHandle.Value);
                }
            );
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요