English 中文(简体)
履历
原标题:Querying serialized object file
  • 时间:2012-01-13 10:58:34
  •  标签:
  • c#
  • .net
  • linq

能否在不把整个档案装上传记的情况下实现这一点? 如果是,你建议我做什么?

班级实施:

[Serializable()]
public class Car
{
    public string Brand { get; set; }
    public string Model { get; set; }
}

[Serializable()]
public class CarCollection : List<Car>
{
}

存档:

CarCollection cars = new CarCollection
{
    new Cars{ Brand = "BMW", Model = "7.20" },
    new Cars{ Brand = "Mercedes", Model = "CLK" }
};

using (Stream stream = File.Open("data", FileMode.Create))
{
    BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(stream, cars);
}
最佳回答

为了在某个时候使一个物体脱轨,你还需要在某个时候将其序列化。

简单的方法是界定你自己的通用类别:

public static class StreamSerializer
{
    public static void Serialize<T>(IList<T> list, string filename)
    {
        using (Stream stream = File.Open(filename, FileMode.Create))
        {
            BinaryFormatter bin = new BinaryFormatter();

            // seralize each object separately
            foreach (var item in list)
                bin.Serialize(stream, item);
        }
    }

    public static IEnumerable<T> Deserialize<T>(string filename)
    {
        using (Stream stream = File.Open(filename, FileMode.Open))
        {
            BinaryFormatter bin = new BinaryFormatter();

            // deserialize each object separately, and 
            // return them one at a time

            while (stream.Position < stream.Length)
                yield return (T)bin.Deserialize(stream);
        }
    }
}

然后,你可以简单写:

CarsCollection cars = new CarsCollection
{
    new Cars{ Brand = "BMW", Model = "7.20" },
    new Cars{ Brand = "Mercedes", Model = "CLK" }
};

// note that you cannot serialize the entire list if
// you want to query without loading - it must be symmetrical

StreamSerializer.Serialize(cars, "data.bin");

// the following expression iterates through objects, processing one 
// at a time. "First" method is a good example because it
// breaks early.

var bmw = StreamSerializer
    .Deserialize<Cars>("data.bin")
    .First(c => c.Brand == "BMW");

A slightly more complex case might be if your CarsCollection belongs to a different class. In that case, you will need to implement ISerializable, but the principle is similar.

On a side note, usual convention is not to name entities in plural (i.e. Cars should be named Car).

问题回答

如果你向XML序列号,你可以使用SAX端ser(,将按顺序逐行读。

一般来说,你可以使用某种读者(StreamReader, BinaryReader,......)和BufferedStream。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签