我试图让这样的事情:C#字符串转换为布尔变量的名称
// int counter; - this is changing in ther other parts of program
bool abc1; bool abc2; bool abc3; bool abc4;
if("abc" + counter == true)
{
//some code
}
无论如何,我需要转换字符串和int为bool名。我怎样才能做到这一点?
我试图让这样的事情:C#字符串转换为布尔变量的名称
// int counter; - this is changing in ther other parts of program
bool abc1; bool abc2; bool abc3; bool abc4;
if("abc" + counter == true)
{
//some code
}
无论如何,我需要转换字符串和int为bool名。我怎样才能做到这一点?
使用,而不是一个数组:
bool[] abc;
// ...
if (abc[counter] == true) {
{
// some code.
}
不能在我的情况下使用数组。有可能将字符串转换为布尔名称? – user5081068
@ user5081068你不能拿一个值,只是神奇地得到它的变量名。为什么你不能使用数组? – gunr2171
你试图做的事听起来不像一个好主意,因为它听起来像你想做一些反思。如果你打算让自己走上这条路,那么我很难相信你不能使用数组。 – sstan
你是什么意思 “字符串转换和int到bool名”?你想达到什么目的? –
你的问题不清楚,请详细描述你正在努力完成的事情。 (“abc”+ counter == true)将永远不会评估为真,并且我很难理解您正在尝试完成的任务 – mgrenier
他试图从字符串和int形成变量的名称以便引用变量。可能通过反思可行,但更好地使用@斯坦的方法。像Eric J说的 –