如何在TPL Dataflow中设置/获取/使用块的名称?

dav*_*bak 7 .net c# task-parallel-library tpl-dataflow

MSDN文档显示该类有一个NameFormat属性DataflowBlockOptions,描述如下:

获取或设置在查询块名称时使用的格式字符串.

那么......你怎么设置这个名字?这个名字怎么样?什么时候使用?

或者......正如我怀疑的那样......这只是一个实际上没有实现的设计残余?

i3a*_*non 8

你没有设置名称,你设置一个NameFormat最终会产生名称的名称(你当然可以忽略参数并设置你想要的任何名称NameFormat = "bar").您可以通过使用获得名称ToString,例如:

var block = new ActionBlock<int>(_ => { }, new ExecutionDataflowBlockOptions
{
    NameFormat = "The name format may contain up to two format items. {0} will be substituted with the block's name. {1} will be substituted with the block's Id, as is returned from the block's Completion.Id property."
});

Console.WriteLine(block.ToString());
Run Code Online (Sandbox Code Playgroud)

输出:

名称格式最多可包含两个格式项.ActionBlock`1将替换为块的名称.1将替换为块的Id,从块的Completion.Id属性返回.


如果我们看一下.Net的核心源代码ToString实现是基本:

return string.Format(options.NameFormat, block.GetType().Name, block.Completion.Id);
Run Code Online (Sandbox Code Playgroud)

  • 神圣的牛,我只是用一根线索击中了自己的脑袋,想出了你想要告诉我的内容.谢谢! (2认同)