Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Utilities.cs
浏览该文件的文档.
1using System.Runtime.InteropServices;
2
3namespace Engine {
4 public static class Utilities {
5 public static void Swap<T>(ref T a, ref T b) {
6 (b, a) = (a, b);
7 }
8
9 public static int SizeOf<T>() where T : unmanaged => Marshal.SizeOf<T>();
10
11 public static unsafe T PtrToStructure<T>(void* ptr) where T : unmanaged => *(T*)ptr;
12
13 public static unsafe T ArrayToStructure<T>(Array array) where T : unmanaged {
14 GCHandle gCHandle = GCHandle.Alloc(array, GCHandleType.Pinned);
15 try {
16 return PtrToStructure<T>(gCHandle.AddrOfPinnedObject().ToPointer());
17 }
18 finally {
19 gCHandle.Free();
20 }
21 }
22
23 public static byte[] StructureToArray<T>(T structure) where T : unmanaged {
24 byte[] array = new byte[SizeOf<T>()];
25 GCHandle gCHandle = GCHandle.Alloc(structure, GCHandleType.Pinned);
26 try {
27 Marshal.Copy(gCHandle.AddrOfPinnedObject(), array, 0, array.Length);
28 return array;
29 }
30 finally {
31 gCHandle.Free();
32 }
33 }
34
35 public static void Dispose<T>(ref T disposable) where T : IDisposable {
36 if (disposable != null) {
37 disposable.Dispose();
38 disposable = default;
39 }
40 }
41
42 public static void DisposeCollection<T>(ICollection<T> disposableCollection) where T : IDisposable {
43 if (disposableCollection != null) {
44 foreach (T item in disposableCollection) {
45 item?.Dispose();
46 }
47 if (!disposableCollection.IsReadOnly) {
48 disposableCollection.Clear();
49 }
50 }
51 }
52 }
53}
unsafe
定义 Main.cs:15
static void Swap< T >(ref T a, ref T b)
static int SizeOf< T >()
static void DisposeCollection< T >(ICollection< T > disposableCollection)
static byte[] StructureToArray< T >(T structure)
static void Dispose< T >(ref T disposable)