|
6 | 6 | using BTDB.Serialization;
|
7 | 7 | using Microsoft.AspNetCore.Http;
|
8 | 8 |
|
9 |
| -var builder = new ContainerBuilder(); |
10 |
| -//builder.RegisterType<Klass>(); |
11 |
| -builder.AutoRegisterTypes().AsSelf(); |
12 |
| -var container = builder.Build(); |
13 |
| -container.Resolve<Klass>(); |
14 | 9 | unsafe
|
15 | 10 | {
|
16 |
| - var h = IAnyHandler.CreateConsumeDispatcher(container); |
17 |
| - h(container, "Hello"); |
18 |
| -} |
| 11 | + var builder = new ContainerBuilder(); |
| 12 | +//builder.RegisterType<Klass>(); |
| 13 | + builder.AutoRegisterTypes().AsSelf(); |
| 14 | + var container = builder.Build(); |
| 15 | + container.Resolve<Klass>(); |
| 16 | + unsafe |
| 17 | + { |
| 18 | + var h = IAnyHandler.CreateConsumeDispatcher(container); |
| 19 | + h(container, "Hello"); |
| 20 | + } |
19 | 21 |
|
20 |
| -unsafe |
21 |
| -{ |
22 |
| - ReflectionMetadata.RegisterCollection(new() |
| 22 | + unsafe |
23 | 23 | {
|
24 |
| - Type = typeof(Dictionary<int, string>), |
25 |
| - ElementKeyType = typeof(int), |
26 |
| - ElementValueType = typeof(string), |
27 |
| - Creator = &Create1, |
28 |
| - AdderKeyValue = &Add1 |
29 |
| - }); |
30 |
| -} |
| 24 | + ReflectionMetadata.RegisterCollection(new() |
| 25 | + { |
| 26 | + Type = typeof(Dictionary<int, string>), |
| 27 | + ElementKeyType = typeof(int), |
| 28 | + ElementValueType = typeof(string), |
| 29 | + Creator = &Create1, |
| 30 | + AdderKeyValue = &Add1 |
| 31 | + }); |
| 32 | + } |
31 | 33 |
|
32 |
| -static object Create1(uint capacity) |
33 |
| -{ |
34 |
| - return new Dictionary<int, string>((int)capacity); |
| 34 | + MyCtx myCtx = default; |
| 35 | + AllocateValueType(ref Unsafe.As<MyCtx, byte>(ref myCtx), ref myCtx.Ptr, &WorkWithAllocatedValueType); |
| 36 | + |
| 37 | + static object Create1(uint capacity) |
| 38 | + { |
| 39 | + return new Dictionary<int, string>((int)capacity); |
| 40 | + } |
| 41 | + |
| 42 | + static void Add1(object c, ref byte key, ref byte value) |
| 43 | + { |
| 44 | + Unsafe.As<Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value)); |
| 45 | + } |
| 46 | + |
| 47 | + static void AllocateValueType(ref byte ctx, ref nint ptr, delegate*<ref byte, void> chain) |
| 48 | + { |
| 49 | + (int, string) value; |
| 50 | +#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type |
| 51 | + ptr = (nint)(&value); |
| 52 | +#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type |
| 53 | + chain(ref ctx); |
| 54 | + ptr = 0; |
| 55 | + } |
| 56 | + |
| 57 | + static void WorkWithAllocatedValueType(ref byte ctx) |
| 58 | + { |
| 59 | +#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type |
| 60 | + ref var value = ref *((int, string)*)Unsafe.As<byte, MyCtx>(ref ctx).Ptr; |
| 61 | +#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type |
| 62 | + value = (42, "Hello"); |
| 63 | + Console.WriteLine(value); |
| 64 | + } |
35 | 65 | }
|
36 | 66 |
|
37 |
| -static void Add1(object c, ref byte key, ref byte value) |
| 67 | +public struct MyCtx |
38 | 68 | {
|
39 |
| - Unsafe.As<Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value)); |
| 69 | + public nint Ptr; |
40 | 70 | }
|
41 | 71 |
|
42 | 72 | [Generate]
|
|
0 commit comments