C# GetType().GetField 在数组位置

标签 c# arrays position gettype

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/

相关文章:

ruby - 数组大小太大 - ruby

jquery - Fancybox 2.0 关闭按钮位于窗口顶角

css - 子 Div 的相对位置插入其他父按钮

css - 如何在底部为 :0px? 的情况下将包装器居中

c# - 具有字典属性的 protobuf-net 类

c# - WPF:创建在构造函数中需要参数的应用程序资源 - 如何?

c# - 这是什么属性(property)?有必要吗?

c# - 不明显的数组索引

python - Numpy:如何最好地对齐两个排序数组?

c# - 在 C# 中循环 SimpleJSON 数组