将变量作为参数从textbox传递到C#responsetext

将变量作为参数从textbox传递到C#responsetext,c#,C#,考虑: int x = 10; 和String str=“x” 我想通过调用str变量输出10,因为它的值x。但问题是,要调用的是带有“的字符串x: response.write(); 我怎样才能解决这个问题?你可以把它装在一个容器里。。大概是这样的: class Container { public int X { get; set; } public Container() { X = 10; } } // .. elsewhere... o

考虑:

int x = 10;
String str=“x”

我想通过调用
str
变量输出10,因为它的值
x
。但问题是,要调用的是带有
字符串x

response.write();

我怎样才能解决这个问题?

你可以把它装在一个容器里。。大概是这样的:

class Container {
    public int X { get; set; }

    public Container() {
        X = 10;
    }
}

// .. elsewhere...

object getVariableValueByStringName(string str) {
    return typeof(Container).GetProperty(str).GetValue(new Container() /* <-- dummy or not.. up to you -->);
}
类容器{
公共整数X{get;set;}
公共容器(){
X=10;
}
}
// .. 在别处
对象GetVariableValueByString名称(字符串str){
返回typeof(Container).GetProperty(str).GetValue(newcontainer()/*);
}

我认为您需要输出x的值,即10作为响应。write()。您甚至可以在不使用字符串变量
str
的情况下执行此操作,如:

response.write(x);

or response.write(x.ToString());
根据你的密码

int x = 10;
and

String str = x.ToString();

而不是字符串str=“x”。

无法理解!,重新表述您的声明取决于您如何以及在何处声明
x
,也许您可以使用反射。但是,也许仅仅使用
字典
就足够了。字符串就是字符串。变量就是变量。不能通过将变量用引号括起来将其强制转换为字符串。尝试字符串str=x.ToString();看见