微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);

原文地址:https://www.jb51.cc/csharp/244096.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐