To create a property with list of available options we usually use SelectionFactory. One SelectionFactory class can be used on many properties. But sometimes, for specific property, we want one or two extra selection items. The SelectOne and SelectMany attributes have SelectionFactoryType property which is a factory type not the factory instance. In this article I’d like to show how create SelectionFactory with parameters.

Example

Let say that we have two properties on the StartPage: ForegroundColors and BackgroundColors. Both should be list of available colors (red, green and blue). But for ForegroundColors we also need orange and yellow and for BackgroundColors we need extra brown color.
SelectionFactory with parameters

Implementation

The selection factory class has to implement ISelectionFactory interface which expose GetSelections method. The factory with the default colors can be implemented as:

The GetSelections method has ExtendedMetadata parameter. Using this parameter we have access to all attributes that annotate the property. So we could create new attrbute ColorOptionAttribute that for adding extra colors to the factory.

Now we can read the attribute in the factory by using metadata.Attributes property.

The attribute has AllowMultiple property set to true, so it can be used on the property more than once.