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

MSDN documentation显示DataflowBlockOptions类上有一个NameFormat属性,描述如下:

Gets or sets the format string to use when a block is queried for its name.

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

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

解决方法

你没有设置名称,你设置一个最终会产生名称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());

输出

The name format may contain up to two format items. ActionBlock`1 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.

如果我们看一下source code on .Net Core,ToString实现基本上是:

return string.Format(options.NameFormat,block.GetType().Name,block.Completion.Id);

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...