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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/csharp/244096.html

猜你在找的C#相关文章