如何从文件中解析通用数据类型?

发布于 2024-11-08 22:42:41 字数 659 浏览 0 评论 0原文

我有一个通用类,如下所示:

class myClass<T>
{
    public T[] m_SomeData;
}

我想实现一个通用方法来从文件中读取数据并填充此类的数据字段。类似于:

class myClass<T>
{
    public T[] m_SomeData;

    public void ReadData(string fileName);
}

ReadData 方法的实现看起来像这样(为简洁起见,删除了所有错误检查):

void ReadData(string fileName)
{
   TextReader rdr = new StreamReader(fileName);

   string line = rdr.ReadLine();
   // Here I need to parse value of type T from the line
   // and initialize the m_SomeData array  
   // How do I do that? I would like to keep it generic if possible
}

注意,我可以保证类型 T 是数字,至少按照惯例

I have a generic class as follows:

class myClass<T>
{
    public T[] m_SomeData;
}

I want to implement a generic method to read data from a file and populate the data fields of this class. Something like:

class myClass<T>
{
    public T[] m_SomeData;

    public void ReadData(string fileName);
}

An implementation of the ReadData methods looks something like this (all error checking removed for brevity):

void ReadData(string fileName)
{
   TextReader rdr = new StreamReader(fileName);

   string line = rdr.ReadLine();
   // Here I need to parse value of type T from the line
   // and initialize the m_SomeData array  
   // How do I do that? I would like to keep it generic if possible
}

Note, I can guarantee the type T is numeric, at least by convention

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-11-15 22:42:41

更新:OP想要人类可读的输出。我建议 JavaScriptSerializer,然后,在:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Web.Extensions.dll

// Serialize:
using (var fs = new FileStream(fileName, FileMode.Create))
using (var writer = new StreamWriter(fs))
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string s = serializer.Serialize(m_SomeData);                
    writer.Write(s);
}

// Deserialize:
using (var fs = new FileStream(fileName, FileMode.Open))
using (var reader = new StreamReader(fs))
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var s = reader.ReadToEnd();
    m_SomeData = serializer.Deserialize<T[]>(s);                
}

旧答案:
这是 BinaryFormatter

using (FileStream fs = new FileStream(fileName, FileMode.Open)) 
{
    BinaryFormatter formatter = new BinaryFormatter();
    m_SomeData = (T[])formatter.Deserialize(fs);
}

这当然假设您还使用它通过 formatter.Serialize(fs, m_SomeData); 进行序列化。

Update: OP would like human readable output. I would suggest JavaScriptSerializer, then, in:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Web.Extensions.dll

// Serialize:
using (var fs = new FileStream(fileName, FileMode.Create))
using (var writer = new StreamWriter(fs))
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string s = serializer.Serialize(m_SomeData);                
    writer.Write(s);
}

// Deserialize:
using (var fs = new FileStream(fileName, FileMode.Open))
using (var reader = new StreamReader(fs))
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var s = reader.ReadToEnd();
    m_SomeData = serializer.Deserialize<T[]>(s);                
}

Old Answer:
This is a job for BinaryFormatter:

using (FileStream fs = new FileStream(fileName, FileMode.Open)) 
{
    BinaryFormatter formatter = new BinaryFormatter();
    m_SomeData = (T[])formatter.Deserialize(fs);
}

This of course assumes you are also using it to serialize via formatter.Serialize(fs, m_SomeData);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文