2014-12-31 5 views
1

Iterator 메서드의 인수를 사용하여 메서드 이름을 가져오고 싶습니다. 간단한 솔루션을 찾기 위해 고심하고 있습니다. 반복자는 컴파일러에 의해 생성됩니다. 결과적으로 소스 메서드 이름과 인수는 생성 된 클래스 이름에 있고 필드는 각각 (일부 마법을 사용하여 <> + _ d 기호) 필드에 있습니다.Iterator 메서드를 출력하는 방법

Visual Studio의 직접 실행 창에서 반복기 메서드를 입력 할 때 메서드 이름과 그 인수가 꽤 비슷합니다. 그들은 몇몇 사용자 정의 프린터를 사용하고 있습니까? 아니면 그렇게 할 수있는 도우미가 있습니까?

편집 :

주요 목표는 코 루틴 비동기 호출 스택을 얻는 것입니다.

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 
    } 
} 
+0

"예쁜 인쇄물"의 의미가 명확하지 않습니다. 당신은 어떤 결과를 기대 했습니까 *? –

+0

메서드 이름과 인수를 가져 오려고합니다. – mipi

+0

목표는 다음과 같습니다. Program.Test (a, b) – mipi

답변

0

당신은 DebuggerTypeProxy 속성을 사용하여 Program 클래스를 설정할 수 있습니다 : 여기

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

은 예입니다. Enumarable 대신 HashTable을 표시하는 방법을 보여주는 MSDN documentation page에 나와있는 예제와 매우 비슷합니다. 대신 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; 
      } 
     } 
    } 
}