/// <summary> /// <para>将指定的键和值添加到Map</para> /// <para>Adds the specified key and value to the Map</para> /// </summary> public void Add(object key, object value) { if (key is BssomValue bsVal) { switch (bsVal.ValueType) { case BssomValueType.Number: case BssomValueType.String: case BssomValueType.Guid: case BssomValueType.Boolean: case BssomValueType.Char: case BssomValueType.DateTime: case BssomValueType.Decimal: case BssomValueType.Float: { _dict.Add(key, value); return; } default: throw BssomSerializationTypeFormatterException.BssomMapKeyUnsupportedType(key.GetType()); } } BssMapKeyResolverProvider.VertyBssMapKeyType(key); _dict.Add(key, value); }
static IBssMapKeyStaticResolverCache() { if (BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(T), out IBssMapKeyResolver convertor)) { Instance = (IBssMapKeyResolver <T>)convertor; } else { BssomSerializationTypeFormatterException.BssomMapKeyUnsupportedType(typeof(T)); } }
static BssMapObjMarshalReader() { if (typeof(TKey) == typeof(object)) { readKeyFunc = GetObjectKey; } else { staticKeyResolver = BssMapKeyResolverProvider.GetAndVertiyBssMapKeyResolver <TKey>(); readKeyFunc = GetKey; } }
/// <summary> /// Initializes a new instance of the <see cref="BssomMap"/> class that contains elements copied from the specified IEnumerable /// </summary> /// <param name="ienums"></param> public BssomMap(IEnumerable <KeyValuePair <object, object> > ienums) { if (ienums.TryGetICollectionCount(out int count)) { _dict = new Dictionary <object, object>(count); } else { _dict = new Dictionary <object, object>(); } foreach (KeyValuePair <object, object> item in ienums) { BssMapKeyResolverProvider.VertyBssMapKeyType(item.Key); _dict.Add(item.Key, item.Value); } }
public void MapTypeOnlySupportsSpecifiedKeyType() { IBssMapKeyResolver keyResolver; BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(sbyte), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Int16), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Int32), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Int64), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(byte), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(UInt16), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(UInt32), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(UInt64), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Char), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Boolean), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Single), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Double), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(string), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Guid), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(DateTime), out keyResolver).IsTrue(); BssMapKeyResolverProvider.TryGetBssMapKeyResolver(typeof(Decimal), out keyResolver).IsTrue(); BssMapKeyResolverProvider.StringBssMapKeyResolver.Is(BssMapStringKeyResolver.Insance); }
static FormatterCache() { Type t = typeof(T); if (t == typeof(IDictionary)) { Formatter = (IBssomFormatter <T>)((IBssomFormatter)IDictionaryFormatter.Instance); return; } if (TypeIsDictionary(t, out ConstructorInfo constructor, out bool typeIsGeneric, out Type genericTypeDefinition, out Type genericKeyType, out Type genericValueType)) { if (typeIsGeneric && genericKeyType != typeof(object) && !BssMapKeyResolverProvider.TryGetBssMapKeyResolver(genericKeyType, out IBssMapKeyResolver keyConvert)) { return;// throw BssomSerializationException.MapKeyTypeError(); } TypeInfo buildType; if (typeIsGeneric) { if (t.IsInterface) { buildType = IDictionaryFormatterTypeBuilder.BuildGenericIDictionaryInterfaceType(DynamicAssembly, t, genericTypeDefinition, genericKeyType, genericValueType); } else { buildType = IDictionaryFormatterTypeBuilder.BuildGenericIDictionaryImplementationType(DynamicAssembly, constructor, t, genericKeyType, genericValueType); } } else { //impl IDictionary class/struct buildType = IDictionaryFormatterTypeBuilder.BuildIDictionaryImplementationType(DynamicAssembly, constructor, t); } Formatter = (IBssomFormatter <T>)Activator.CreateInstance(buildType); } }
public static BssomFieldOffsetInfo IndexOf(IBssomBuffer buffer, IIndexOfInputSource indexOfInputSource) { if (buffer is null) { throw new ArgumentNullException(nameof(buffer)); } if (indexOfInputSource is null) { throw new ArgumentNullException(nameof(indexOfInputSource)); } BssomReader reader = new BssomReader(buffer); BssomFieldOffsetInfo info = new BssomFieldOffsetInfo(); indexOfInputSource.Reset(); if (!indexOfInputSource.MoveNext()) { throw BssomSerializationOperationException.InputDataSouceIsEmpty(); } Next: byte bssOjectType = reader.SkipBlankCharacterAndReadBssomType(); switch (bssOjectType) { case BssomType.Map1: { object key = indexOfInputSource.CurrentMapKey(); if (!BssMapKeyResolverProvider.TryGetBssMapKeyResolver(key.GetType(), out IBssMapKeyResolver resolver)) { throw BssomSerializationTypeFormatterException.BssomMapKeyUnsupportedType(key.GetType()); } reader.SkipVariableNumber(); //Skip Length int count = reader.ReadVariableNumber(); Raw64BytesISegment keyISegment = resolver.GetMap1KeySegment(key); for (int i = 0; i < count; i++) { int keyLength = reader.GetMap1KeyLength(); if (keyISegment.DataLen != keyLength) { reader.BssomBuffer.Seek(keyLength, BssomSeekOrgin.Current); //Advance Key reader.SkipObject(); //Skip Value continue; } //determine if the key is equal bool keyIsEqual = true; for (int z = 0; z < keyISegment.Length; z++) { if (keyISegment[z] != reader.ReadRaw64(ref keyLength)) { reader.BssomBuffer.Seek(keyLength, BssomSeekOrgin.Current); //Advance remaining keyLength reader.SkipObject(); //Skip Value keyIsEqual = false; break; } } if (keyIsEqual) { if (!indexOfInputSource.MoveNext()) { info.Offset = reader.Position; info.IsArray1Type = false; return(info); } else { goto Next; } } } if (indexOfInputSource.MoveNext()) { throw BssomSerializationOperationException.BssomMapIsNull(key); } info.Offset = -1; return(info); } case BssomType.Map2: { object key = indexOfInputSource.CurrentMapKey(); if (!BssMapKeyResolverProvider.TryGetBssMapKeyResolver(key.GetType(), out IBssMapKeyResolver resolver)) { throw BssomSerializationTypeFormatterException.BssomMapKeyUnsupportedType(key.GetType()); } UInt64BytesISegment keyISegment = resolver.GetMap2KeySegment(key); BssMapHeadPackInfo aprp = BssMapHeadPackInfo.Create(ref reader); if (aprp.MapHead.ElementCount > 0) { ref byte refb = ref reader.BssomBuffer.TryReadFixedRef(aprp.MapHead.RouteLength, out bool haveEnoughSizeAndCanBeFixed); if (haveEnoughSizeAndCanBeFixed) { if (BssMapObjMarshalReader.TrySeek(keyISegment, resolver.KeyType, resolver.KeyIsNativeType, ref aprp, ref reader, ref refb)) { if (!indexOfInputSource.MoveNext()) { info.Offset = reader.Position; info.IsArray1Type = false; return(info); } else { goto Next; } } } else { if (BssMapObjMarshalReader.TrySeekSlow(keyISegment, resolver.KeyType, resolver.KeyIsNativeType, ref aprp, ref reader)) { if (!indexOfInputSource.MoveNext()) { info.Offset = reader.Position; info.IsArray1Type = false; return(info); } else { goto Next; } } } } if (indexOfInputSource.MoveNext()) { throw BssomSerializationOperationException.BssomMapIsNull(key); } info.Offset = -1; return(info); }
private static TKey GetObjectKey(bool isNativeType, byte keyType, BssmapAnalysisStack stack, byte valueByteCount) { IBssMapKeyResolver convert = BssMapKeyResolverProvider.GetAndVertiyBssMapKeyResolver(isNativeType, keyType); return((TKey)convert.ReadMap2Key(stack.ToUlongs((byte)valueByteCount))); }