基于文本文件(C#)定义项目
我正在设置一个类似于这样的项目列表基于文本文件(C#)定义项目,c#,file,text,macros,C#,File,Text,Macros,我正在设置一个类似于这样的项目列表 List<BankInfo> all_branches = new List<BankInfo>(); Equipment.set_slot = "Mail"; all_branches.Add(new BankInfo { name = "West Bank", city = "San Francisco", owner = new Person { name = "Jeff Bridges", age = 5
List<BankInfo> all_branches = new List<BankInfo>();
Equipment.set_slot = "Mail";
all_branches.Add(new BankInfo
{
name = "West Bank",
city = "San Francisco",
owner = new Person { name = "Jeff Bridges", age = 55 }
});
all_branches.Add(new BankInfo
{
name = "East Bank",
city = "Concord",
owner = new Person { name = "Upton Sinclair", age = 102 }
});
有没有办法做这样的事
至少(在c#中)有什么方法可以使像$ITEM这样的符号成为所有分支。添加(new BankInfo{这样我就可以做$ITEM(像c++中的宏)?我理解你的意思是一个处理所有属性分配的函数:
private List<BankInfo> addToBankInfo(string name, string city, string owner_name, int owner_age, List<BankInfo> all_branches)
{
return all_branches.Add(new BankInfo { name = name, city = city, owner = new Person { name = owner_name, age = owner_age } });
}
private List addToBankInfo(字符串名称、字符串城市、字符串所有者名称、整数所有者年龄、列出所有分支)
{
返回所有分支机构.Add(newbankinfo{name=name,city=city,owner=newperson{name=owner\u name,age=owner\u age});
}
你可以称之为:
List<BankInfo> all_branches = new List<BankInfo>();
Equipment.set_slot = "Mail";
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader("input_file.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if(line != null && line.Trim().Length > 0 && line.Contains(","))
{
string[] temp = line.Split(',');
if(temp.Length >= 4)
{
all_branches = addToBankInfo(temp[0], temp[1], temp[2], Convert.ToInt32(temp[3]), all_branches);
}
}
}
}
}
catch
{
}
列出所有分支=新建列表();
device.set_slot=“Mail”;
尝试
{
使用(System.IO.StreamReader sr=new System.IO.StreamReader(“input_file.txt”))
{
弦线;
而((line=sr.ReadLine())!=null)
{
如果(line!=null&&line.Trim().Length>0&&line.Contains(“,”))
{
string[]temp=line.Split(',');
如果(温度长度>=4)
{
所有分支=addToBankInfo(临时[0]、临时[1]、临时[2]、转换为32(临时[3]),所有分支;
}
}
}
}
}
抓住
{
}
在上面的示例中,我假设所有输入都在一个TXT文件中,用逗号分隔。如果您提供有关确切输入格式的更多信息,我可以相应地更新代码。从文本或XML文件中读取所有这些数据,然后编写一个函数来完成所有这些工作。啊!我真傻,我忘了您当然可以简单地从文本文件中读取中的信息。感谢帮助:)。没问题。这就是此网站的用途:)
List<BankInfo> all_branches = new List<BankInfo>();
Equipment.set_slot = "Mail";
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader("input_file.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if(line != null && line.Trim().Length > 0 && line.Contains(","))
{
string[] temp = line.Split(',');
if(temp.Length >= 4)
{
all_branches = addToBankInfo(temp[0], temp[1], temp[2], Convert.ToInt32(temp[3]), all_branches);
}
}
}
}
}
catch
{
}