import problem with CreationPolicy

Jun 18, 2010 at 9:26 AM
Edited Jun 18, 2010 at 9:30 AM

below is the demo of my problem,

I'd like to create many child which have a reference to their parent.

How to write the import attribute to get the parent reference instead of create a new parent instance?

 

public partial class MainPage : UserControl
{
    [Import(typeof(Parent))]
    public Parent Parent1 { get; set; }

    [Import(typeof(Parent))]
    public Parent Parent2 { get; set; }


    public MainPage()
    {
        InitializeComponent();
        CompositionInitializer.SatisfyImports(this);
        Parent1.name = "p1";
        Parent2.name = "p2";
    }
}

[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(Parent))]
public class Parent
{
    [Import(typeof(Child))]
    public Child Child1 { get; set; }

    [Import(typeof(Child))]
    public Child Child2 { get; set; }

    public string name;
}


[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(Child))]
public class Child
{
    //how to write the import attribute
    public Parent Parent { get; set; }
    public string name;
}