Skip to content

Instantly share code, notes, and snippets.

@jcdickinson
Created February 14, 2011 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcdickinson/826043 to your computer and use it in GitHub Desktop.
Save jcdickinson/826043 to your computer and use it in GitHub Desktop.
Selectors for MEF
[Export(typeof(LanguageSelector))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class LanguageSelector : Selector<ILanguageProvider>
{
protected override string IdentityFor(ILanguageProvider import)
{
return import.Name;
}
protected override ILanguageProvider ImportFor(string identity)
{
foreach (var item in base.Options)
{
if (item.Name == identity)
return item;
}
return base.Options.First();
}
}
/// <summary>
/// Event arguments about a selection change.
/// </summary>
public class SelectionChangedEventArgs<TImport> : EventArgs
{
/// <summary>
/// Gets the new selection.
/// </summary>
public TImport NewSelection
{
get;
private set;
}
/// <summary>
/// Initializes a new instance of the <see cref="SelectionChangedEventArgs&lt;TImport&gt;"/> class.
/// </summary>
public SelectionChangedEventArgs(TImport newSelection)
{
NewSelection = newSelection;
}
}
/// <summary>
/// Represens a selector.
/// </summary>
/// <typeparam name="TImport">The type of the import.</typeparam>
public abstract class Selector<TImport>
{
/// <summary>
/// Gets the options.
/// </summary>
[ImportMany(typeof(TImport))]
public IEnumerable<TImport> Options
{
get;
private set;
}
private TImport _selectedValue;
/// <summary>
/// Gets the selected value.
/// </summary>
public TImport SelectedValue
{
get
{
return SelectedValue;
}
private set
{
_selectedValue = value;
var temp = SelectionChanged;
if (temp != null)
{
temp(this, new SelectionChangedEventArgs<TImport>(value));
}
}
}
/// <summary>
/// Occurs when the selection is changed.
/// </summary>
public event EventHandler<SelectionChangedEventArgs<TImport>> SelectionChanged;
/// <summary>
/// Initializes a new instance of the <see cref="Selector&lt;TImport&gt;"/> class.
/// </summary>
public Selector()
{
Monoflector.CompositionServices.ComposeParts(this);
}
protected abstract string IdentityFor(TImport import); // We need this for saving settings later.
protected abstract TImport ImportFor(string identity);
}
public class MustUseSelectedLanguage : IPartImportsSatisfiedNotification
{
[Import(typeof(LanguageSelector))]
private LanguageSelector _selector;
void IPartImportsSatisfiedNotification.OnImportsSatisfied()
{
_selector.SelectionChanged += new EventHandler<SelectionChangedEventArgs<ILanguageProvider>>(_selector_SelectionChanged);
_theCombo.SelectedItem = _selector.SelectedValue;
UpdateDissasemblers();
}
void _selector_SelectionChanged(object sender, SelectionChangedEventArgs<ILanguageProvider> e)
{
_theCombo.SelectedItem = _selector.SelectedValue;
UpdateDissasemblers();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment