| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using System.Reflection; |
| | 6 | |
|
| | 7 | | namespace Ice.Internal; |
| | 8 | |
|
| | 9 | | public sealed class Patcher |
| | 10 | | { |
| | 11 | | public static System.Action<T?> arrayReadValue<T>(T?[] arr, int index) where |
| 1 | 12 | | T : Ice.Value => (T? v) => arr[index] = v; |
| | 13 | |
|
| | 14 | | public static System.Action<T?> listReadValue<T>(List<T?> seq, int index) where T : Ice.Value |
| | 15 | | { |
| 1 | 16 | | return (T? v) => |
| 1 | 17 | | { |
| 1 | 18 | | int count = seq.Count; |
| 1 | 19 | | if (index >= count) // Need to grow the sequence. |
| 1 | 20 | | { |
| 1 | 21 | | for (int i = count; i < index; i++) |
| 1 | 22 | | { |
| 0 | 23 | | seq.Add(null); |
| 1 | 24 | | } |
| 1 | 25 | | seq.Add(v); |
| 1 | 26 | | } |
| 1 | 27 | | else |
| 1 | 28 | | { |
| 0 | 29 | | seq[index] = v; |
| 1 | 30 | | } |
| 0 | 31 | | }; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public static System.Action<T?> customSeqReadValue<T>(IEnumerable<T?> seq, int index) where T : Ice.Value |
| | 35 | | { |
| 1 | 36 | | return (T? v) => |
| 1 | 37 | | { |
| 1 | 38 | | InvokeInfo info = getInvokeInfo<T>(seq.GetType()); |
| 1 | 39 | | int count = info.getCount(seq); |
| 1 | 40 | | if (index >= count) // Need to grow the sequence. |
| 1 | 41 | | { |
| 1 | 42 | | for (int i = count; i < index; i++) |
| 1 | 43 | | { |
| 0 | 44 | | info.invokeAdd(seq, null); |
| 1 | 45 | | } |
| 1 | 46 | | info.invokeAdd(seq, v); |
| 1 | 47 | | } |
| 1 | 48 | | else |
| 1 | 49 | | { |
| 0 | 50 | | info.invokeSet(seq, index, v); |
| 1 | 51 | | } |
| 0 | 52 | | }; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | private static InvokeInfo getInvokeInfo<T>(Type t) |
| | 56 | | { |
| 1 | 57 | | lock (_methodTable) |
| | 58 | | { |
| 1 | 59 | | if (_methodTable.TryGetValue(t, out InvokeInfo? i)) |
| | 60 | | { |
| 1 | 61 | | return i; |
| | 62 | | } |
| | 63 | |
|
| 1 | 64 | | MethodInfo? am = t.GetMethod("Add", [typeof(T)]) ?? |
| 1 | 65 | | throw new Ice.MarshalException("Cannot patch a collection without an Add() method"); |
| 1 | 66 | | PropertyInfo? pi = t.GetProperty("Item") ?? |
| 1 | 67 | | throw new Ice.MarshalException("Cannot patch a collection without an indexer"); |
| 1 | 68 | | MethodInfo? sm = pi.GetSetMethod() ?? |
| 1 | 69 | | throw new Ice.MarshalException("Cannot patch a collection without an indexer to set a value"); |
| 1 | 70 | | pi = t.GetProperty("Count"); |
| 1 | 71 | | if (pi == null) |
| | 72 | | { |
| 0 | 73 | | throw new Ice.MarshalException("Cannot patch a collection without a Count property"); |
| | 74 | | } |
| 1 | 75 | | MethodInfo? cm = pi.GetGetMethod() ?? |
| 1 | 76 | | throw new Ice.MarshalException("Cannot patch a collection without a readable Count property"); |
| 1 | 77 | | i = new InvokeInfo(am, sm, cm); |
| 1 | 78 | | _methodTable.Add(t, i); |
| 1 | 79 | | return i; |
| | 80 | | } |
| 1 | 81 | | } |
| | 82 | |
|
| | 83 | | private class InvokeInfo |
| | 84 | | { |
| 1 | 85 | | public InvokeInfo(MethodInfo am, MethodInfo sm, MethodInfo cm) |
| | 86 | | { |
| 1 | 87 | | _addMethod = am; |
| 1 | 88 | | _setMethod = sm; |
| 1 | 89 | | _countMethod = cm; |
| 1 | 90 | | } |
| | 91 | |
|
| | 92 | | internal int getCount(System.Collections.IEnumerable seq) |
| | 93 | | { |
| | 94 | | try |
| | 95 | | { |
| 1 | 96 | | return (int)_countMethod.Invoke(seq, null)!; |
| | 97 | | } |
| 0 | 98 | | catch (System.Exception ex) |
| | 99 | | { |
| 0 | 100 | | throw new Ice.MarshalException("Could not read Count property during patching", ex); |
| | 101 | | } |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | internal void invokeAdd(System.Collections.IEnumerable seq, object? v) |
| | 105 | | { |
| | 106 | | try |
| | 107 | | { |
| 1 | 108 | | object?[] arg = new object?[] { v }; |
| 1 | 109 | | _addMethod.Invoke(seq, arg); |
| 1 | 110 | | } |
| 0 | 111 | | catch (System.Exception ex) |
| | 112 | | { |
| 0 | 113 | | throw new Ice.MarshalException("Could not invoke Add method during patching", ex); |
| | 114 | | } |
| 1 | 115 | | } |
| | 116 | |
|
| | 117 | | internal void invokeSet(System.Collections.IEnumerable seq, int index, object? v) |
| | 118 | | { |
| | 119 | | try |
| | 120 | | { |
| 0 | 121 | | object?[] args = new object?[] { index, v }; |
| 0 | 122 | | _setMethod.Invoke(seq, args); |
| 0 | 123 | | } |
| 0 | 124 | | catch (System.Exception ex) |
| | 125 | | { |
| 0 | 126 | | throw new Ice.MarshalException("Could not call indexer during patching", ex); |
| | 127 | | } |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | private readonly MethodInfo _addMethod; |
| | 131 | | private readonly MethodInfo _setMethod; |
| | 132 | | private readonly MethodInfo _countMethod; |
| | 133 | | } |
| | 134 | |
|
| 1 | 135 | | private static readonly Dictionary<Type, InvokeInfo> _methodTable = new Dictionary<Type, InvokeInfo>(); |
| | 136 | | } |