0

exemple d'extension conventionComment utiliser l'extension de la convention avec l'initiation complexe (comme: ToMethod)

kernel.Bind(x => 
{ 
    x.FromThisAssembly() 
    .SelectAllClasses() 
    .WithAttribute<SomeAttribute>() 
    .BindBase(); 
}); 

Et chaque type que je reçois devrait être une initiation complexe avec cette méthode

public static IPage GetInstance(Type type) 
     {   
      MethodInfo method = typeof(PageService).GetMethod("Create"); 
      IPage page = (IPage)method.MakeGenericMethod(type).Invoke(null, new object[] { null }); 
      return page; 
     } 

Répondre

0

Créer un IBindingGenerator:

public class MyBindingGenerator : IBindingGenerator 
{ 
    public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(
     Type type, 
     IBindingRoot bindingRoot) 
    { 
     yield return bindingRoot 
      .Bind(type) 
      .ToMethod(ctx => GetInstance(type)); 
    } 

    public static object GetInstance(Type type) 
    { 
     MethodInfo method = typeof(PageService).GetMethod("Create"); 
     return method.MakeGenericMethod(type).Invoke(null, new object[] { null }); 
    } 
} 

et l'utiliser comme suit:

kernel.Bind(x => 
{ 
    x.FromThisAssembly() 
    .SelectAllClasses() 
    .WithAttribute<SomeAttribute>() 
    .BindWith<MyBindingGenerator>(); 
}); 
+0

Bonjour @BatteryBackupUnit ** Ce code ne compile pas **. La première erreur .BindWith parce que MyBindingGenerator doit hérité de IBindingGenerator Et la prochaine erreur il y a .Bind (serviceTypes) –

+0

@ValeraFedorenko pouvez-vous s'il vous plaît essayer à nouveau? – BatteryBackupUnit

+0

** ne compile toujours pas ** regardez la capture d'écran http://take.ms/SrZjn. Parce que CreateBindings n'a pas ce type de surcharge avec IEnumerable > CreateBindings ( IBindingRoot bindingRoot, IEnumerable servicetypes, type implementationType) –

0

Après quelques recherches d'exemples, j'ai écrit ces solutions. Je ne sais pas est-ce la meilleure solution, mais de toute façon il fonctionne pour moi. Si quelqu'un va améliorer mon code ou d'écrire les meilleures pratiques pour ma question que je serai reconnaissant :-)

UnitTest Là où je veux obtenir le type qui a hérité de IStepContext, qui ont MarkAttribute

[TestClass] 
public class Test 
{ 
    private readonly IKernel _kernel = new StandardKernel(); 

    [TestInitialize] 
    public void Startup() 
    {   
     _kernel.Bind(a => 
     { 
      a.FromThisAssembly() 
       .SelectAllClasses() 
       .InheritedFrom<IStepContext>() 
       .WithAttribute<MarkAttribute>(x => x.Type == Inheritance.Derived).BindWith<SettingsBindGenerator>(); 
     }); 
    } 
    [TestMethod] 
    public void BaseClass() 
    { 
     BaseClass1 derived1 = _kernel.Get<BaseClass1>(); 
     Type res = derived1.WhoIAm(); //-- - > "BaseClass1" 
     Assert.AreEqual(res, typeof (BaseClass1)); 
    } 

    [TestMethod] 
    public void DerivedClass() 
    { 
     IStepContextA derived = _kernel.Get<IStepContextA>(); 
     Type res = derived.WhoIAm(); //-- - > "DerivedClass" 
     Assert.AreEqual(res, typeof (DerivedClass)); 
    } 

    [TestMethod] 
    public void DerivedClassA1() 
    { 
     IStepContext derived2 = _kernel.Get<BaseClassA>(); 
     Type res = derived2.WhoIAm(); //-- - > "DerivedClass" 
     Assert.AreEqual(res, typeof (DerivedClassA1)); 
    } 
} 

sur mesure BindGenerator

public class SettingsBindGenerator : IBindingGenerator 
    { 
     public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot) 
     { 
      Func<Type, IBindingWhenInNamedWithOrOnSyntax<object>> func = 
       t => bindingRoot.Bind(t).ToMethod(ctx => GetInstance(ctx, type)); 


      var bindings = new List<IBindingWhenInNamedWithOrOnSyntax<object>>(); 

      // if type inherited from interface 
      Type[] interfs = type.GetInterfaces(); 
      if (interfs.Length > 1) 
      { 
       // skip base interface (IStepContext) 
       interfs = interfs.Take(interfs.Length - 1).ToArray(); 
       bindings = interfs.Select(x => func(x)).ToList(); 
      } 

      // if type inherited from baseType 
      Type baseType = type.BaseType; 
      if (baseType != null) 
      { 
       bindings.Add(func(baseType)); 
       if (ShouldBeBound(baseType)) 
       { 
        var ancestor = baseType.BaseType; 
        while (ancestor != null && ShouldBeBound(ancestor)) 
        { 
         bindings.Add(func(ancestor)); 
         ancestor = ancestor.BaseType; 
        } 
       } 
      } 
      return bindings; 
     } 

     private static bool ShouldBeBound(Type type) 
     { 
      return type.IsClass && type != typeof (object); 
     } 

     private object GetInstance(IContext ctx, Type type) 
     { 
      MethodInfo method = typeof(PageService).GetMethod("Create"); 
      IPage page = method.MakeGenericMethod(type).Invoke(null, new object[] { null });   
      return page; 
     } 
    } 

classes d'aide

public class BaseClass : AbstractStepContext 
    { 
     public override Type WhoIAm() 
     { 
      return GetType(); 
     } 
    } 

    public class AbstractStepContext : IStepContext 
    { 
     public virtual Type WhoIAm() 
     { 
      return GetType(); 
     } 
    } 

public class BaseClass1 : IBase 
    { 
     public virtual Type WhoIAm() 
     { 
      return GetType(); 
     } 
    } 
[Mark(Inheritance.Base)] 
    public class BaseClass1Derivied : BaseClass1 
    { 
     public override Type WhoIAm() 
     { 
      return GetType(); 
     } 
    } 

public class BaseClassA : AbstractStepContext 
    { 
     public virtual Type WhoIAm() 
     { 
      return GetType(); 
     } 
    } 

[Mark(Inheritance.Derived)] 
    public class DerivedClass : IStepContextA 
    { 

     public Type WhoIAm() 
     { 
      return GetType(); 
     } 

    } 

[Mark(Inheritance.Derived)] 
    public class DerivedClassA1 : BaseClassA 
    { 
     public override Type WhoIAm() 
     { 
      return GetType(); 
     } 
    } 

public interface IStepContext 
    { 
     Type WhoIAm(); 
    } 
    public interface IStepContextA : IStepContext 
    {  
    } 

public class MarkAttribute : Attribute 
    { 
     public Inheritance Type ; 
     public MarkAttribute(Inheritance type) 
     { 
      Type = type; 
     } 
    } 
+0

Bonjour @BatteryBackupUnit pouvez-vous regarder sur cette solution, parce que je pense peut être une meilleure solution. –