예제 #1
0
        public void CompressOrDecompress(object name, ActionMode mode)
        {
            if (mode == ActionMode.Compress)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (FileStream fs = new FileStream(@"D:\people.dat", FileMode.OpenOrCreate))
                {
                    formatter.Serialize(fs, name);

                    Console.WriteLine("Объект сериализован");
                }
            }

            if (mode == ActionMode.Decompress)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (FileStream fs = new FileStream(@"D:\people.dat", FileMode.OpenOrCreate))
                {
                    Biography newPerson = (Biography)formatter.Deserialize(fs);

                    Console.WriteLine("Объект десериализован");
                    Console.WriteLine(newPerson.ToString());
                }
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            //pulling out the attribute
            Type name = typeof(Program);

            object[] atrrs = name.GetCustomAttributes(false);

            //getting attribute values
            foreach (PathAttribute atrr in atrrs)
            {
                //directory path
                string   dirName = atrr.Path;
                string[] files   = Directory.GetFiles(dirName);

                //I get files from the directory
                foreach (var item in files)
                {
                    Console.WriteLine(item);

                    //I connect the library
                    Assembly asm = Assembly.LoadFile(item);
                    Console.WriteLine(asm.FullName);

                    //I indicate my class
                    Type type = asm.GetType("ClassLibrary1.Class1");

                    Type[] types = asm.GetTypes();

                    foreach (Type t in types)
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.WriteLine(t.Name);
                        Console.ResetColor();
                    }
                    object obj = Activator.CreateInstance(type);

                    MethodInfo method = type.GetMethod("GetBiography");

                    //calling method
                    method.Invoke(obj, new object[] { });
                    Console.WriteLine();
                    Console.WriteLine();
                }

                Biography my = new Biography("Vadim", "Bezhkov", "Alexandrovich",
                                             new DateTime(1988, 12, 31), "Pinsk");

                while (true)
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("Hello Friend");
                    Console.WriteLine("Press 1 to serializable");
                    Console.WriteLine("Press 2 to deserializable");
                    Console.WriteLine("Press 3 to exit this programm");
                    Console.ResetColor();

                    Serializable op;
                    Enum.TryParse(Console.ReadLine(), out op);

                    switch (op)
                    {
                    case Serializable.serializable:

                        Console.Clear();
                        my.CompressOrDecompress(my, ActionMode.Compress);
                        Console.WriteLine();

                        break;

                    case Serializable.deserializable:
                        Console.Clear();
                        my.CompressOrDecompress(my, ActionMode.Decompress);
                        Console.WriteLine();

                        break;

                    case Serializable.exit:
                        Environment.Exit(0);
                        break;
                    }
                }
            }
        }