【问题标题】:How do you set/get/use the name of a block in TPL Dataflow?如何在 TPL 数据流中设置/获取/使用块的名称?
【发布时间】:2014-06-28 17:51:54
【问题描述】:

MSDN documentation 表明在DataflowBlockOptions 类上有一个NameFormat 属性,描述为:

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

那么......你如何设置名称?名称如何可用?什么时候使用?

或者......我怀疑......这只是设计的残余,并没有真正得到实施?

【问题讨论】:

  • 我没有对此进行测试 - 但那里没有记录:“名称格式最多可以包含两个格式项。{0} 将替换为块的名称。{1} 将用块的 ID 替换,从块的 Completion.Id 属性返回。" - msdn.microsoft.com/en-us/library/…
  • 嗯,这很尴尬,但是查看 DataflowBlockOptions 属性页面时,我并没有想到单击 NameFormat 来查看该页面所说的内容,因为它在那里对我来说似乎是完整的。现在我学得更好了!

标签: c# .net task-parallel-library tpl-dataflow


【解决方案1】:

你没有设置名字,你设置了一个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());

输出:

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


如果我们查看source code on .Net CoreToString 的实现基本上

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

【讨论】:

  • 啊!它不是块的一些用户定义的标识符......它就像类名。所以......你不能以这种方式区分你从TransformBlock创建的不同块,除了它们的数字ID。并且数据流块是密封的(这一直让我烦恼)所以你不能通过派生来改变它。)谢谢!
  • @davidbak 数字 ID 是区分 IMO 项目的最佳方式,但是是的,这是真的。
  • 天哪,我只是用线索条敲了自己的头,然后才明白你想告诉我什么。谢谢!
  • @i3arnon 使用数字 ID 来区分块是什么意思?据我所知,与特定块关联的 Completion Id 可能会更改,我错了吗?我实现了一个有助于构建数据流管道的简单类,该类仅公开创建的 IDataflowBlock 列表。我试图找到一种方法来测试这个类,以确保每个块都是我创建的。我唯一能想到的就是使用 ToString 并与我给它的 NameFormat 进行比较
  • @JoãoGonçalves 与块实例关联的 id 不会改变 AFAIK。如果您创建另一个实例,它将获得不同的 ID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多