Example #1
0
        public bool TrySerialize(BinaryWriter writer, object value, string scenario = default, string @case = default)
        {
            bool        result   = true;
            ObjectFlags flag     = ObjectFlags.None;
            TypeInfo    typeInfo = value?.GetType().GetTypeInfo();

            if (typeInfo == null)
            {
                flag    = ObjectFlags.IsNull;
                result &= BinarySerializer.TrySerialize(writer, flag);
            }
            else if (!typeInfo.IsValueType)
            {
                result &= BinarySerializer.TrySerialize(writer, flag);
            }
            switch (flag)
            {
            case ObjectFlags.None:
                IEnumerable <FieldInfo>    fields     = typeInfo.DeclaredFields.Where(x => FieldSearcher(x, scenario, @case));
                IEnumerable <PropertyInfo> properties = typeInfo.DeclaredProperties.Where(x => PropertySearcher(x, scenario, @case));
                uint count = (uint)(fields.Count() + properties.Count());
                result &= BinarySerializer.TrySerialize(writer, count);
                foreach (FieldInfo fi in fields)
                {
                    SerializationScenarioAttribute attr = fi.GetCustomAttributes <SerializationScenarioAttribute>().FirstOrDefault(x => x.Scenario == scenario && x.Case == @case);
                    if (attr != null)
                    {
                        ISerializationScenario wrapper = BinarySerializer.Scenarios.Find(x => x.Name == scenario);
                        MethodInfo             method  = wrapper.GetType().GetTypeInfo().GetDeclaredMethod("Wrap").MakeGenericMethod(fi.FieldType, attr.WrappedType);
                        object field = method.Invoke(wrapper, new object[] { fi.GetValue(value), @case });
                        result &= BinarySerializer.TrySerialize(writer, attr.WrappedType, field);
                    }
                    else
                    {
                        result &= BinarySerializer.TrySerialize(writer, fi.FieldType, fi.GetValue(value));
                    }
                }
                foreach (PropertyInfo pi in properties)
                {
                    SerializationScenarioAttribute attr = pi.GetCustomAttributes <SerializationScenarioAttribute>().FirstOrDefault(x => x.Scenario == scenario && x.Case == @case);
                    if (attr != null)
                    {
                        ISerializationScenario wrapper = BinarySerializer.Scenarios.Find(x => x.Name == scenario);
                        MethodInfo             method  = wrapper.GetType().GetTypeInfo().GetDeclaredMethod("Wrap").MakeGenericMethod(pi.PropertyType, attr.WrappedType);
                        object field = method.Invoke(wrapper, new object[] { pi.GetValue(value), @case });
                        result &= BinarySerializer.TrySerialize(writer, attr.WrappedType, field);
                    }
                    else
                    {
                        result &= BinarySerializer.TrySerialize(writer, pi.PropertyType, pi.GetValue(value));
                    }
                }
                break;

            default:
            case ObjectFlags.IsNull:
                break;
            }
            return(result);
        }
Example #2
0
        public bool TryDeserialize(BinaryReader reader, Type type, out object value, string scenario = default, string @case = default)
        {
            bool        result = true;
            ObjectFlags flag   = ObjectFlags.None;

            if (!type.GetTypeInfo().IsValueType)
            {
                result &= BinarySerializer.TryDeserialize(reader, out flag);
            }
            switch (flag)
            {
            case ObjectFlags.None:
                try
                {
                    object obj = Activator.CreateInstance(type);
                    IEnumerable <FieldInfo>    fields     = type.GetTypeInfo().DeclaredFields.Where(x => FieldSearcher(x, scenario, @case));
                    IEnumerable <PropertyInfo> properties = type.GetTypeInfo().DeclaredProperties.Where(x => PropertySearcher(x, scenario, @case));
                    result &= BinarySerializer.TryDeserialize(reader, out uint count);
                    if (count == fields.Count() + properties.Count())
                    {
                        foreach (FieldInfo fi in fields)
                        {
                            object field;
                            SerializationScenarioAttribute attr = fi.GetCustomAttributes <SerializationScenarioAttribute>().FirstOrDefault(x => x.Scenario == scenario && x.Case == @case);
                            if (attr != null)
                            {
                                ISerializationScenario wrapper = BinarySerializer.Scenarios.Find(x => x.Name == scenario);
                                MethodInfo             method  = wrapper.GetType().GetTypeInfo().GetDeclaredMethod("Unwrap").MakeGenericMethod(attr.WrappedType, fi.FieldType);
                                result &= BinarySerializer.TryDeserialize(reader, attr.WrappedType, out field);
                                field   = method.Invoke(wrapper, new object[] { field, @case });
                            }
                            else
                            {
                                result &= BinarySerializer.TryDeserialize(reader, fi.FieldType, out field);
                            }
                            fi.SetValue(obj, field);
                        }
                        foreach (PropertyInfo pi in properties)
                        {
                            object field;
                            SerializationScenarioAttribute attr = pi.GetCustomAttributes <SerializationScenarioAttribute>().FirstOrDefault(x => x.Scenario == scenario && x.Case == @case);
                            if (attr != null)
                            {
                                ISerializationScenario wrapper = BinarySerializer.Scenarios.Find(x => x.Name == scenario);
                                MethodInfo             method  = wrapper.GetType().GetTypeInfo().GetDeclaredMethod("Unwrap").MakeGenericMethod(attr.WrappedType, pi.PropertyType);
                                result &= BinarySerializer.TryDeserialize(reader, attr.WrappedType, out field);
                                field   = method.Invoke(wrapper, new object[] { field, @case });
                            }
                            else
                            {
                                result &= BinarySerializer.TryDeserialize(reader, pi.PropertyType, out field);
                            }
                            pi.SetValue(obj, field);
                        }
                        if (result)
                        {
                            value = obj;
                        }
                        else
                        {
                            value = null;
                        }
                    }
                    else
                    {
                        value  = null;
                        result = false;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;

            default:
            case ObjectFlags.IsNull:
                value  = null;
                result = true;
                break;
            }
            return(result);
        }