Ninject:当属性

时间:2016-09-28 13:47:53

标签: c# dependency-injection ninject inversion-of-control ioc-container

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

我们可以使用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();
}

1 个答案:

答案 0 :(得分:0)

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

这样的东西
source: function (query, response) {
    return $.get('/ajax/customtags/gettags', {
        search: query,
        userId: 1
        }, function (data) {
             console.log(data);
             var result =  $.map(data.data, function (item) {
                  console.log(item.text);
                  item.text                            
             });
             response(result);
    }, 'json');
},