A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections; 3using System.Collections.Generic; 4 5namespace Unity.VisualScripting 6{ 7 public class VariantCollection<TBase, TImplementation> : ICollection<TBase> where TImplementation : TBase 8 { 9 public VariantCollection(ICollection<TImplementation> implementation) 10 { 11 if (implementation == null) 12 { 13 throw new ArgumentNullException(nameof(implementation)); 14 } 15 16 this.implementation = implementation; 17 } 18 19 public ICollection<TImplementation> implementation { get; private set; } 20 21 public int Count => implementation.Count; 22 23 public bool IsReadOnly => implementation.IsReadOnly; 24 25 IEnumerator IEnumerable.GetEnumerator() 26 { 27 return GetEnumerator(); 28 } 29 30 public IEnumerator<TBase> GetEnumerator() 31 { 32 foreach (var i in implementation) 33 { 34 yield return i; 35 } 36 } 37 38 public void Add(TBase item) 39 { 40 if (!(item is TImplementation)) 41 { 42 throw new NotSupportedException(); 43 } 44 45 implementation.Add((TImplementation)item); 46 } 47 48 public void Clear() 49 { 50 implementation.Clear(); 51 } 52 53 public bool Contains(TBase item) 54 { 55 if (!(item is TImplementation)) 56 { 57 throw new NotSupportedException(); 58 } 59 60 return implementation.Contains((TImplementation)item); 61 } 62 63 public bool Remove(TBase item) 64 { 65 if (!(item is TImplementation)) 66 { 67 throw new NotSupportedException(); 68 } 69 70 return implementation.Remove((TImplementation)item); 71 } 72 73 public void CopyTo(TBase[] array, int arrayIndex) 74 { 75 if (array == null) 76 { 77 throw new ArgumentNullException(nameof(array)); 78 } 79 80 if (arrayIndex < 0) 81 { 82 throw new ArgumentOutOfRangeException(nameof(arrayIndex)); 83 } 84 85 if (array.Length - arrayIndex < Count) 86 { 87 throw new ArgumentException(); 88 } 89 90 var implementationArray = new TImplementation[Count]; 91 implementation.CopyTo(implementationArray, 0); 92 93 for (var i = 0; i < Count; i++) 94 { 95 array[i + arrayIndex] = implementationArray[i]; 96 } 97 } 98 } 99}