public string[] tName = new string[]{"Whatever","Doesntmatter"};
string vBob = "Something";
string[] tVars = new string[]{"tName[0]","vBob","tName[1]"};
现在,我想更改 tName[0] 的值,但它不适用于:
for(int i = 0; i < tVars.Lenght;++i)
{
this.GetType().GetField("tVars[0]").SetValue(this, ValuesThatComeFromSomewhereElse[i]));
}
我该怎么做?
编辑:更改代码以更准确地显示我正在尝试做的事情。
最佳答案
不知道做你想做的事是否是个好主意,但这应该行得通:
((string[])GetType().GetField("tName").GetValue(this))[0] = "TheNewValue";
我认为将其拆分为多个语句是个好主意! ;-)
关于C# GetType().GetField 在数组位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3213070/