This may be simple but as I am new to MEF arena that is why I am having some difficulty to figure out the solution to my problem.
I am working on an application using WPF and Prism with MEF as DI container. I want to tie my object (i.e.
Now the challenging part is how to replace this object with the deserialized one in the MEF container?
This is the code effort that i was trying to do but no luck:
I am working on an application using WPF and Prism with MEF as DI container. I want to tie my object (i.e.
RuleFile
) with each application instance by associating it with the file say RuleFile1.ruleapp. Therefore I have decorated it with attribute [PartCreationPolicy(CreationPolicy.Shared)]
to treat it as singleton so that it remains same throughout the application with each application instance.[Serializable()]
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class RuleFile : NotifyPropertyChanged, IRuleFile { }
Next, at the time of ViewModel [ImportingContructor] as shown below, the object is same as desired.[ImportingConstructor]
public RuleViewModel(RuleFile ruleFile)
[ImportingConstructor]
public SchemaViewModel(RuleFile ruleFile)
Until now everything is smooth.Now the challenging part is how to replace this object with the deserialized one in the MEF container?
This is the code effort that i was trying to do but no luck:
public void Load()
{
var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
var myExport = container.GetExport<RuleFile>();
container.ReleaseExport(myExport);
RuleFile = SerializationHelper.DeserializeFromFile<RuleFile>(Constants.RuleFilePath);
container.ComposeExportedValue<RuleFile>(RuleFile);
}