Exemplo n.º 1
0
        public DataReader(DynamicSerializer parent, Type type, Func <object> activator = null)
        {
            this.Parent = parent;
            ModelType   = type;
            if (type.IsEnum)
            {
                ReadFunction = ReadEnum;
                return;
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                if (type.GenericTypeArguments.Any())
                {
                    ReadFunction = ReadList;
                    ListType     = type.GenericTypeArguments.First();
                }
                else
                {
                    ReadFunction = ReadArray;
                    ListType     = type.GetElementType();
                }

                return;
            }

            Properties         = Parent.GetSerializableProperties(type);
            ReadFunction       = ReadPropertyValues;
            ActivationFunction = activator;
        }
Exemplo n.º 2
0
        public DataWriter(DynamicSerializer parent, Type type)
        {
            this.Parent = parent;
            if (type.IsEnum)
            {
                WriteAction = WriteEnum;
                return;
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                WriteAction = WriteEnumerable;

                if (type.GenericTypeArguments.Any())
                {
                    ListType = type.GenericTypeArguments.First();
                }
                else
                {
                    ListType = type.GetElementType();
                }

                return;
            }

            Properties  = Parent.GetSerializableProperties(type);
            WriteAction = WritePropertyValues;
        }