如何将文本数组元素转换为对象数组元素 [英] How do I convers a text array element to an object array element

查看:87
本文介绍了如何将文本数组元素转换为对象数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我给Serilog记录器文本文件写了一个用户名变量,事务名称变量和一个包含10个元素的字典。从下面的记录器输出中可以看出这个工作非常完美。





Below I am writing to a Serilog logger text file a user name variable, transaction name variable and a dictionary with 10 elements. This work perfectly as you can see from the logger output below.


var array1 ={"User name:", "Transaction:" ,"Transaction values:"};
var array2 = new object[] { MyUser.userId, MyTransSeq.transactionId, MyVarValueDict };
for(int j=0; j< array1.Length; j++)
{
    text = text + list1[j] + " {"+j+"} ";
}
MyGlobals.logger.Information(text, array2.ToArray());





上述代码的输出

2015-01-29 21:20:57.935 +00:00 [信息]用户名:pclarkeirl交易:initmenu01交易价值:[( <& option>:),(<& envname>:\\),(<& test>:MYTESTVALU),(< ;& envcount>:3),(<& companyid>:strykerlmk),(<& envdesc1>:),(<& envdesc2> :),(<& envcolour>:),(<& count>:),(<& selopt>:)]




我现在想要改变这一点,以便用户可以选择记录哪些变量而不是硬编码变量。因此,用户可以输入字段列表作为逗号分隔字段,例如MyUser.userId,MyTransSeq.transactionId,MyVarValueDict,并将其写入array2。 array2的内容现在包含:

[0] =MyUser.userId,

[1] =MyTransSeq.transactionId,

[2] =MyVarValueDict



问题是它们现在是引号中的文本值。所以我的问题是如何将文本数组值(如MyUser.userId)重新转换为对象。以下是我尝试过的一些代码没有成功?







Output from above code
2015-01-29 21:20:57.935 +00:00 [Information] User name: "pclarkeirl" Transaction: "initmenu01" Transaction values: [("<&option>": " "), ("<&envname>": "\" \""), ("<&test>": "MYTESTVALU"), ("<&envcount>": "3"), ("<&companyid>": "strykerlmk"), ("<&envdesc1>": " "), ("<&envdesc2>": " "), ("<&envcolour>": " "), ("<&count>": " "), ("<&selopt>": " ")]


I now want to change this so that rather than hard code the variables, the user can select which variables are to be logged. So the user can type in the list of fields as a comma delimited field such as "MyUser.userId, MyTransSeq.transactionId, MyVarValueDict" and this is written to array2. The contents of array2 now contain:
[0]="MyUser.userId",
[1]="MyTransSeq.transactionId",
[2]="MyVarValueDict"

The problem is that they are now Text values in quotes. So my question is How can I turn a text array value such as "MyUser.userId" back into an object. Below is some code I have tried without success ?


string[] userSelArray={"MyUser.userId", "MyTransSeq.thisTransId", "MyVarValuesDict" }; // This is simulating the array values received from user.

string[] array1 = { "User name:", "Transaction:", "Transaction values:" };
object[] array2 = new object[array1.Count()];
string text = "";
for (int j = 0; j < array1.Length; j++)
{
  text = text + array1[j] + " {" + j + "} ";
  array2[j] = userSelArray[j];
}
MyGlobals.logger.Information("NEXT" + text, array2.ToArray());





以上代码输出

2015-01-29 22:01:44.502 +00:00 [信息] NEXTUser名称:MyUser.userId交易:MyTransSeq。 thisTransId交易价值:MyVarValuesDict



上面你可以看到变量名称被视为文本并且没有被翻译

推荐答案

C#是面向对象的。您需要创建表示问题/应用程序中的事物的类。变量包含属性/值,并且方法使您能够访问和操作变量。

您对每个值进行硬编码。您需要设计程序以适用于所有情况。
C# is object-oriented. You need to create classes representing the things in your problem/application. The variables hold the attributes/values and the methods enable you to access and manipulate the variables.
You are hard-coding every value. You need to design the program to work for all cases.


我很难对此进行编码以仅提供示例。所以也许专注于提供问题的答案



问候

Pat
I am hard coding this to provide an example only. So maybe concentrate on providing an answer to the question

regards
Pat


这篇关于如何将文本数组元素转换为对象数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆