Note that there are some explanatory texts on larger screens.

plurals
  1. POMEF Property Allways Returning Null
    primarykey
    data
    text
    <p><strong>Original Source Code</strong></p> <p>I've got a simple business object in my BusinessObjects.dll file:</p> <pre><code>namespace BusinessObjects { public class MyClass { public MyClass() { DateTime = DateTime.Now; } public DateTime DateTime { get; set; } } } </code></pre> <p>In my SharedUI.dll I've got this "Context-provider" class, that I use to hold a referece to the currently selected MyClass - remember this is a simplyfied example :)...</p> <pre><code>namespace SharedUI { public class AppContext { [Export] public MyClass SelectedMyClass { get; private set; } public void SetupContext(MyClass myClass) { SelectedMyClass = myClass; } public static AppContext Context { get { if (context == null) { context = new AppContext(); } return context; } } private static AppContext context; } } </code></pre> <p>My MefTest.exe has this class:</p> <pre><code>namespace MefTest { public class Program { [Import] public MyClass MyClass { get; set; } private void Compose() { var ventSystem = new MyClass(); AppContext.Context.SetupContext(ventSystem); var executingAssembly = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var contextAssembly = new AssemblyCatalog(Assembly.LoadFile(string.Format(@"{0}\SharedUI.dll", Environment.CurrentDirectory))); var catalog = new AggregateCatalog(executingAssembly, contextAssembly); var container = new CompositionContainer(catalog); container.ComposeParts(this); } private void Run() { Compose(); // MyClass is always null in the next line? Console.WriteLine(MyClass.DateTime.ToString()); Console.ReadKey(); } private static void Main(string[] args) { var p = new Program(); p.Run(); } } } </code></pre> <p>I'm a MEF rookie so please bear with me :)</p> <p><strong>UPDATED Source Code with suggestions from Daniel Plaisted</strong></p> <p>MyClass source is the same...</p> <p>SharedUI.dll now looks like this:</p> <pre><code>namespace SharedUI { [Export] public class AppContext { [Export(typeof(MyClass))] public MyClass SelectedMyClass { get; private set; } public void SetupContext(MyClass myClass) { SelectedMyClass = myClass; } } } </code></pre> <p>MefTest.exe now looks like this:</p> <pre><code>namespace MefTest { public class Program { [Import] public MyClass MyClass { get; set; } [Import] public AppContext AppContext { get; set; } private void Compose() { var executingAssembly = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var contextAssembly = new AssemblyCatalog(Assembly.LoadFile(string.Format(@"{0}\SharedUI.dll", Environment.CurrentDirectory))); var catalog = new AggregateCatalog(executingAssembly, contextAssembly); var container = new CompositionContainer(catalog); container.ComposeParts(this); var myClass = new MyClass(); AppContext.SetupContext(myClass); } private void Run() { Compose(); // AppContext.SelectedMyClass is NOT null in the next line... which is good I guess :) Console.WriteLine(AppContext.SelectedMyClass.DateTime.ToString()); // MyClass is always null in the next line? Console.WriteLine(MyClass.DateTime.ToString()); Console.ReadKey(); } private static void Main(string[] args) { var p = new Program(); p.Run(); } } } </code></pre> <p>What am I doing wrong since I can't get it working?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload