views:

20

answers:

1

I wish to specify a concrete type (MyType1) to be instantiated with a specific func passed into the constructor.

The constructor is:

 public MyTYpe1(Func<Type1, Type2> myFunc)
 {
    //...
 }

How can I specify the myFunc param in a StructureMap XML configuration file?

Note, I wish to pass into myFunc a static method on another type (MyType2.MyMethod). If I were to construct MyType1 in code it'd be:

var instance = new MyType1(MyType2.MyMethod);
A: 

You could write the name of the class and the name of the method in the XML file.

Then, at runtime, through reflection, you would get the MethodInfo for that method:

var method = Type.GetType(nameOfClass).GetMethod(nameOfMethod);

then you pass a lambda that invoked that method:

var instance = new MyType1<Type1, Type2>(x => (Type2)method.Invoke(null, x));

The null parameter is used for static methods.

Mau
I'm looking for the XML configuration. I'd expect something like (pseudo-xml): "the concrete type for this interface is this, with this method passed into the constructor". When configured, I expect StructureMap tp instantiate instances of MyType1 with the func defined in the xml, automatically. Does this make sense?
Ben Aston