C# 如何漂亮地打印迭代器方法
我想用迭代器方法的参数获取方法名,我正在努力找到一个简单的解决方案。迭代器是由编译器生成的,因此源方法名和它的参数现在分别位于生成的类名和字段中(带有一些magic+\d符号) 在VisualStudio的即时窗口中,当我输入迭代器方法时,我以一种非常好的方式获得了方法名及其参数。他们是否在使用一些定制打印机,或者可能有一些帮助者可以这样做 编辑: 主要目标是获得协同路由异步调用堆栈C# 如何漂亮地打印迭代器方法,c#,visual-studio,yield,coroutine,object-dumper,C#,Visual Studio,Yield,Coroutine,Object Dumper,我想用迭代器方法的参数获取方法名,我正在努力找到一个简单的解决方案。迭代器是由编译器生成的,因此源方法名和它的参数现在分别位于生成的类名和字段中(带有一些magic+\d符号) 在VisualStudio的即时窗口中,当我输入迭代器方法时,我以一种非常好的方式获得了方法名及其参数。他们是否在使用一些定制打印机,或者可能有一些帮助者可以这样做 编辑: 主要目标是获得协同路由异步调用堆栈 Program.Test(a, b) Program.TestB(b) 以下是一个例子: using Sy
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
}
}
使用系统;
使用系统集合;
使用System.Linq;
班级计划
{
静态IEnumerable测试b(字符串b)
{
收益率b;
收益率b;
}
静态IEnumerable测试(字符串a、字符串b)
{
收益率a;
收益率试验b(b);
}
静态void Main(字符串[]参数)
{
var迭代器=测试(“foo”、“bar”);
//即时窗口
// ================
//迭代器
//{Program.Test}
//a:空
//b:空
//System.Collections.Generic.IEnumerator.Current:null
//System.Collections.IEnumerator.Current:null
Console.WriteLine(迭代器);
//印刷品:
//程序+d\u 0
foreach(iterator.GetType().GetFields()中的变量字段)
控制台写入线(字段);
//印刷品:
//系统。字符串a
//系统。字符串3\u\a
//系统字符串b
//系统字符串3\u\b
}
}
您可以使用DebuggerTypeProxy
属性,并在程序
类中设置它。这与上给出的示例非常相似,该示例演示了如何显示哈希表,而不是枚举表。将其转换为使用可枚举项应该相当容易。您可能需要创建自己的类,该类继承自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;
}
}
}
}
不清楚你说的“漂亮的印刷品”是什么意思。您希望得到什么输出?我想得到方法名及其参数。目标是打印:Program.Test(a,b),但您刚刚得到调用该方法的结果。虽然您可以通过尝试将类型名称与方法匹配,但您必须考虑如果方法重载会发生什么情况,并且如果该方法的实现更改为以后不使用迭代器块,那么您还想做什么。(例如,假设您决定改用LINQ…)我使用迭代方法来实现协同路由。这个漂亮的打印将允许以与常规堆栈相同的方式获取异步调用堆栈。我想我应该用一些正则表达式来进行上面的思考。。。