Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pCYSl5EDgo/11893fca137a42852d77794eb988b8aa to your computer and use it in GitHub Desktop.
Save pCYSl5EDgo/11893fca137a42852d77794eb988b8aa to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using System.Reflection.Emit;
static class Program
{
static Guid CALC() => Guid.NewGuid();
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveOptimization)]
static void Main(string[] args)
{
var z = new Func<Guid>(CALC).GetFuncPtr();
System.Console.WriteLine(z.ToString());
}
}
public static class StaticDelegateHelper
{
public static IntPtr GetFuncPtr(this Delegate function)
{
DynamicMethod dm = new DynamicMethod("", typeof(IntPtr), Array.Empty<Type>());
ILGenerator ilgen = dm.GetILGenerator();
ilgen.Emit(OpCodes.Ldftn, function.GetMethodInfo());
ilgen.Emit(OpCodes.Ret);
var a = dm.CreateDelegate(typeof(Func<IntPtr>));
var b = a as Func<IntPtr>;
var c = b(); //System.Security.VerificationException
return c;
}
}
/*
PowerShell抜粋
PS C:\Users\HogeHoge\Saved Games\Homo> dotnet build
.NET Core 向け Microsoft (R) Build Engine バージョン 16.0.225-preview+g5ebeba52a1
Copyright (C) Microsoft Corporation.All rights reserved.
C:\Users\HogeHoge\Saved Games\Homo\Homo.csproj の復元が 28.57 ms で完了しました。
C:\Program Files\dotnet\sdk\3.0.100-preview-009812\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(142,5): message NETSDK1057: .NET Core SDK のプレビュー バージョンで作業をしています。現在のプロジェクトの global.json ファイルに
C:\Program Files\dotnet\sdk\3.0.100-preview-009812\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(142,5): message NETSDK1057: .NET Core SDK のプレビュー バージョンで作業をしています。現在のプロジェクトの global.json ファイルに
よって SDK バージョンを定義できます。詳細については、https://go.microsoft.com/fwlink/?linkid=869452 をご覧ください [C:\Users\HogeHoge\Saved Games\Homo\Homo.csproj]
Homo -> C:\Users\HogeHoge\Saved Games\Homo\bin\Debug\netcoreapp3.0\Homo.dll
ビルドに成功しました。
0 個の警告
0 エラー
経過時間 00:00:02.07
PS C:\Users\HogeHoge\Saved Games\Homo> dotnet run
140729405610624
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment