ローカル変数の一覧を取得
Posted feedbacks - C#
正直、C#では厳しいので、静的な情報出力でお茶を濁している。 変数の名前も内容も出力していない。 できるとすれば、以下のような感じかな。デバッガ作るようなもんですね。 ・コンパイルしてアセンブリにする。 ・ホスティングプロセスでアセンブリを読込んで実行する。 ・ProgramDebugDatabaseを読みこんでシンボルを取得。 ・LocalVariablesを取得して表示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System;
using System.Reflection;
using System.Diagnostics;
class Program
{
static void Main()
{
int x = 1;
string y = "abc";
ShowLocalVariables(new StackFrame(true));
// System.Int32 (0)
// System.String (1)
}
static void ShowLocalVariables(StackFrame sf)
{
foreach (LocalVariableInfo lv in sf.GetMethod().GetMethodBody().LocalVariables)
Console.WriteLine(lv);
}
}
|

にしお
#3391()
Rating0/0=0.00
Pythonで表現すると、下のコードの???部分を埋めることになります。
>>> def foo(): x = 1 y = "hello" ??? return result >>> foo() {'y': 'hello', 'x': 1}[ reply ]