728x90
728x170
▶ 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
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] 특정 인터페이스를 구현한 모든 타입 구하기 (0) | 2020.02.28 |
---|---|
[C#/COMMON] Domain 클래스 : GetComputerDomain 정적 메소드를 사용해 컴퓨터 도메인 구하기 (0) | 2020.02.28 |
[C#/COMMON] Regex 클래스 : 파일명 와일드 카드 사용하기 (0) | 2020.02.28 |
[C#/COMMON] SecureString 클래스 : 보안 문자열 구하기 (0) | 2020.02.28 |
[C#/COMMON] 제네릭 확장 메소드를 사용해 값 비교하기 (0) | 2020.02.28 |
[C#/COMMON] TypedReference 구조체 : __refvalue 키워드를 사용해 객체 참조 구하기 (0) | 2020.02.28 |
[C#/COMMON] TypedReference 구조체 : __makeref 키워드를 사용해 객체 주소 구하기 (0) | 2020.02.28 |
[C#/COMMON] Microsoft Visual Studio Installer 설치 프로그램 만들기 (0) | 2020.02.24 |
[C#/COMMON] DataTable 클래스 : 데이터 테이블에서 객체 리스트 구하기 (0) | 2020.02.23 |
[C#/COMMON] DataRow 클래스 : 데이터 행에서 객체 생성하기 (0) | 2020.02.23 |