Search code examples
c#mefprism-4

MEF share instance between modules


I'm using PRISM/MEF for my C# application. I want to do the following, I'm really new to PRISM and maybe I'm doing something completely wrong or got something wrong but it does not work like showed below.

I created two modules, where in Module1 I created an instance of a class. I want to share this instance with the other Module2.

Something like this:

Module1 (ViewModel):

[Export("ME")]
private Person me = new Person();
//within some function...
me.Name = "Jasmin";

Module2 (ViewModel):

[Import("ME")]
private Lazy<Person> me;
//within some function
Console.Writeline(me.Name); //here I want to get "Jasmin"

Shared Model Class:

public class Details
{
   public string Name;
}
public class Person
{
   public Name first = new Name();
}

Solution

  • It exactly works like shown in my question. I just had to rebuild the entire solution.

    To access the object later on, instead of:

    Console.Writeline(me.Name); //here I want to get "Jasmin"
    

    One has to use:

    Console.Writeline(me.Value.Name); //here I want to get "Jasmin"
    

    Thanks for your help.