KO CSharp Keyword unmanaged function pointer calling convention - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
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 ํ์ต.