1

Iterator Method の引数を使用してメソッド名を取得したいのですが、簡単な解決策を見つけるのに苦労しています。イテレータはコンパイラによって生成されます。その結果、ソース メソッド名とその引数が、生成されたクラス名とフィールドにそれぞれ含まれるようになりました (魔法の <>+_d シンボルを使用)。

Visual Studio のイミディエイト ウィンドウで、イテレータ メソッドを入力すると、メソッド名とその引数が適切な方法で取得されます。彼らはいくつかのカスタムプリンターを使用していますか、それともそれを行うためのヘルパーがいますか?

編集:

主な目標は、コルーチンの非同期コール スタックを取得することです。

Program.Test(a, b)
  Program.TestB(b)

次に例を示します。

using System;
using System.Collections;
using System.Linq;

class Program
{
    static IEnumerable TestB(string b)
    {
        yield return b;
        yield return b;
    }

    static IEnumerable Test(string a, string b)
    {
        yield return a;
        yield return TestB(b);
    }

    static void Main(string[] args)
    {
        var iterator = Test("foo", "bar");

        // Immediate Window
        // ================
        // iterator
        // {Program.Test}
        //     a: null
        //     b: null
        //     System.Collections.Generic.IEnumerator<System.Object>.Current: null
        //     System.Collections.IEnumerator.Current: null

        Console.WriteLine(iterator);
        // prints:
        // Program+<Test>d__0

        foreach (var field in iterator.GetType().GetFields())
            Console.WriteLine(field);
        // prints:
        // System.String a
        // System.String <>3__a
        // System.String b
        // System.String <>3__b
    }
}
4

1 に答える 1

0

DebuggerTypeProxy属性を使用して、Programクラスに設定できます。これは、Enumarable の代わりに HashTable を表示する方法を示すMSDN ドキュメント ページの例と非常によく似ています。代わりに Enumerable を使用するようにこれを変換するのはかなり簡単なはずです。ただし、これを機能させるには、IEnumerable から継承する独自のクラスを作成する必要がある場合があります。暗黙の列挙型で機能するかどうかはわかりませんが、ここでもショートカットを使用していて、シュガー コーティングが必要なため、これらはうまく連携しないことがよくあります。

[DebuggerDisplay("{value}", Name = "{key}")]
internal class KeyValuePairs
{
    private IDictionary dictionary;
    private object key;
    private object value;

    public KeyValuePairs(IDictionary dictionary, object key, object value)
    {
        this.value = value;
        this.key = key;
        this.dictionary = dictionary;
    }
}

[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable : Hashtable //this would be Program
{
    private const string TestString = "This should not appear in the debug window.";

    internal class HashtableDebugView
    {
        private Hashtable hashtable;
        public const string TestString = "This should appear in the debug window.";
        public HashtableDebugView(Hashtable hashtable)
        {
            this.hashtable = hashtable;
        }

        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
        public KeyValuePairs[] Keys
        {
            get
            {
                KeyValuePairs[] keys = new KeyValuePairs[hashtable.Count];

                int i = 0;
                foreach(object key in hashtable.Keys)
                {
                    keys[i] = new KeyValuePairs(hashtable, key, hashtable[key]);
                    i++;
                }
                return keys;
            }
        }
    }
}
于 2014-12-31T10:15:45.757 に答える