public void TestGlobalSerializer()
        {
            var config       = new SerializationConfig();
            var globalConfig = new GlobalSerializerConfig();

            globalConfig.SetClassName(typeof(GlobalSerializer).AssemblyQualifiedName);
            config.SetGlobalSerializerConfig(globalConfig);

            var ss = new SerializationServiceBuilder().SetConfig(config).Build();

            var foo = new CustomSerializableType {
                Value = "fooooo"
            };

            var d      = ss.ToData(foo);
            var newFoo = ss.ToObject <CustomSerializableType>(d);

            Assert.AreEqual(newFoo.Value, foo.Value);
        }
        public void TestGlobalSerializerOverride()
        {
            var config       = new SerializationConfig();
            var globalConfig = new GlobalSerializerConfig();

            var globalListSerializer = new GlobalListSerializer();

            globalConfig.SetImplementation(globalListSerializer).SetOverrideClrSerialization(true);
            config.SetGlobalSerializerConfig(globalConfig);

            var ss = new SerializationServiceBuilder().SetConfig(config).Build();


            var list = new List <string> {
                "foo", "bar"
            };

            var d     = ss.ToData(list);
            var input = new ByteArrayObjectDataInput(d.ToByteArray(), HeapData.DataOffset, ss, ByteOrder.BigEndian);

            var actual = (List <string>)globalListSerializer.Read(input);

            Assert.AreEqual(list, actual);
        }
        public ISerializationService Build()
        {
            if (_portableVersion < 0)
            {
                _portableVersion = 0;
            }
            if (_config != null)
            {
                AddConfigDataSerializableFactories(_dataSerializableFactories, _config);
                AddConfigPortableFactories(_portableFactories, _config);
                classDefinitions = classDefinitions.Union(_config.GetClassDefinitions()).ToList();
            }
            //TODO: add support for multiple versions
            var ss = new SerializationService(
                CreateInputOutputFactory(),
                _portableVersion,
                _dataSerializableFactories,
                _portableFactories,
                classDefinitions,
                _checkClassDefErrors,
                _managedContext,
                _partitioningStrategy,
                _initialOutputBufferSize,
                _enableCompression,
                _enableSharedObject);

            if (_config != null)
            {
                if (_config.GetGlobalSerializerConfig() != null)
                {
                    GlobalSerializerConfig globalSerializerConfig = _config.GetGlobalSerializerConfig();
                    ISerializer            serializer             = globalSerializerConfig.GetImplementation();
                    if (serializer == null)
                    {
                        try
                        {
                            string className = globalSerializerConfig.GetClassName();
                            Type   type      = Type.GetType(className);
                            if (type != null)
                            {
                                serializer = Activator.CreateInstance(type) as ISerializer;
                            }
                        }
                        catch (Exception e)
                        {
                            throw new HazelcastSerializationException(e);
                        }
                    }
                    var aware = serializer as IHazelcastInstanceAware;
                    if (aware != null)
                    {
                        aware.SetHazelcastInstance(_hazelcastInstance);
                    }
                    ss.RegisterGlobal(serializer);
                }
                ICollection <SerializerConfig> typeSerializers = _config.GetSerializerConfigs();
                foreach (SerializerConfig serializerConfig in typeSerializers)
                {
                    ISerializer serializer = serializerConfig.GetImplementation();
                    if (serializer == null)
                    {
                        try
                        {
                            string className = serializerConfig.GetClassName();
                            Type   type      = Type.GetType(className);
                            if (type != null)
                            {
                                serializer = Activator.CreateInstance(type) as ISerializer;
                            }
                        }
                        catch (Exception e)
                        {
                            throw new HazelcastSerializationException(e);
                        }
                    }
                    if (serializer is IHazelcastInstanceAware)
                    {
                        ((IHazelcastInstanceAware)serializer).SetHazelcastInstance(_hazelcastInstance);
                    }
                    Type typeClass = serializerConfig.GetTypeClass();
                    if (typeClass == null)
                    {
                        try
                        {
                            string className = serializerConfig.GetTypeClassName();
                            typeClass = Type.GetType(className);
                        }
                        catch (TypeLoadException e)
                        {
                            throw new HazelcastSerializationException(e);
                        }
                    }
                    ////call by reflaction
                    //MethodInfo method = typeof(ISerializationService).GetMethod("Register");
                    //MethodInfo generic = method.MakeGenericMethod(typeClass);
                    //generic.Invoke(ss, new object[] { serializer });
                    ////mimics: ss.Register<typeClass>(serializer);"
                    ss.Register(typeClass, serializer);
                }
            }
            return(ss);
        }