-2

考虑:

int x = 10;

String str = "x";

我想通过调用str变量来输出 10,因为它有 value x。但问题是它是一个string x""调用的:

response.write();

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

4

2 回答 2

0

您可以将其包装在容器中.. 像这样:

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 -->);
}
于 2013-05-25T07:33:33.580 回答
0

我认为您需要在 response.write() 中输出 x 即 10 的值。您甚至可以在没有 String 变量的情况下执行此操作str,例如:

response.write(x);

or response.write(x.ToString());

并根据您的代码

int x = 10;
and

String str = x.ToString();

而不是字符串 str = "x"。

于 2013-05-25T07:33:48.093 回答