Exemplo n.º 1
0
        public void RegisterCustomType <T>(byte code, CustomTypeSerializer <T> serializer)
        {
            var type = typeof(T);

            if (_typeMappings.ContainsKey(type))
            {
                throw new TypeRegistrationException($"The type '{type.FullName}' is already registered.");
            }

            if (_codeMappings.ContainsKey(code))
            {
                throw new TypeRegistrationException($"The code '{code}' is already registered.");
            }

            var customType = new CustomType(code, serializer);

            _typeMappings.Add(type, customType);
            _codeMappings.Add(code, customType);
        }
Exemplo n.º 2
0
        public void RegisterCustomType <T>(CustomTypeSerializer <T> serializer)
        {
            byte nextCode = _nextCustomTypeCode;

            while (_codeMappings.ContainsKey(nextCode))
            {
                if (++nextCode == byte.MaxValue)
                {
                    throw new TypeRegistrationException(
                              $"Type registration failed: You registered too many custom types. You can register up to {MaxCustomTypes} types");
                }
            }

            if (nextCode == byte.MaxValue)
            {
                throw new TypeRegistrationException(
                          $"Type registration failed: You registered too many custom types. You can register up to {MaxCustomTypes} types");
            }

            RegisterCustomType(nextCode, serializer);

            _nextCustomTypeCode = ++nextCode;
        }