开发者

Auto registering complex generic types in StructureMap

开发者 https://www.devze.com 2023-03-18 23:14 出处:网络
I need some help in auto-registering generics using StructureMap. Here is my scenario: public class Object<T>

I need some help in auto-registering generics using StructureMap. Here is my scenario:

public class Object<T>
{
}

public interface IBehvior<T>
{
    void DoSomething(T t);
}

public class Behvior<T> : IBehvior<Object<T>>
{
    public void DoSomething(Object<T> t)
    {

    }
}

What I want to accomplish is something like:

var x = ObjectFactory.GetInstance<IBehavior<Object<int>>();

But when I run this statement, it gives me an error that no default instance is configured. In my StructureMap configuration I've used

ConnectImplementationsToTypesClosing(typeof(IBehavior<>))

But it still doesn't work!

Note that this worked fine if I didn't have Object. For example, if I have:

public class IntBehavior : IBehavior<int>
{ 
}

Everything works perfec开发者_JAVA百科tly fine. But when I replace int with a generic type, it doesn't work!

Any ideas?


Ok I discovered the solution here:

http://lostechies.com/jimmybogard/2010/01/07/advanced-structuremap-custom-registration-conventions-for-partially-closed-types/

0

精彩评论

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