public static ArraySegment<byte>[] SerializePropertyBag(IDictionary<XName, InstanceValue> properties, InstanceEncodingOption encodingOption)
 {
     ArraySegment<byte>[] segmentArray = new ArraySegment<byte>[4];
     if (properties.Count > 0)
     {
         IObjectSerializer objectSerializer = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
         XmlPropertyBag bag = new XmlPropertyBag();
         XmlPropertyBag bag2 = new XmlPropertyBag();
         Dictionary<XName, object> dictionary = new Dictionary<XName, object>();
         Dictionary<XName, object> dictionary2 = new Dictionary<XName, object>();
         Dictionary<XName, object>[] dictionaryArray = new Dictionary<XName, object>[] { bag, dictionary, bag2, dictionary2 };
         foreach (KeyValuePair<XName, InstanceValue> pair in properties)
         {
             bool flag = XmlPropertyBag.GetPrimitiveType(pair.Value.Value) == PrimitiveType.Unavailable;
             int index = (((pair.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.WriteOnly) ? 2 : 0) + (flag ? 1 : 0);
             dictionaryArray[index].Add(pair.Key, pair.Value.Value);
         }
         bag2.Remove(SqlWorkflowInstanceStoreConstants.StatusPropertyName);
         bag2.Remove(SqlWorkflowInstanceStoreConstants.LastUpdatePropertyName);
         bag2.Remove(SqlWorkflowInstanceStoreConstants.PendingTimerExpirationPropertyName);
         dictionary2.Remove(SqlWorkflowInstanceStoreConstants.BinaryBlockingBookmarksPropertyName);
         for (int i = 0; i < dictionaryArray.Length; i++)
         {
             if (dictionaryArray[i].Count > 0)
             {
                 if (dictionaryArray[i] is XmlPropertyBag)
                 {
                     segmentArray[i] = objectSerializer.SerializeValue(dictionaryArray[i]);
                 }
                 else
                 {
                     segmentArray[i] = objectSerializer.SerializePropertyBag(dictionaryArray[i]);
                 }
             }
         }
     }
     return segmentArray;
 }
        public static ArraySegment<byte>[] SerializePropertyBag(IDictionary<XName, InstanceValue> properties, InstanceEncodingOption encodingOption)
        {
            ArraySegment<byte>[] dataArrays = new ArraySegment<byte>[4];

            if (properties.Count > 0)
            {
                IObjectSerializer serializer = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
                XmlPropertyBag primitiveProperties = new XmlPropertyBag();
                XmlPropertyBag primitiveWriteOnlyProperties = new XmlPropertyBag();
                Dictionary<XName, object> complexProperties = new Dictionary<XName, object>();
                Dictionary<XName, object> complexWriteOnlyProperties = new Dictionary<XName, object>();
                Dictionary<XName, object>[] propertyBags = new Dictionary<XName, object>[] { primitiveProperties, complexProperties,
                    primitiveWriteOnlyProperties, complexWriteOnlyProperties };

                foreach (KeyValuePair<XName, InstanceValue> property in properties)
                {
                    bool isComplex = (XmlPropertyBag.GetPrimitiveType(property.Value.Value) == PrimitiveType.Unavailable);
                    bool isWriteOnly = (property.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.WriteOnly;
                    int index = (isWriteOnly ? 2 : 0) + (isComplex ? 1 : 0);
                    propertyBags[index].Add(property.Key, property.Value.Value);
                }

                // Remove the properties that are already stored as individual columns from the serialized blob
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.StatusPropertyName);
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.LastUpdatePropertyName);
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.PendingTimerExpirationPropertyName);

                complexWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.BinaryBlockingBookmarksPropertyName);

                for (int i = 0; i < propertyBags.Length; i++)
                {
                    if (propertyBags[i].Count > 0)
                    {
                        if (propertyBags[i] is XmlPropertyBag)
                        {
                            dataArrays[i] = serializer.SerializeValue(propertyBags[i]);
                        }
                        else
                        {
                            dataArrays[i] = serializer.SerializePropertyBag(propertyBags[i]);
                        }
                    }
                }
            }

            return dataArrays;
        }