Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#类、方法或解析配置文件的内容_C#_Parsing_Class_Methods_Call - Fatal编程技术网

c#类、方法或解析配置文件的内容

c#类、方法或解析配置文件的内容,c#,parsing,class,methods,call,C#,Parsing,Class,Methods,Call,对面向对象的做事方法还不熟悉,只是想知道做这件事的建议方法是什么 在我的第一个问题中,我问了关于解析文本文件的问题,现在有了一些好的想法(谢谢大家),我开始编写程序的这一部分 文本文件有多个部分和多个需要读入的数据类型。这一切进展顺利,随着文本文件复杂性的增加,我正在修改代码以适应。但我最终会得到30个左右不同的变量/数组/列表等,其中至少有一些变量需要全局可用 我现在有一段代码,目前是类中包含“main”的方法,所有变量都是全局变量。(我知道不太好) 我想一定有一种更简洁的方法来实现这一点,我

对面向对象的做事方法还不熟悉,只是想知道做这件事的建议方法是什么

在我的第一个问题中,我问了关于解析文本文件的问题,现在有了一些好的想法(谢谢大家),我开始编写程序的这一部分

文本文件有多个部分和多个需要读入的数据类型。这一切进展顺利,随着文本文件复杂性的增加,我正在修改代码以适应。但我最终会得到30个左右不同的变量/数组/列表等,其中至少有一些变量需要全局可用

我现在有一段代码,目前是类中包含“main”的方法,所有变量都是全局变量。(我知道不太好)

我想一定有一种更简洁的方法来实现这一点,我想使用一个单独的类,创建这个类的一个实例,并在这个类中调用解析文本和填充变量的方法。然后简单地从with调用我需要的任何其他类中的类变量(任何被认为是公共的)

但我知道什么:)我只是在寻找一些想法,这样我就不会走太远的路而不得不回头


干杯

如果配置文件的格式不是一成不变的,那么我建议使用System.Xml.XmlSerializer类将配置文件保存/加载为Xml

保存起来很简单。。负载将作为练习留给您:

using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.something, FileShare.something))
{
    XmlSerializer serializer = new XmlSerializer(this.getType());
    serializer.serialize(file, this);
}

我可能搞错了参数的顺序,我是一个vb的家伙,但你明白了。

如果配置文件的格式不是一成不变的,那么我建议使用System.Xml.XmlSerializer类将配置文件保存/加载为Xml

保存起来很简单。。负载将作为练习留给您:

using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.something, FileShare.something))
{
    XmlSerializer serializer = new XmlSerializer(this.getType());
    serializer.serialize(file, this);
}

我可能搞错了参数的顺序,我是一个vb的家伙,但你明白了。

为了简单起见,我会使用类来完成:

以下是配置类的示例:

[Serializable]
public class Config
{
    public int ValueOne { get; set; }
    public string ValueTwo { get; set; }
    public int[] LotsOfValues { get; set; }
    public List<FurtherConfig> Other { get; set; }
}

[Serializable]
public class FurtherConfig 
{
    public int Value { get; set; }
}
[可序列化]
公共类配置
{
public int ValueOne{get;set;}
公共字符串ValueTwo{get;set;}
public int[]LotsOfValues{get;set;}
公共列表其他{get;set;}
}
[可序列化]
公共类配置
{
公共int值{get;set;}
}
下面是一些示例用法:

var config = new Config
{
    ValueOne = 23,
    ValueTwo = "Second Value",
    LotsOfValues = new[] { 23, 34, 34 },
    Other = new List<FurtherConfig> {
        new FurtherConfig { Value = 567},
        new FurtherConfig { Value = 98 }
    }
};

var serializer = new XmlSerializer(typeof(Config));

//write out to file
using (var fw = File.OpenWrite("config.xml"))
    serializer.Serialize(fw, config);

//read in from file
Config newConfig;
using (var fr = File.OpenRead("config.xml"))
    newConfig = serializer.Deserialize(fr) as Config;

Console.WriteLine(newConfig.Other[1].Value); //prints 98
var-config=new-config
{
ValueOne=23,
ValueTwo=“第二个值”,
LotsOfValues=new[]{23,34,34},
其他=新列表{
新的配置{Value=567},
新配置{Value=98}
}
};
var serializer=新的XmlSerializer(typeof(Config));
//写入文件
使用(var fw=File.OpenWrite(“config.xml”))
serializer.Serialize(fw,config);
//从文件读入
配置newConfig;
使用(var fr=File.OpenRead(“config.xml”))
newConfig=serializer.Deserialize(fr)为Config;
Console.WriteLine(newConfig.Other[1].Value)//印刷品98
这是序列化输出:

<?xml version="1.0"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ValueOne>23</ValueOne>
  <ValueTwo>Second Value</ValueTwo>
  <LotsOfValues>
    <int>23</int>
    <int>34</int>
    <int>34</int>
  </LotsOfValues>
  <Other>
    <FurtherConfig>
      <Value>567</Value>
    </FurtherConfig>
    <FurtherConfig>
      <Value>98</Value>
    </FurtherConfig>
  </Other>
</Config>

23
第二值
23
34
34
567
98

为了保持简单,我会使用类来完成:

以下是配置类的示例:

[Serializable]
public class Config
{
    public int ValueOne { get; set; }
    public string ValueTwo { get; set; }
    public int[] LotsOfValues { get; set; }
    public List<FurtherConfig> Other { get; set; }
}

[Serializable]
public class FurtherConfig 
{
    public int Value { get; set; }
}
[可序列化]
公共类配置
{
public int ValueOne{get;set;}
公共字符串ValueTwo{get;set;}
public int[]LotsOfValues{get;set;}
公共列表其他{get;set;}
}
[可序列化]
公共类配置
{
公共int值{get;set;}
}
下面是一些示例用法:

var config = new Config
{
    ValueOne = 23,
    ValueTwo = "Second Value",
    LotsOfValues = new[] { 23, 34, 34 },
    Other = new List<FurtherConfig> {
        new FurtherConfig { Value = 567},
        new FurtherConfig { Value = 98 }
    }
};

var serializer = new XmlSerializer(typeof(Config));

//write out to file
using (var fw = File.OpenWrite("config.xml"))
    serializer.Serialize(fw, config);

//read in from file
Config newConfig;
using (var fr = File.OpenRead("config.xml"))
    newConfig = serializer.Deserialize(fr) as Config;

Console.WriteLine(newConfig.Other[1].Value); //prints 98
var-config=new-config
{
ValueOne=23,
ValueTwo=“第二个值”,
LotsOfValues=new[]{23,34,34},
其他=新列表{
新的配置{Value=567},
新配置{Value=98}
}
};
var serializer=新的XmlSerializer(typeof(Config));
//写入文件
使用(var fw=File.OpenWrite(“config.xml”))
serializer.Serialize(fw,config);
//从文件读入
配置newConfig;
使用(var fr=File.OpenRead(“config.xml”))
newConfig=serializer.Deserialize(fr)为Config;
Console.WriteLine(newConfig.Other[1].Value)//印刷品98
这是序列化输出:

<?xml version="1.0"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ValueOne>23</ValueOne>
  <ValueTwo>Second Value</ValueTwo>
  <LotsOfValues>
    <int>23</int>
    <int>34</int>
    <int>34</int>
  </LotsOfValues>
  <Other>
    <FurtherConfig>
      <Value>567</Value>
    </FurtherConfig>
    <FurtherConfig>
      <Value>98</Value>
    </FurtherConfig>
  </Other>
</Config>

23
第二值
23
34
34
567
98

你的直觉很好;解析特定类型的文本文件的任务应该封装在一个类中,该类提供了有关从何处获取数据的一些信息,并生成表示解析数据的对象图。至于输入,一个带有文件名的字符串就可以了;更妙的是,在溪流或溪流阅读器中传递;然后,相同的解析器可以从文件、内存块或网络传输中提取信息

一定要把事情分解成可重复的部分;如果一个文件包含许多解析为子对象的类似行,请创建一个方法来解析一行并返回子对象


也要使事情尽可能的单一化;如果必须处理两种类型的文本文件,请为每种文件类型创建一个类,如果可能,使它们实现相同的接口。然后,您可以实现所谓的策略模式,其中一个对象的任务是根据具体情况确定应该使用一系列其他类中的哪一个来执行任务;解析特定类型的文本文件的任务应该封装在一个类中,该类提供了有关从何处获取数据的一些信息,并生成表示解析数据的对象图。至于输入,一个带有文件名的字符串就可以了;更妙的是,在溪流或溪流阅读器中传递;然后,相同的解析器可以从文件、内存块或网络传输中提取信息

一定要把事情分解成可重复的部分;如果一个文件包含许多解析为子对象的类似行,请创建一个方法来解析一行并返回子对象

也使