需要帮助请............ [英] Need Help Please............

查看:65
本文介绍了需要帮助请............的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是在给定以下类的示例的情况下,如何将静态列表中的值序列化为.dat文件.请以示例代码回复


My question is how do i serialize the values in my static list to a .dat file given the sample of my classes below. Please reply with example codes thanks


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JustMe
{
    [Serializable]
   public class FITS
    {
        
        static List<Fitness> fitness;
        
        static FITS()
        {
            fitness = new List<Fitness>();
        }
        
        public void  AddFitness(Fitness addF)
        {
            
            for (int i = 0; i < fitness.Count; i++)
            {
                if (fitness[i].Id.Equals(addF.Id) == true)
                {
                    ExceptionHandler error = new ExceptionHandler(addF.Id);
                    throw (error);
                }

            }
          
            fitness.Add(addF);

        }
        
        public  void DeleteFitness(Fitness deleteF)
        {
            fitness.Remove(deleteF);
        }

        
        public  void SelectSort()
        {
            
            int smallest;

            for (int i = 0; i < fitness.Count - 1; i++)
            {
                smallest = i;
                
                for (int index = i + 1; index < fitness.Count; index++)
                {

                    if (fitness[index].Id.CompareTo(fitness[smallest].Id) < 0)
                    {
                        smallest = index;
                    }
                }
                
                var temp = fitness[i];
                fitness[i] = fitness[smallest];
                fitness[smallest] = temp;

            }

        }
        
        public  void SelectSort1()
        {
            
            int smallest1;

            for (int i = 0; i < fitness.Count - 1; i++)
            {
                smallest1 = i;
                
                for (int index1 = i + 1; index1 < fitness.Count; index1++)
                {

                    if (fitness[smallest1].Id.CompareTo(fitness[index1].Id) < 0)
                    {
                        smallest1 = index1;
                    }
                }
                
                var temp1 = fitness[i];
                fitness[i] = fitness[smallest1];
                fitness[smallest1] = temp1;

            }

        }

        public  void clear()
        {
            fitness.Clear();
        }

        public Fitness getFitnessClass(int index)
        {
            return fitness[index];
        }

        public  int Count()
        {
            return fitness.Count;
        }
    }
}




上面是我的课程,保存我的列表,而下面是我的课程,它将列表中的值序列化为.dat文件.




above is my class that holds my list while below is my class the serializes values in the list to a .dat file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace JustMe
{
   public class SerialiseHandler
    {
        public void WriteListToFile(string filePath)
        {
            FileStream outFile;
            BinaryFormatter bFormatter = new BinaryFormatter();
            string newpath = HttpContext.Current.Server.MapPath(filePath);
            
            outFile = new FileStream(newpath, FileMode.Create, FileAccess.Write);

            
            bFormatter.Serialize(outFile, FITS.GetValues());

            
            outFile.Close();
        }

       
        public FITS ReadListFromFile(String filePath)
        {
            FileStream inFile;
            BinaryFormatter bFormatter = new BinaryFormatter();
            FITS fits = new FITS();
            string newpath = HttpContext.Current.Server.MapPath(filePath);
            
            inFile = new FileStream(newpath, FileMode.Open, FileAccess.Read);

            
            fits = (FITS)bFormatter.Deserialize(inFile);

            inFile.Close();
            return fits;
        }
    }
}

推荐答案

[Serializable]属性仅适用于实例公共属性,您不能将其用于静态成员.

您可以在类中实现ISerializable接口.这样,您可以完全控制要序列化哪些数据以及如何序列化,但是需要一些编码.

或者,考虑将"fitness"成员实现为单例而不是静态字段.
[Serializable] attribute works only on instance public properties, you can''t use it for static members.

You can implement ISerializable interface in your class. That way you have complete control of what data is serialized and how, but it takes some coding.

As an alternative, consider implementing ''fitness'' member as a singleton instead of static field.


这篇关于需要帮助请............的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆