Пример #1
0
        public Bin[] Serialize(IGrainState grainState)
        {
            var properties = grainState.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            List <Bin> binList = new List <Bin>();

            binList.Add(new Bin("type", grainState.Type.Name));

            foreach (var propertyInfo in properties)
            {
                // ignore ignore attributes
                if (propertyInfo.GetCustomAttribute <JsonIgnoreAttribute>() != null)
                {
                    continue;
                }
                if (propertyInfo.GetCustomAttribute <System.Text.Json.Serialization.JsonIgnoreAttribute>() != null)
                {
                    continue;
                }
                if (propertyInfo.GetCustomAttribute <MessagePack.IgnoreMemberAttribute>() != null)
                {
                    continue;
                }

                var propertyType = propertyInfo.PropertyType;
                var value        = propertyInfo.GetValue(grainState.State);

                Value binValue;
                if (propertyType == typeof(int))
                {
                    binValue = new Value.IntegerValue((int)value);
                }
                else if (propertyType == typeof(uint))
                {
                    binValue = new Value.UnsignedIntegerValue((uint)value);
                }
                else if (propertyType == typeof(short))
                {
                    binValue = new Value.ShortValue((short)value);
                }
                else if (propertyType == typeof(ushort))
                {
                    binValue = new Value.UnsignedShortValue((ushort)value);
                }
                else if (propertyType == typeof(long))
                {
                    binValue = new Value.LongValue((long)value);
                }
                else if (propertyType == typeof(ulong))
                {
                    binValue = new Value.UnsignedLongValue((ulong)value);
                }
                else if (propertyType == typeof(string))
                {
                    binValue = new Value.StringValue((string)value);
                }
                else if (propertyType == typeof(char))
                {
                    binValue = new Value.StringValue(((char)value).ToString());
                }
                else if (propertyType == typeof(double))
                {
                    binValue = new Value.DoubleValue((double)value);
                }
                else if (propertyType == typeof(float))
                {
                    binValue = new Value.DoubleValue((float)value);
                }
                else if (propertyType == typeof(byte))
                {
                    binValue = new Value.ByteValue((byte)value);
                }
                else if (propertyType == typeof(byte[]))
                {
                    binValue = new Value.BytesValue((byte[])value);
                }
                else if (propertyType == typeof(Guid))
                {
                    binValue = new Value.BytesValue(((Guid)value).ToByteArray());
                }
                else if (propertyType.IsEnum)
                {
                    binValue = new Value.StringValue(value.ToString());
                }
                else if (typeof(IList <string>).IsAssignableFrom(propertyType) ||
                         typeof(IList <int>).IsAssignableFrom(propertyType) ||
                         typeof(IList <long>).IsAssignableFrom(propertyType) ||
                         typeof(IList <short>).IsAssignableFrom(propertyType) ||
                         typeof(IList <uint>).IsAssignableFrom(propertyType) ||
                         typeof(IList <ulong>).IsAssignableFrom(propertyType) ||
                         typeof(IList <ushort>).IsAssignableFrom(propertyType) ||
                         typeof(IList <double>).IsAssignableFrom(propertyType) ||
                         typeof(IList <float>).IsAssignableFrom(propertyType))
                {
                    binValue = new Value.ListValue((IList)value);
                }
                else if (typeof(IDictionary).IsAssignableFrom(propertyType) && // just generic dictionary of scalar types
                         propertyType.IsGenericType &&
                         propertyType.GenericTypeArguments.All(p =>
                                                               p == typeof(int) || p == typeof(string) || p == typeof(long) ||
                                                               p == typeof(short) || p == typeof(uint) || p == typeof(ulong) ||
                                                               p == typeof(ushort) || p == typeof(double) || p == typeof(float)))
                {
                    binValue = new Value.MapValue((IDictionary)value);
                }
                else // all othersto json
                {
                    binValue = new Value.StringValue(JsonConvert.SerializeObject(value));
                }

                binList.Add(new Bin(propertyInfo.Name, binValue));
            }
            return(binList.ToArray());
        }
        public Bin[] Serialize(IGrainState grainState)
        {
            var properties = grainState.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            List <Bin> binList = new List <Bin>();

            binList.Add(new Bin("type", grainState.Type.Name));

            foreach (var propertyInfo in properties)
            {
                // ignore ignore attributes
                if (propertyInfo.GetCustomAttribute <JsonIgnoreAttribute>() != null)
                {
                    continue;
                }
                if (propertyInfo.GetCustomAttribute <System.Text.Json.Serialization.JsonIgnoreAttribute>() != null)
                {
                    continue;
                }
                if (propertyInfo.GetCustomAttribute <MessagePack.IgnoreMemberAttribute>() != null)
                {
                    continue;
                }

                var propertyType = propertyInfo.PropertyType;
                var value        = propertyInfo.GetValue(grainState.State);

                Value binValue;
                if (propertyType == typeof(int))
                {
                    binValue = new Value.IntegerValue((int)value);
                }
                else if (propertyType == typeof(uint))
                {
                    binValue = new Value.UnsignedIntegerValue((uint)value);
                }
                else if (propertyType == typeof(short))
                {
                    binValue = new Value.ShortValue((short)value);
                }
                else if (propertyType == typeof(ushort))
                {
                    binValue = new Value.UnsignedShortValue((ushort)value);
                }
                else if (propertyType == typeof(long))
                {
                    binValue = new Value.LongValue((long)value);
                }
                else if (propertyType == typeof(ulong))
                {
                    binValue = new Value.UnsignedLongValue((ulong)value);
                }
                else if (propertyType == typeof(string))
                {
                    binValue = new Value.StringValue((string)value);
                }
                else if (propertyType == typeof(char))
                {
                    binValue = new Value.StringValue(((char)value).ToString());
                }
                else if (propertyType == typeof(double))
                {
                    binValue = new Value.DoubleValue((double)value);
                }
                else if (propertyType == typeof(float))
                {
                    binValue = new Value.DoubleValue((float)value);
                }
                else if (propertyType == typeof(byte))
                {
                    binValue = new Value.ByteValue((byte)value);
                }
                else if (propertyType == typeof(byte[]))
                {
                    binValue = new Value.BytesValue((byte[])value);
                }
                else if (propertyType == typeof(Guid))
                {
                    binValue = new Value.BytesValue(((Guid)value).ToByteArray());
                }
                else if (propertyType.IsEnum)
                {
                    binValue = new Value.StringValue(value.ToString());
                }
                else // to json
                {
                    binValue = new Value.StringValue(JsonConvert.SerializeObject(value));
                }

                binList.Add(new Bin(propertyInfo.Name, binValue));
            }
            return(binList.ToArray());
        }