My question is, why is this property a string that has to follow a specific format?
I suspect this is a throwback to the Windows API, where OPENFILENAME
's lpstrFilter
is effectively using this format, with a null character replacing the |
characters, and two terminating null characters.
What are the advantages of this, as opposed to, for instance, using a Dictionary, where the first string is the name of the filter and the second name an array of file extensions supported by this filter, or something like that.
The main advantage here would purely be simplicity. A single string is far less code than a Dictionary<T,U>
where the value is, itself, an object that needs construction (ie: a string[]
). In general, file filters are typically not something that changes at runtime, so the string is effectively a constant that is determined once.
The disadvantage is having to work with "magic strings" of a strange format, of course.
It would be fairly easy to build something to handle this for you, though:
public static string BuildFileFilter(Dictionary<string, string[]> filters)
{
return string.Join("|",
filters.Select(kvp => kvp.Key
+ "|" + string.Join(";", kvp.Value)));
}