Ninject:当属性标记的继承类型时如何获取继承类型而不是基类型

Ninject : How Get inherited type instead Base type when inherited type marked by attribute

当继承类型被属性标记时,如何获取继承类型而不是基类型?

我们可以用 Ninject 做吗?这是我拥有的:

static void Main(string[] args)
{
    IKernel kernel = new StandardKernel();

    // use Method which return properly type
    kernel.Bind<IBase>().ToMethod(context => SOMEMETHOD());

    // should be return DerivedClass 
    BaseClass derived = kernel.Get<BaseClass>();
    BaseClass1 derived1 = kernel.Get<BaseClass1>();

    // The result should be:
    derived.WhoIAm();  //--> "DerivedClass"
    derived1.WhoIAm(); //--> "DerivedClass1"
}

// method which resolve this
static object SOMEMETHOD()
{
    // return derived class wich marked by
    // SomeAttribure where base class that which we use in kernel.Get 
}    

基地类

class BaseClass : IBase
{    
    public virtual void WhoIAm()
    {
        Console.Write(this.GetType());
    }   
}

class BaseClass1 : IBase
{    
    public virtual void WhoIAm()
    {
        Console.Write(this.GetType());
    }   
}

继承类。 SomeAttribute 是标记应该创建的类型的属性

[SomeAttribute]
class DerivedClass : BaseClass
{
    public override void WhoIAm()
    {
        Console.Write(this.GetType());
    }
}    

其他派生 类 继承自 BaseClass1

[SomeAttribute]
class DerivedClass1 : BaseClass1
{
    public override void WhoIAm()
    {
        Console.Write(this.GetType());
    }
}    
class SomeAttribute : Attribute {}

interface IBase
{
    void WhoIAm();
}

查看约定扩展。应该直截了当。像

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