Get ExportDefinition of a Lazy<T> import

Aug 24, 2009 at 1:21 PM

In preview 5 it was possible to get the ExportDefinition of an Export<T> by accessing the Defintion property. This allowed me to get the Type of Export without the need to instantiate the value. Or to log the type when the composition of the export failes. In preview 6 the Export<T> is replaced with Lazy<T> but this this type has no Defintion property anymore. Is there another way to retrieve the Type of the Lazy loaded value?

Thanks in advance,

Tim Cools

Aug 24, 2009 at 5:36 PM

Hi Tim

When you say type of export, what do you mean exactly? The contract?

Glenn

Developer
Aug 24, 2009 at 10:59 PM

MEF in general does not give you the Type automatically, even in the previous previews. However we did give access to the ExportDefinition which had the ContractName and the Metadata. You can still get access to the metadata via Lazy<T, M> (note if you don't want a strongly typed metadata view you can use IDictionary<string, object> as your M) but you cannot get the contract name any longer.

If it is the contract name you are looking for can you give us a scenario where you need this? Because if you are doing an Import of a particular contract name then every export that matches it should have the same contract name and thus you should already know the contract name.

Aug 25, 2009 at 7:52 AM

That is correct, PartDefinition only contains the contract name, and the contract name is not so useful. I was hoping to get the implementation type to notify the user when the loading the addin fails to indicate which add-in fails. For example when an Exception is thrown in the ctor of a specific plug-in, so the user knowns which add-in fails. PartDefinition.ToString() gives the implementation type, but the used PartDefinition is not available anymore for lazy loaded value.

The Stable Composition solves a lot of problems to prevent a lot of composition errors. Now I can log the rejected types very easily:

var info = new CompositionInfo(catalog, container);
var rejectedTypes = info.PartDefinitions.Where(d => d.IsRejected);
foreach (var partDefinition in rejectedTypes)
{
  Console.WriteLine("Type rejected: '{0}'", partDefinition.PartDefinition);
}

And I would like to give the user the same information when the loading of a value of a Lazy<T> object fails. Any suggestions?

Tim

Aug 25, 2009 at 6:26 PM

Hi Tim,

Glad to hear that you're having success with CompositionInfo.

Failure during Lazy.Value/construction of a part should be treated as a critical failure and is best caught at a top-level exception handler (e.g. in 'Main()').

The exception you get when construction fails will provide information about the part being constructed - see CompositionException.Errors[i].Element.

Cheers,

Nick