NCache 4.6 - Online Documentation

Using Compact Serialization

To use Compact Serialization, object types that are to be compact serialized need to be configured through NCache Manager.  NCache internally handles serialization/deserialization of registered classes and the user does not need to explicitly code for it.
However, NCache also allows to explicitly implement ICompactSerializable interface.
       To utilize the ICompactSerializable interface, include the following namespace in your application: Alachisoft.NCache.Runtime.GenericTypesProvider.
To implement this interface, the custom object class needs to implement Serialize/Deserialize methods which will be called by NCache while serializing/deserializing this object. Sample code for implement this interface is given below:
   //Sample code for custom ICompactSerializable class
    public class CustomObject : ICompactSerializable
        private string _attribute1;
        private int _attribute2;
        private bool _attribute3;
        public CustomObject()
        { }
        public CustomObject(string attrib1, int attrib2, bool attrib3)
            _attribute1 = attrib1;
            _attribute2 = attrib2;
            _attribute3 = attrib3;
        #region ICompactSerializable Members
        public void Deserialize(Alachisoft.NCache.Runtime.Serialization.IO.CompactReader reader)
            _attribute1 = reader.ReadObject() as string;
            _attribute2 = reader.ReadInt32();
            _attribute3 = reader.ReadBoolean();
        public void Serialize(Alachisoft.NCache.Runtime.Serialization.IO.CompactWriter writer)
Care should be taken that class attributes are deserialized in the same order in which they are serialized otherwise serialized stream won’t be deserialized.
Implementing a Generic Type Handler
NCache also allows registering generic types in Compact Serialization. For this,  each type needs to be registered through NCache Manager. There is another way for registering generic compact types through "Generic Type Handler". With Generic Type Handler, the user can add all the generic types at once by implementing the IGenericTypeProvider interface. This saves a lot of time and effort in case the user wants to register a large number of types.
       To utilize the IGenericTypeProvider, include the following namespace in your application: Alachisoft.NCache.Runtime.GenericTypesProvider.
To implement this, the user needs to implement GetGenericTypes method and return the desired generic types in Type array. After this, deploy this class assembly to NCache Manager. NCache Manager will register all types populated in Type array. Sample code is given below:
namespace CompactSerialization
    public class CustomGenericType<T, K, V>
    public class SetGenerics : IGenericTypeProvider
        Type[] allowedGenericTypes = new Type[5];
        Type[] IGenericTypeProvider.GetGenericTypes()
            allowedGenericTypes[0] = typeof(Dictionary<string, Customer>);
            allowedGenericTypes[1] = typeof(List<int>);
            allowedGenericTypes[2] = typeof(CustomGenericType<Employee, Product, Customer>);
            allowedGenericTypes[3] = typeof(CustomGenericType<Employee, Product, int>);
            allowedGenericTypes[4] = typeof(CustomGenericType<Employee, string, byte>);
            return allowedGenericTypes;
As can be seen in the above example, a list of types are specified which are to be allowed in the generic types for Compact Serialization.
See Also