How can I prevent MEF from throwing an exception when an Export is not found?

Posted by Kilhoffer on Stack Overflow See other posts from Stack Overflow or by Kilhoffer
Published on 2010-05-13T16:37:18Z Indexed on 2010/05/13 23:54 UTC
Read the original article Hit count: 218

I have a class with an IList<T> property decorated with the [ImportMany(allowRecomposition = true)] attribute. There are some conditions in which the application might not find any available Exports of the requested type. Right now, it's throwing a CompositionException if no Exports of the requested type are found. I dislike application flow being determined by thrown exceptions, so I would rather not catch and react in this case. Rather, I just want program execution to continue.

Is there a flag or something I can set to make this Import optional? I know for single import properties, you can do this: [Import(AllowDefault = true)] but 'AllowDefault' is not an option on the ImportMany attribute.

© Stack Overflow or respective owner

Related posts about MEF

Related posts about .NET