示例#1
0
        public static void DeserializeFields(IniReader reader, object ret, String prefix = "", EventHandler <OnSerializeNotificationEventArgs> OnSerializingMember = null)
        {
            Type t = ret.GetType();

            foreach (var field in t.GetFields())
            {
                Object FieldValue = null;
                OnSerializeNotificationEventArgs OnSerializeArgs = new OnSerializeNotificationEventArgs();
                OnSerializeArgs.Reader = reader;
                OnSerializeArgs.Field  = field;
                OnSerializeArgs.Target = ret;
                if (field.IsLiteral)
                {
                    continue;
                }
                NonSerializedAttribute[] nonSerialize = (NonSerializedAttribute[])field.GetCustomAttributes(typeof(NonSerializedAttribute), false);
                if (nonSerialize != null && nonSerialize.Length > 0)
                {
                    continue;
                }
                if (field.IsPublic)
                {
                    var    fieldType = field.FieldType;
                    String name      = prefix + field.Name;
                    IniFieldNameAttribute iniFieldName = (IniFieldNameAttribute)field.GetCustomAttribute(typeof(IniFieldNameAttribute), true);
                    if (iniFieldName != null && !String.IsNullOrEmpty(iniFieldName.Name))
                    {
                        name = iniFieldName.Name;
                    }
                    OnSerializeArgs.FullName = name;

                    if (fieldType.IsPrimitive)
                    {
                        if (fieldType == typeof(int))
                        {
                            int val = reader.GetInt(name);
                            field.SetValue(ret, val);
                            FieldValue = val;
                        }
                        else if (fieldType == typeof(bool))
                        {
                            bool val = reader.GetBoolean(name);
                            field.SetValue(ret, val);
                            FieldValue = val;
                        }
                        else if (fieldType == typeof(double))
                        {
                            double val = reader.GetDouble(name);
                            field.SetValue(ret, val);
                            FieldValue = val;
                        }
                        else if (fieldType.IsEnum)
                        {
                            try
                            {
                                String sval    = reader.GetString(name);
                                object enumVal = Enum.Parse(fieldType, sval);
                                field.SetValue(ret, enumVal);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    else if (fieldType == typeof(string))
                    {
                        object val = reader.GetString(name);
                        field.SetValue(ret, val);
                        FieldValue = val;
                    }
                    else if (fieldType == typeof(double[]))
                    {
                        String        val     = reader.GetString(name);
                        List <double> intList = DoubleListFromString(val);
                        field.SetValue(ret, intList.ToArray());
                        FieldValue = intList.ToArray();
                    }
                    else if (fieldType == typeof(int[]))
                    {
                        String     val     = reader.GetString(name);
                        List <int> intList = IntListFromString(val);
                        field.SetValue(ret, intList.ToArray());
                        FieldValue = intList.ToArray();
                    }
                    else if (fieldType == typeof(Color))
                    {
                        String     val     = reader.GetString(name);
                        List <int> intList = IntListFromString(val);
                        int        a       = 0;
                        int        r       = 0;
                        int        g       = 0;
                        int        b       = 0;
                        if (intList.Count == 4)
                        {
                            a = intList[0];
                            r = intList[1];
                            g = intList[2];
                            b = intList[3];
                            Color clrVal = Color.FromArgb(a, r, g, b);
                            field.SetValue(ret, clrVal);
                            FieldValue = clrVal;
                        }
                        else if (intList.Count == 3)
                        {
                            r = intList[0];
                            g = intList[1];
                            b = intList[2];
                            Color clrVal = Color.FromArgb(255, r, g, b);
                            field.SetValue(ret, clrVal);
                            FieldValue = clrVal;
                        }
                    }
                    else if (fieldType == typeof(Size))
                    {
                        String     val     = reader.GetString(name);
                        List <int> intList = IntListFromString(val);
                        int        w       = 0;
                        int        h       = 0;
                        if (intList.Count == 2)
                        {
                            w = intList[0];
                            h = intList[1];
                            field.SetValue(ret, new Size(w, h));
                        }
                        FieldValue = val;
                    }
                    else if (fieldType.IsArray && fieldType.GetElementType().IsClass)
                    {
                        // for case of
                        // SomeThing.Count=2
                        // SomeThing
                        FlattenArrayLengthName arrayLengthName = (FlattenArrayLengthName)field.GetCustomAttribute(typeof(FlattenArrayLengthName), true);
                        if (arrayLengthName != null && !String.IsNullOrEmpty(arrayLengthName.Name))
                        {
                            Type             elementType   = fieldType.GetElementType();
                            Array            arrayInstance = Array.CreateInstance(elementType, reader.GetInt(arrayLengthName.Name));
                            FlattenArrayName arrayName     = (FlattenArrayName)field.GetCustomAttribute(typeof(FlattenArrayName), true);
                            for (int i = 0; i < arrayInstance.Length; ++i)
                            {
                                String flattenArrayName = name + "[" + i.ToString() + "].";
                                if (arrayName != null && !String.IsNullOrEmpty(arrayName.Name) && !String.IsNullOrEmpty(arrayName.Replacement))
                                {
                                    flattenArrayName = arrayName.Name.Replace(arrayName.Replacement, i.ToString()) + ".";
                                }
                                var    constructor  = elementType.GetConstructor(new Type[] { });
                                object fieldContent = null;
                                fieldContent = field.GetValue(ret);
                                if (constructor != null)
                                {
                                    fieldContent = constructor.Invoke(new Object[] { });
                                    arrayInstance.SetValue(fieldContent, i);
                                }
                                DeserializeFields(reader, arrayInstance.GetValue(i), flattenArrayName, OnSerializingMember);
                            }
                            field.SetValue(ret, arrayInstance);
                        }
                    }
                    else if (fieldType == typeof(Rectangle))
                    {
                        String     val     = reader.GetString(name);
                        List <int> intList = IntListFromString(val);
                        int        x       = 0;
                        int        y       = 0;
                        int        w       = 0;
                        int        h       = 0;
                        if (intList.Count == 4)
                        {
                            x = intList[0];
                            y = intList[1];
                            w = intList[2];
                            h = intList[3];
                            field.SetValue(ret, new Rectangle(x, y, w, h));
                        }
                        FieldValue = val;
                    }
                    else if (fieldType == typeof(Point))
                    {
                        String     val     = reader.GetString(name);
                        List <int> intList = IntListFromString(val);
                        int        x       = 0;
                        int        y       = 0;
                        if (intList.Count == 2)
                        {
                            x = intList[0];
                            y = intList[1];
                            field.SetValue(ret, new Point(x, y));
                        }
                        FieldValue = val;
                    }
                    else if (fieldType.IsEnum)
                    {
                        try
                        {
                            String sval    = reader.GetString(name);
                            object enumVal = Enum.Parse(fieldType, sval);
                            field.SetValue(ret, enumVal);
                            FieldValue = sval;
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else if (fieldType.IsClass)
                    {
                        var    constructor  = fieldType.GetConstructor(new Type[] { });
                        object fieldContent = null;
                        fieldContent = field.GetValue(ret);
                        if (fieldContent == null)
                        {
                            if (constructor != null)
                            {
                                fieldContent = constructor.Invoke(new Object[] { });
                                field.SetValue(ret, fieldContent);
                            }
                        }

                        FieldValue = fieldContent;
                        if (fieldContent != null)
                        {
                            DeserializeFields(reader, fieldContent, field.Name + ".", OnSerializingMember);
                        }
                    }
                    OnSerializeArgs.FieldValue = FieldValue;
                    if (reader.FieldSectionMapper.ContainsKey(name))
                    {
                        OnSerializeArgs.Section = reader.FieldSectionMapper[name];
                    }
                    if (OnSerializingMember != null)
                    {
                        OnSerializingMember(reader, OnSerializeArgs);
                    }
                }
            }
        }
示例#2
0
        private void SerializeObject(object ret, String prefix)
        {
            Type t = ret.GetType();

            if (t == typeof(DBNull))
            {
                return;
            }
            foreach (var field in t.GetFields())
            {
                try
                {
                    if (field.IsSpecialName)
                    {
                        continue;
                    }

                    if (field.IsPublic)
                    {
                        var fieldType = field.FieldType;
                        if (fieldType == typeof(DBNull))
                        {
                            continue;
                        }
                        String name = prefix + field.Name;
                        object val  = field.GetValue(ret);
                        NonSerializedAttribute[] nonSerialize = (NonSerializedAttribute[])field.GetCustomAttributes(typeof(NonSerializedAttribute), false);
                        if (nonSerialize != null && nonSerialize.Length > 0)
                        {
                            continue;
                        }
                        DescriptionAttribute[] attrs = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (attrs != null)
                        {
                            for (int i = 0; i < attrs.Length; ++i)
                            {
                                WriteComment(attrs[i].Description);
                            }
                        }
                        if (GivenValue != null && GivenValue.ContainsKey(name))
                        {
                            val = GivenValue[name];
                        }
                        if (fieldType.IsPrimitive)
                        {
                            if (fieldType == typeof(int))
                            {
                                Write(name, (int)val);
                            }
                            else if (fieldType == typeof(bool))
                            {
                                Write(name, (bool)val);
                            }
                            else if (fieldType == typeof(double))
                            {
                                Write(name, (double)val);
                            }
                            else if (fieldType.IsEnum)
                            {
                                Write(name, (string)val);
                            }
                        }
                        else if (fieldType == typeof(string))
                        {
                            Write(name, (String)val);
                        }
                        else if (fieldType == typeof(int[]))
                        {
                            Write(name, (int[])val);
                        }
                        else if (fieldType == typeof(double[]))
                        {
                            Write(name, (double[])val);
                        }
                        else if (fieldType == typeof(Color))
                        {
                            Write(name, (Color)val);
                        }
                        else if (fieldType == typeof(Size))
                        {
                            Write(name, (Size)val);
                        }
                        else if (fieldType.IsArray && fieldType.GetElementType().IsClass)
                        {
                            // for case of
                            // SomeThing.Count=2
                            // SomeThing
                            FlattenArrayLengthName arrayLengthName = (FlattenArrayLengthName)field.GetCustomAttribute(typeof(FlattenArrayLengthName), true);
                            if (arrayLengthName != null && !String.IsNullOrEmpty(arrayLengthName.Name))
                            {
                                Type  elementType   = fieldType.GetElementType();
                                Array arrayInstance = (Array)val;
                                Write(arrayLengthName.Name, arrayInstance.Length);

                                FlattenArrayName arrayName = (FlattenArrayName)field.GetCustomAttribute(typeof(FlattenArrayName), true);
                                for (int i = 0; i < arrayInstance.Length; ++i)
                                {
                                    String flattenArrayName = name + "[" + i.ToString() + "].";
                                    if (arrayName != null && !String.IsNullOrEmpty(arrayName.Name) && !String.IsNullOrEmpty(arrayName.Replacement))
                                    {
                                        flattenArrayName = arrayName.Name.Replace(arrayName.Replacement, i.ToString()) + ".";
                                    }
                                    SerializeObject(arrayInstance.GetValue(i), flattenArrayName);
                                }
                            }
                        }
                        else if (fieldType == typeof(Rectangle))
                        {
                            Write(name, (Rectangle)val);
                        }
                        else if (fieldType == typeof(Point))
                        {
                            Write(name, (Point)val);
                        }
                        else if (fieldType.IsEnum)
                        {
                            Write(name, (string)val.ToString());
                        }
                        else if (fieldType.IsClass)
                        {
                            if (fieldType == typeof(Color))
                            {
                                Write(name, (Color)val);
                            }
                            else if (fieldType == typeof(Size))
                            {
                                Write(name, (Size)val);
                            }
                            else if (fieldType == typeof(Rectangle))
                            {
                                Write(name, (Rectangle)val);
                            }
                            else if (fieldType == typeof(Point))
                            {
                                Write(name, (Point)val);
                            }
                            object fieldContent = null;
                            fieldContent = field.GetValue(ret);
                            if (fieldContent != null)
                            {
                                WriteComment("Class:" + fieldType.Name + Environment.NewLine +
                                             "FieldName:" + field.Name + Environment.NewLine);
                                SerializeObject(fieldContent, field.Name + ".");
                            }
                        }
                    }
                }
                catch (Exception ee)
                {
                }
            }
        }