(
IEnumerable<T> values,
Serializer<T> elementSerializer,
WriteContext context,
bool hasGenerics)
| 93 | } |
| 94 | |
| 95 | public static void WriteCollectionData<T>( |
| 96 | IEnumerable<T> values, |
| 97 | Serializer<T> elementSerializer, |
| 98 | WriteContext context, |
| 99 | bool hasGenerics) |
| 100 | { |
| 101 | TypeInfo elementTypeInfo = context.TypeResolver.GetTypeInfo<T>(); |
| 102 | List<T> list = values as List<T> ?? [.. values]; |
| 103 | int count = list.Count; |
| 104 | context.Writer.WriteVarUInt32((uint)count); |
| 105 | if (count == 0) |
| 106 | { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | bool hasNull = false; |
| 111 | if (elementTypeInfo.IsNullableType) |
| 112 | { |
| 113 | for (int i = 0; i < count; i++) |
| 114 | { |
| 115 | if (list[i] is not null) |
| 116 | { |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | hasNull = true; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | bool trackRef = context.TrackRef && elementTypeInfo.IsRefType; |
| 126 | bool declaredElementType = hasGenerics && |
| 127 | (CanDeclareElementType<T>(elementTypeInfo, context) || |
| 128 | CanDeclareRuntimeElementType(list, elementTypeInfo, context)); |
| 129 | bool dynamicElementType = elementTypeInfo.IsDynamicType; |
| 130 | byte header = dynamicElementType ? (byte)0 : CollectionBits.SameType; |
| 131 | if (trackRef) |
| 132 | { |
| 133 | header |= CollectionBits.TrackingRef; |
| 134 | } |
| 135 | |
| 136 | if (hasNull) |
| 137 | { |
| 138 | header |= CollectionBits.HasNull; |
| 139 | } |
| 140 | |
| 141 | if (declaredElementType) |
| 142 | { |
| 143 | header |= CollectionBits.DeclaredElementType; |
| 144 | } |
| 145 | |
| 146 | context.Writer.WriteUInt8(header); |
| 147 | if (!dynamicElementType && !declaredElementType) |
| 148 | { |
| 149 | context.TypeResolver.WriteTypeInfo(elementSerializer, context); |
| 150 | } |
| 151 | |
| 152 | if (dynamicElementType) |
no test coverage detected