예제 #1
0
        private static void SerializeMember(Dictionary <string, string> buffer, SerializeMember member, object reference, string subkey)
        {
            var ignoreAttribute = member.GetCustomAttribute <RedisIgnoreAttribute>();

            if (ignoreAttribute != null)
            {
                return;
            }

            //Ignore generated blocks
            var generatedAttribute = member.GetCustomAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute>();

            if (generatedAttribute != null)
            {
                return;
            }


            var attribute = member.GetCustomAttribute <RedisPropertyAttribute>();

            if (attribute == null)
            {
                //We will skip fields without propert attributes. They need to be explicitly defined unlike properties that need to be explicitly ignored.
                if (member.IsField)
                {
                    return;
                }
                attribute = new RedisPropertyAttribute(member.Name);
            }

            //Prepare its key
            string key = PrepareKey(attribute, member.Name, subkey);

            //If it has a serialize attribute, we want to construct its serializer
            //var serializerAttribute = member.GetCustomAttribute<RedisSerializerAttribute>();
            //if (serializerAttribute != null)
            //{
            //	//They have a custom serializer, so lets construct its type
            //	var constructor = serializerAttribute.Serializer.GetConstructor(new Type[0]);
            //	if (constructor != null)
            //	{
            //		var serializer = constructor.Invoke(new object[0]) as RedisSerializer;
            //		if (serializer != null)
            //		{
            //			serializer.Serialize(buffer, member, reference, key);
            //			return;
            //		}
            //
            //		throw new Exception("Bad Serialization on the custom serializer! Failed to cast into a RedisSerializer");
            //	}
            //
            //	throw new Exception("Bad Serialization on the custom serializer! Failed to find a constructor with 0 elements");
            //}

            //Make sure its a object
            if (member.IsPrimitive || member.IsEnum || member.IsString)
            {
                //Add it to the dictionary
                object v = member.GetValue(reference);
                if (v != null)
                {
                    buffer.Add(key, v.ToString());
                }
                return;
            }

            //Everything else fails, so do classical serialization
            object propval = member.GetValue(reference);

            if (propval != null)
            {
                Serialize(buffer, propval, key + ".");
            }
        }
예제 #2
0
        private static bool DeserializeMember(Dictionary <string, string> buffer, Type type, SerializeMember member, ref object reference, string subkey, RedisOptionAttribute options)
        {
            //Ignore elements with ignore attributes
            var ignoreAttribute = member.GetCustomAttribute <RedisIgnoreAttribute>();

            if (ignoreAttribute != null)
            {
                return(false);
            }

            //Ignore generated blocks
            var generatedAttribute = member.GetCustomAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute>();

            if (generatedAttribute != null)
            {
                return(false);
            }

            //Serialize the member if it has an attribute or is forced to serialize.
            var attribute = member.GetCustomAttribute <RedisPropertyAttribute>();

            if (attribute == null)
            {
                if (member.IsField)
                {
                    return(false);
                }
                attribute = new RedisPropertyAttribute(member.Name);
            }

            //Prepare its key
            string key = PrepareKey(attribute, member.Name, subkey);

            //If class, we have to do something completely different
            //If it has a serialize attribute, we want to construct its serializer
            //var serializerAttribute = member.GetCustomAttribute<RedisSerializerAttribute>();
            //if (serializerAttribute != null)
            //{
            //	//They have a custom serializer, so lets construct its type
            //	var constructor = serializerAttribute.Serializer.GetConstructor(new Type[0]);
            //	if (constructor != null)
            //	{
            //		var serializer = constructor.Invoke(new object[0]) as RedisSerializer;
            //		if (serializer != null)
            //		{
            //			object v = serializer.Deserialize(buffer, member, key);
            //			member.SetValue(reference, v);
            //			return true;
            //		}
            //
            //		throw new Exception("Bad Serialization on the custom serializer! Failed to cast into a RedisSerializer");
            //	}
            //
            //	throw new Exception("Bad Serialization on the custom serializer! Failed to find a constructor with 0 elements");
            //}

            //If the property is a string, just cast ez pz
            if (member.IsPrimitive || member.IsString || member.IsEnum)
            {
                string primval;
                if (buffer.TryGetValue(key, out primval))
                {
                    if (member.IsPrimitive || member.IsEnum)
                    {
                        object v = TypeDescriptor.GetConverter(member.Type).ConvertFromString(primval);
                        member.SetValue(reference, v);
                    }
                    else
                    {
                        member.SetValue(reference, primval);
                    }

                    return(true);
                }

                return(false);
            }

            //We have to do it the classical way with a subkey
            //var propvalConstructor = propertyType.GetConstructor(new Type[0]);
            //object propval = propvalConstructor.Invoke(new object[0]);
            object propval = null;

            try
            {
                propval = Activator.CreateInstance(member.Type);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception while creating a instance!");
                throw e;
            }

            //Serialize
            if (propval != null && Deserialize(buffer, member.Type, ref propval, key + "."))
            {
                member.SetValue(reference, propval);
                return(true);
            }

            return(false);
        }