C# 如何创建;“公共字符串数组”;从参数到数组?
我有以下代码:C# 如何创建;“公共字符串数组”;从参数到数组?,c#,
elasticsearch,arraylist,parameters,C#,
elasticsearch,Arraylist,Parameters,我有以下代码: public static void SendDataToES(String startTimestamp, String startDate, String bid_x, String ask_x) { var tem_ehm = new Pre_Market { timestamp = startTimestamp, date = startDate, bid = bid_x, ask = ask
public static void SendDataToES(String startTimestamp, String startDate, String bid_x, String ask_x)
{
var tem_ehm = new Pre_Market
{
timestamp = startTimestamp,
date = startDate,
bid = bid_x,
ask = ask_x
};
}
class Pre_Market
{
public string x_ric { get; set; }
public string ask { get; set; }
public string bid { get; set; }
public string date { get; set; }
public string timestamp { get; set; }
}
但在将来,它将具有数组形式的参数
public static void SendDataToES(字符串startTimestamp、字符串startDate、字符串bid_x、字符串ask_x、IList name abc、字符串[]getABCs)
哪个名称abc[]的值为A、B、C,而getABC[]的值为1、2、3,所以我想在类中创建Pre_Market作为数组
public string[] A { get; set;}
public string[] B { get; set;}
public string[] C { get; set;}
不确定下面是否正常工作
for ( int i = 0 ; i < nameABC.Count(); i++ )
{
public string[] nameABC[i] { get; set; }
}
更新!以下内容在我这边很好用。
var temp_ehm = new Pre_Market
{
timestamp = startTimestamp,
date = startDate,
fids = new Dictionary<string, string>(),
};
for (int i = 0; i < nameFIDs.Count() - 1; i++)
{
temp_ehm.fids.Add(nameFIDs[i], get_FIDs[i]);
}
var temp\u ehm=新的前期市场
{
时间戳=开始时间戳,
日期=开始日期,
fids=新字典(),
};
对于(int i=0;i
“临时ehm中的FID可以添加”这对我来说是新闻 如果您同意使用匿名类而不是
Pre_Market
,那么您可以用JSON构建对象结构(构建字符串很容易),然后使用JSON反序列化器(通过NuGet安装NewtonSoft.JSON
)将其转换为对象
下面是生成JSON字符串的方法
string jsonStr = "{ timestamp = \"" + startTimestamp + "\"," +
"date = \"" + startDate + "\"," +
"bid = \"" + bid_x + "\"," +
"ask = \"" + ask_x + "\"";
for ( int i = 0 ; i < nameABC.Count(); i++ )
{
jsonStr += "," + nameABC[i] + " = \"" + getABCs[i] + "\""
}
jsonStr += "}";
string jsonStr=“{timestamp=\”+startTimestamp+“\”,”+
“日期=\”“+startDate+”“,”+
“出价=\”“+bid\ux+”,”+
“询问=\”“+询问x+”\”;
对于(int i=0;i
一个简单的解决方案是使用字典
public static void SendDataToES(String startTimestamp, String startDate, String bid_x, String ask_x, IList<string> nameABC, string[] getABCs)
{
var tem_ehm = new Pre_Market
{
timestamp = startTimestamp,
date = startDate,
bid = bid_x,
ask = ask_x,
ABCs = new Dictionary<string, string>()
};
int i = 0;
foreach (string name in nameABCs)
tem_ehm.Add(name, getABCs[i++];
// access the ABCs like so:
string A = tem_ehm.ABCs["A"];
string B = tem_ehm.ABCs["B"];
string C = tem_ehm.ABCs["C"];
}
class Pre_Market
{
public string x_ric { get; set; }
public string ask { get; set; }
public string bid { get; set; }
public string date { get; set; }
public string timestamp { get; set; }
public Dictionary<string, string> ABCs { get; set; }
}
public static void SendDataToES(字符串startTimestamp、字符串startDate、字符串bid_x、字符串ask_x、IList nameABC、字符串[]getABCs)
{
var tem_ehm=新的预市场
{
时间戳=开始时间戳,
日期=开始日期,
bid=bid_x,
ask=ask_x,
ABCs=新字典()
};
int i=0;
foreach(nameABCs中的字符串名称)
tem_ehm.Add(名称,getABCs[i++];
//按如下方式访问ABC:
字符串A=tem_ehm.ABCs[“A”];
字符串B=tem_ehm.ABCs[“B”];
字符串C=tem_ehm.ABCs[“C”];
}
班前市场
{
公共字符串x_ric{get;set;}
公共字符串ask{get;set;}
公共字符串bid{get;set;}
公共字符串日期{get;set;}
公共字符串时间戳{get;set;}
公共字典ABCs{get;set;}
}
您可以用JSON构建对象结构(构建字符串很容易),然后使用JSON反序列化器(通过NuGet安装NewtonSoft.JSON)将其转换为一个对象。在这种情况下,您可以使用anonymos对象而不是定义的Pre\u-Market
Classis。我不确定我是否理解您的意思。您希望您的Pre\u-Market
-类也有一个A
、一个B
和一个C
-属性?并且应该在getABC中设置thiose属性代码>?我不确定此getABC
应该做什么或应该返回什么。您是否正在尝试“生成”名为“nameABC[0]”、“nameABC[1]”、“nameABC[2]”的公共字符串[]
属性在Pre_Market
课程中?如果是这样,你肯定需要学习更多关于C#的知识。你不能在定义的课程中生成属性。实际上你可以,使用某种反射和/或动态魔法,但这不是你想要的。谢谢你的想法。我喜欢这个想法,但我尝试了你的想法,然后在没有课前检查的情况下面对一个问题它需要用于PostAPI来进行弹性搜索type@SnowprincezCoffee你能投赞成票吗,因为它解决了添加A、B、属性的问题谢谢你的想法。我这边的效果很好。
public static void SendDataToES(String startTimestamp, String startDate, String bid_x, String ask_x, IList<string> nameABC, string[] getABCs)
{
var tem_ehm = new Pre_Market
{
timestamp = startTimestamp,
date = startDate,
bid = bid_x,
ask = ask_x,
ABCs = new Dictionary<string, string>()
};
int i = 0;
foreach (string name in nameABCs)
tem_ehm.Add(name, getABCs[i++];
// access the ABCs like so:
string A = tem_ehm.ABCs["A"];
string B = tem_ehm.ABCs["B"];
string C = tem_ehm.ABCs["C"];
}
class Pre_Market
{
public string x_ric { get; set; }
public string ask { get; set; }
public string bid { get; set; }
public string date { get; set; }
public string timestamp { get; set; }
public Dictionary<string, string> ABCs { get; set; }
}