How to set ExportMetaData with multiple values as well as single w/ custom attribute ?

Posted by Matthew M. on Stack Overflow See other posts from Stack Overflow or by Matthew M.
Published on 2011-01-10T20:22:59Z Indexed on 2011/01/10 21:53 UTC
Read the original article Hit count: 393

Filed under:
|
|

I have the following ExportMetaData attributes set on my class:

  [Export(typeof(IDocumentViewer))]
  [ExportMetadata("Name", "MyViewer")]
  [ExportMetadata("SupportsEditing", true)]
  [ExportMetadata("Formats", DocFormat.DOC, IsMultiple = true)]
  [ExportMetadata("Formats", DocFormat.DOCX, IsMultiple = true)]
  [ExportMetadata("Formats", DocFormat.RTF, IsMultiple = true)]  

I also have a supporting interface:

  public interface IDocumentViewerMetaData {
    /// <summary>
    /// Gets the format.
    /// </summary>
    /// <value>The format.</value>
    IEnumerable<DocFormat> Formats { get; }
    /// <summary>
    /// Gets the name of the viewer
    /// </summary>
    /// <value>The name.</value>
    string Name { get; }
    /// <summary>
    /// Gets a value indicating whether this viewer supports editing
    /// </summary>
    /// <value><c>true</c> if [supports editing]; otherwise, <c>false</c>.</value>
    bool SupportsEditing { get; }
  }

And of course my ImportMany:

[ImportMany(typeof(IDocumentViewer))]
public IEnumerable<Lazy<IDocumentViewer, IDocumentViewerMetaData>> _viewers { get; set; }

What I would like to do is use a strongly-typed attribute class instead of using the ExportMetaData attribute. I have not figured out a way to do this while also supporting single values (Name, SupportsEditing, in the example above).

I envision doing something similiar the following (or whatever is suggested as best):

[Export(typeof(IDocumentViewer))]
[DocumentViewerMetadata(Name = "MyViewer")]
[DocumentViewerMetadata(SupportsEditing = true)]
[DocumentViewerMetadata(Format = DocFormat.DOC)]
[DocumentViewerMetadata(Format = DocFormat.DOCX)]

I am fairly certain that there IS a way to do this, I just haven't found the right way to connect the dots. :)

Thank you,

  • Matthew

© Stack Overflow or respective owner

Related posts about c#

Related posts about interface