我无法理解 C# 中的 Action 委托(delegate)
我看过this问题,我理解那里的例子,但在我正在处理的代码库中,它的使用方式我不太明白。这是使用 Action 委托(delegate)的方法:
public static string RenderTemplate<T>(string templatePath, T data)
{
string result = null;
ExecuteRazorMethod(() =>
{
result = Razor.Run(data, templatePath);
});
return result;
}
调用 ExecuteRazorMethod() 将调用以下方法:
private static void ExecuteRazorMethod(Action a)
{
try
{
a();
}
.
.
.//irrelevant code
.
}
我不明白执行 a() 时会发生什么。调用了什么方法?我试过调试它,传递给方法的值是:Void <RenderTemplate> b__5
我不明白。我需要知道 RenderTemplate 中的两个参数实际发生了什么,但不了解 a()
的内容/位置执行起来很难。可能是关于匿名类型的一些我不明白的事情?
最佳答案
当 a
在 ExecuteRazorMethod
中执行时,它会执行您作为 a
传递给 ExecuteRazorMethod
的委托(delegate)。尝试在这段代码中 result = Razor.Run(data, templatePath);
这一行切换断点:
ExecuteRazorMethod(() =>
{
result = Razor.Run(data, templatePath);
});
你会看到,当 a
开始执行时,你的断点就会命中。
关于c# - C# 中的 Action 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25240029/