Serialize a Static Class?

后端 未结 6 1167
耶瑟儿~
耶瑟儿~ 2020-11-27 06:25

What happens if we serialize a static class? Can more than one instance of the static class be created if we serialize it?

[Serializable]
public static class         


        
6条回答
  •  一生所求
    2020-11-27 07:06

    You may create the following class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Reflection;
    using System.IO;
    
    namespace SerializeStatic_NET
    {
        public class SerializeStatic
        {
            public static bool Save(Type static_class, string filename)
            {
                try
                {
                    FieldInfo[] fields = static_class.GetFields(BindingFlags.Static | BindingFlags.Public);
                    object[,] a = new object[fields.Length,2];
                    int i = 0;
                    foreach (FieldInfo field in fields)
                    {
                        a[i, 0] = field.Name;
                        a[i, 1] = field.GetValue(null);
                        i++;
                    };
                    Stream f = File.Open(filename, FileMode.Create);
                    SoapFormatter formatter = new SoapFormatter();                
                    formatter.Serialize(f, a);
                    f.Close();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            public static bool Load(Type static_class, string filename)
            {
                try
                {
                    FieldInfo[] fields = static_class.GetFields(BindingFlags.Static | BindingFlags.Public);                
                    object[,] a;
                    Stream f = File.Open(filename, FileMode.Open);
                    SoapFormatter formatter = new SoapFormatter();
                    a = formatter.Deserialize(f) as object[,];
                    f.Close();
                    if (a.GetLength(0) != fields.Length) return false;
                    int i = 0;
                    foreach (FieldInfo field in fields)
                    {
                        if (field.Name == (a[i, 0] as string))
                        {
                            field.SetValue(null, a[i,1]);
                        }
                        i++;
                    };                
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
    

    You must define a reference to System.Runtime.Serialization.Formatters.Soap.

    Say, in your program you want to save the following static class:

    public static class A
    {
        public static string s;
        public static int i;
        public static double z;
    }
    

    You may use the following code:

    bool ok = SerializeStatic.Save(typeof(A), "c:\\tests\\a.dat");
    

    If you want to load the saved data (in the same program or in another program), use the following code:

    bool ok2 = SerializeStatic.Load(typeof(A), "c:\\tests\\a.dat");
    

    The fields A.s, A.i, A.z will get the new, loaded values.

提交回复
热议问题