private object Serialize(Type type, object obj)
        {
            // Check if raw serialization is possible.
            if (this.IsRawSerializationPossible(type))
            {
                return(obj);
            }

            // Get serializer for object.
            IDictionarySerializer serializer = this.GetSerializer(type);

            if (serializer == null)
            {
                // Check if nullable.
                Type nullableType = Nullable.GetUnderlyingType(type);
                if (nullableType != null)
                {
                    return(this.Serialize(nullableType, obj));
                }

                // Check if generic type.
                if (type.IsGenericType())
                {
                    Type genericType = type.GetGenericTypeDefinition();
                    if (genericType != null && this.genericSerializerMap.TryGetValue(genericType, out serializer))
                    {
                        return(serializer.Serialize(this, obj));
                    }
                }

                // Check if enum type.
                if (type.IsEnum())
                {
                    return(obj.ToString());
                }

                // No custom serializer found - try reflection.
                if (type.IsAttributeDefined <DictionarySerializableAttribute>())
                {
                    return(this.SerializeReflection(obj));
                }

                throw new ArgumentException(
                          string.Format("Unsupported type for dictionary serialization: {0}", type), "obj");
            }

            return(serializer.Serialize(this, obj));
        }
示例#2
0
        public void Execute()
        {
            IDictionary <string, string> dictionary;

            using (var fs = new FileStream(_storeFile, FileMode.Open))
            {
                dictionary = _deserializer.FullDeserialize(fs);
            }

            dictionary.Remove(_key);

            using (var fs = new FileStream(_storeFile, FileMode.Create))
            {
                _serializer.Serialize(dictionary, fs);
            }
        }
示例#3
0
文件: SetCommand.cs 项目: jimtonn/kv
        public void Execute()
        {
            IDictionary <string, string> dictionary;

            if (!File.Exists(_storeFile))
            {
                using (File.Create(_storeFile)) { }
            }

            using (var fs = new FileStream(_storeFile, FileMode.Open))
            {
                dictionary = _deserializer.FullDeserialize(fs);
            }
            dictionary[_key] = _value;

            using (var fs = new FileStream(_storeFile, FileMode.Create))
            {
                _serializer.Serialize(dictionary, fs);
            }
        }