Repository navigation
JA CSharp Keyword unmanaged function pointer calling convention
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
delegate* タイプの unmanaged は、アンマネージド呼び出し規則を指定します。リストがない場合は、プラットフォーム契約が使用されます。 Cdecl は角括弧内で明示的に指定できます。 UnmanagedCallersOnly を含む静的 C# メソッドは、通常のマネージド呼び出しではなく、互換性のあるアンマネージド ポインターを介して呼び出されます。プロジェクトでは安全でないブロックが許可されています。
この例を Example.cs として別に保存し、このページの他の例とは独立して実行してください。
Example.cs:
// PROJECT-EXAMPLE
unsafe
{
delegate* unmanaged[Cdecl]<int, int> twice = &Tools.Twice;
System.Console.WriteLine(twice(4));
}
static class Tools { [System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] public static int Twice(int value) => value * 2; }プロジェクトで <AllowUnsafeBlocks>true</AllowUnsafeBlocks> を有効にしてください。ポインターは示されたスコープ内でのみ有効です。
結果:
8
delegate* unmanaged<int, int> operation = &Transform;
delegate* unmanaged[Cdecl]<int, int> callback = &TransformCdecl;ファイルExample.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>ファイルProgram.cs:
using System;
using System.Runtime.InteropServices;
internal static unsafe class Program
{
public static void Main()
{
delegate* unmanaged<int, int> doubleValue = &DoubleValue;
Console.WriteLine(doubleValue(12));
}
[UnmanagedCallersOnly]
private static int DoubleValue(int value) => value * 2;
}結果:
24
ファイルExample.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>ファイルProgram.cs:
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
internal static unsafe class Program
{
public static void Main()
{
delegate* unmanaged[Cdecl]<int, int, int> callback = &Difference;
int result = InvokeCallback(callback, 14, 5);
Console.WriteLine(result);
}
private static int InvokeCallback(delegate* unmanaged[Cdecl]<int, int, int> callback,
int left, int right) => callback(left, right);
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static int Difference(int left, int right) => left - right;
}結果:
9
公式ソース: Microsoft Learn。
ClassicUO — Bad Newbie & C# Scripts IDE