开发者

How do I specify a constructor parameter in StructureMap XML config file?

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开发者_StackOverflow中文版 type (MyType2.MyMethod). If I were to construct MyType1 in code it'd be:

var instance = new MyType1(MyType2.MyMethod);


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.


StructureMap does not support the option of specifying the Constructor through the config file(at least till 2.6.4.1). By default and always, when configured dependency is configured through the XML file, StructureMap always chooses the greediest Constructor. To overcome this limitation, such dependencies will have to be put in a registry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜