我一直在尝试访问在内部本身就是变量的变量值,我甚至不确定是否可能。
string var1 = "var2";
string var2 = "testvar";
那么,在这里,是否有可能使用var1变量访问值“ testvar”?
谢谢!
为更好地说明场景,以下是实际代码- 从excel列中读取变量 并且这些变量的值出现在代码中
//Excel codeFieldsListLocal column in CodeFields.xlsx
//row 1: var1
//row 2: var2
//row 3: var3
//row 4: var4
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Generic;
using HL7Spy.Core;
using HL7Spy.Core.Hl7;
using Excel = Microsoft.Office.Interop.Excel;
public partial class DynamicClassTest : HL7Spy.Core.BaseCustomFunction
{
string _outFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"out.txt");
string _inFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"CodeFields.xlsx");
Excel.Application xlApp = new Excel.Application();
string var1 = "John";
string var2 = "Tom";
string var3 = "Bruce";
string var4 = "Hugh";
string var5 = "Peter";
string var6 = "Steve";
string var7 = "Bob";
StreamWriter _file;
/Called for a set of data
public override void Run()
{
List<string> codeFieldsListLocal = new List<string>();
// create the file to be written out
_file = new StreamWriter(_outFileName, false);
codeFieldsListLocal = getCodeFieldsNamesFromExcel();
for(int i = 0; i < codeFieldsListLocal.Count; i++) {
_file.WriteLine(codeFieldsListLocal[i]); /* --> Here I wanted to display the values instead of the variable names, is it possbile? Like John, Tom and others; and not var1, var2...*/
}
}
//Called at time of code closure
public override void OnFinish() {
_file.WriteLine("OnFinish");
_file.Close();
// launch notepad to display the file.
System.Diagnostics.Process.Start("Notepad",_outFileName);
}
//Creating a list of variables from the excel column
public List<string> getCodeFieldsNamesFromExcel () {
List<string> codeFieldsListLocal = new List<string>();
Excel.Workbook workbook = xlApp.Workbooks.Open(_inFileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Excel.Worksheet sheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
Excel.Range range = (Excel.Range)sheet.UsedRange.Columns[3];
object[,] currentColumn = (object[,])range.Cells.Value2;
for (int i = 2; i < currentColumn.Length; i++) {
codeFieldsListLocal.Add(currentColumn[i,1].ToString());
_file.WriteLine(currentColumn[i,1]);
}
return codeFieldsListLocal;
}
}