加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:34:54 所属栏目:百科 来源:网络整理
导读:MSDN documentation显示DataflowBlockOptions类上有一个NameFormat属性,描述如下: Gets or sets the format string to use when a block is queried for its name. 那么……你怎么设置这个名字?这个名字怎么样?什么时候使用? 或者……正如我怀疑的那样…
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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读