How do you set/get/use the name of a block in TPL Dataflow?
Asked Answered
B

1

9

MSDN documentation shows that there is a NameFormat attribute on the DataflowBlockOptions class, described as:

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

So ... how do you set the name? How is the name available? When is it used?

Or ... as I suspect ... is this just a remnant of design that didn't actually get implemented?

Bagger answered 28/6, 2014 at 17:51 Comment(2)
I did not test this - but isn't this documented right there: "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." - msdn.microsoft.com/en-us/library/…Biodegradable
Well, this is embarrassing, but looking at the DataflowBlockOptions Properties page it didn't occur to me to click on NameFormat to see that that page said, since it seemed complete to me right there. Now I've learned better!Bagger
D
10

You don't set the name, you set a NameFormat that will eventually result in a name (you can of course disregard the parameters and set whatever you want like NameFormat = "bar") . You may get the name by using ToString, for example:

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

Output:

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.


If we look at the source code on .Net Core the ToString implementation is basically:

return string.Format(options.NameFormat, block.GetType().Name, block.Completion.Id);
Damal answered 28/6, 2014 at 18:1 Comment(5)
Ahh! It isn't some user defined identifier for the block ... it is like the class name. So ... you can't distinguish in this way different blocks you've created from, say, TransformBlock, except by their numerical ID. And dataflow blocks are sealed (which has always annoyed me) so you can't alter it by deriving from it.) Thanks!Bagger
@Bagger a numerical Id is the best to differentiate between items IMO, but yes, that's true.Damal
holy cow, I just hit myself over the head with a cluebar and figured out what you were trying to tell me. Thanks!Bagger
@Damal What do you mean by using a numerical Id to differentiate between the blocks? As far as I can tell, the Completion Id associated to a specific block might change, am I wrong? I implemented a simple class that helps to construct a dataflow pipeline, and that class only exposes the list of IDataflowBlock created. I was trying to find a way to test this class to make sure each block was the one I created. The only thing I can think of is to use the ToString and compare to the NameFormat I gave itMcnalley
@JoãoGonçalves the id associated with a block instance doesn't change AFAIK. If you create another instance it will get a different Id.Damal

© 2022 - 2024 — McMap. All rights reserved.