c# - C# 中的 Action 委托(delegate)

标签 c# delegates anonymous-types

我无法理解 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() 的内容/位置执行起来很难。可能是关于匿名类型的一些我不明白的事情?

最佳答案

aExecuteRazorMethod 中执行时,它会执行您作为 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/

相关文章:

c# - 如何在 OpenCVSharp 代码中使用新的 C++ 样式 API?

c# - 如何使用 Expression 类调用 Lambda 表达式?

iphone - 如何使用自己的委托(delegate)方法添加 subview ?

c# - 匿名类型的 IEqualityComparer

c# - 为什么 Visual Studio 2010 C# Express 不能正确格式化嵌套代码?

c# - 根据文本确定自定义消息框的窗口大小

C# DLL 配置文件不存在

ios - 想要创建一个监听器来检测整个应用程序中的 viewWillAppear 调用

c# - LINQ 到 SQL : How to handle ambiguous column names when joining tables?

Linq ToDictionary 返回匿名类型