示例#1
0
        private static void SaveInstance(object instance, string path, StreamWriter streamWriter)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

            foreach (PropertyDescriptor item in properties)
            {
                if (item.IsBrowsable && item.ShouldSerializeValue(instance) && item.SerializationVisibility != 0)
                {
                    if (item.SerializationVisibility == DesignerSerializationVisibility.Content)
                    {
                        object value   = item.GetValue(instance);
                        string newPath = InstanceIO.GetNewPath(path, ".", item.Name);
                        if (value is System.Collections.CollectionBase)
                        {
                            InstanceIO.SaveInstance(value, newPath, streamWriter);
                        }
                        else
                        {
                            InstanceIO.SaveInstance(value, newPath, streamWriter);
                        }
                    }
                    if (!item.IsReadOnly && (!item.PropertyType.IsClass || !(item.PropertyType != typeof(string))))
                    {
                        InstanceIO.WriteProperty(instance, item, path, streamWriter);
                    }
                }
            }
            if (instance is IList)
            {
                IList list = instance as IList;
                streamWriter.WriteLine(path + " = LoadingBegin");
                for (int i = 0; i < list.Count; i++)
                {
                    object obj      = list[i];
                    string fullName = obj.GetType().FullName;
                    streamWriter.WriteLine(path + " = new " + fullName);
                    InstanceIO.SaveInstance(obj, InstanceIO.GetNewPath(path, "", "(" + i.ToString() + ")"), streamWriter);
                }
                streamWriter.WriteLine(path + " = LoadingEnd");
            }
        }
示例#2
0
 public static void SavePropertiesToStream(object instance, StreamWriter streamWriter)
 {
     InstanceIO.SaveInstance(instance, "", streamWriter);
 }