I have a class that is DataContract
serialized, and also marked as a shared MEF [Export]
.
example:
[DataContract(Name="MyClass")]
[Export(typeof(MyClass))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class MyClass
{
[DataMember]
public string Field1{get;set;}
[DataMember]
public string Field2{get;set;}
public static MyClass Deserialize(XElement serialized)
{
DataContractSerializer ser = new DataContractSerializer(typeof(MyClass));
MyClass anotherMyClass = (MyClass)ser.ReadObject(serialized.CreateReader());
return anotherMyClass;
}
}
I have this marked as a shared export because I only want one instance at a time, but if I reassign an MEF [Import]
ed instance of this class (by calling the Deserialize
in an assignment) will it still be a singleton? Also, will the other Imported instances reflect this?
The PartCreationPolicy
only applies to parts created by MEF. Your Deserialize
method doesn't use MEF, so it will create a new instance. The instance created and managed by MEF will not be updated to match the new instance.