Wil*_*orn 3 c# arrays position gettype
public string[] tName = new string[]{"Whatever","Doesntmatter"};
string vBob = "Something";
string[] tVars = new string[]{"tName[0]","vBob","tName[1]"};
Run Code Online (Sandbox Code Playgroud)
现在,我想更改tName [0]的值,但它不能用于:
for(int i = 0; i < tVars.Lenght;++i)
{
this.GetType().GetField("tVars[0]").SetValue(this, ValuesThatComeFromSomewhereElse[i]));
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
编辑:更改代码以更准确地显示我想要做的事情.
不知道做你想做的事情是不是一个好主意,但这应该有效:
((string[])GetType().GetField("tName").GetValue(this))[0] = "TheNewValue";
Run Code Online (Sandbox Code Playgroud)
我认为将它分成多个语句是个好主意!;-)