开发者

Ninject 2.0 - What's the alternative to ConventionMemberSelector

I have just downloaded the latest version of Ninject and replaced our existing Ninject.Core and Ninject.Condidtions assemblies with the single Ninject.dll (CF builds if that makes a difference). All has gone smoothly until I get to:

kernel.Components.Connect<IMemberSelector>(new MyMemberSelector());

Which is implemented:

public class MyMemberSelector : ConventionMemberSelector
{
    protected override void DeclareHeuristics()
    {
        InjectProperties(When.Property.Name.StartsWith("View"));
    }
}

I can't find any refe开发者_如何学Gorence to what this has been replaced with and my bindings don't just work - the View properties aren't injected.

Can anyone help?

Thanks


You can implement your own IInjectionHeuristic and add it as a Kernel component.

var selector = kernel.Components.Get<ISelector>();
var heuristic = new PropertyMemberSelector(member => member.Name.StartsWith("View"));
selector.InjectionHeuristics.Add(heuristic);


public class PropertyMemberSelector
    : NinjectComponent, IInjectionHeuristic
{
    private readonly Func<MemberInfo, bool> _predicate;

    public PropertyMemberSelector(Func<MemberInfo, bool> predicate)
    {
        _predicate = predicate;
    }

    public bool ShouldInject(MemberInfo member)
    {
        return member.MemberType == MemberTypes.Property && _predicate( member );
    }
}

Regards,

Ian

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜