-2

検討:

int x = 10;

String str = "x";

strvalue を持つ変数を呼び出して 10 を出力したいx。しかし、問題は、呼び出されるのがstring xwith""であることです:

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 ie 10 の値を出力する必要があると思います。str次のように、 String variable なしでそれを行うこともできます。

response.write(x);

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

そして、あなたのコードに従って

int x = 10;
and

String str = x.ToString();

String str = "x" の代わりに。

于 2013-05-25T07:33:48.093 に答える