从接口

时间:2015-07-13 17:42:45

标签: c# generics dependency-injection

在方法WhatGenericTypeIAm中我想获得有关实现的信息而不是泛型的抽象,所以我可以看到“Person”而不是“AbstractModel” 有没有办法做到这一点,或者这是不可能的,需要重新设计?

public class Program
{
    private static void Main(string[] args)
    {
        var container = new ContainerBuilder();
        container.RegisterType<SomeA>();
        container.RegisterType<MyDependency>().As<IMyDependency<Person>>();  // generic T is a Person
        var buildedContainer = container.Build();

        var someBase = buildedContainer.Resolve<SomeA>();
        someBase.SayName();


        Console.ReadKey();
    }
}

public interface IMyDependency<in T>
{
    void WhatGenericTypeIAm();
}

public class MyDependency : IMyDependency<AbstractModel>
{
    private string objectName = typeof (AbstractModel).Name;

    public void WhatGenericTypeIAm()
    {
        Console.WriteLine("My generic type is " + objectName );  // it shows "AbstractModel" but i want to see "Person"
    }
}

public abstract class AbstractModel  // will be implemented not only by Person
{

}

public class Person : AbstractModel
{

}

public abstract class SomeBaseClass<T>
where T : AbstractModel
{
    private readonly IMyDependency<Person> _myDependency;

    public SomeBaseClass(IMyDependency<Person> myDependency)  // generic T is Person 
    {
        _myDependency = myDependency;
    }

    public void SayName()
    {
        _myDependency.WhatGenericTypeIAm();
    }
}

public class SomeA : SomeBaseClass<Person>
{
    public SomeA(IMyDependency<Person> myDependency) : base(myDependency)
    {

    }
}

@BrentWorden嗯,我真的没有得到它,现在我有了这个,这是同样的问题

namespace ConsoleApplication1
{
public class Program
{
    private static void Main(string[] args)
    {
        var container = new ContainerBuilder();
        container.RegisterType<SomeA>();
        container.RegisterType<SomeB>();
        container.RegisterType<MyDependency<AbstractModel>>().As<IMyDependency>();  // generic T is a Person
        var buildedContainer = container.Build();

        var someBase = buildedContainer.Resolve<SomeA>();
        someBase.SayName();


        Console.ReadKey();
    }
}

public interface IMyDependency
{
    void WhatGenericTypeIAm();
}

public class MyDependency<T> : IMyDependency
{
    private string objectName = typeof (T).Name;

    public void WhatGenericTypeIAm()
    {
        Console.WriteLine("My generic type is " + objectName );  // it shows "AbstractModel" but i want to see "Person"
    }
}

public abstract class AbstractModel  // will be implemented not only by Person
{

}

public class Person : AbstractModel
{

}

public class OtherPerson : AbstractModel
{

}

public abstract class SomeBaseClass<T>
where T : AbstractModel
{
    private readonly IMyDependency _myDependency;

    public SomeBaseClass(IMyDependency myDependency)  // generic T is Person or OtherPerson
    {
        _myDependency = myDependency;
    }

    public void SayName()
    {
        Console.WriteLine("My name is " + typeof(T)); // i want this in _myDependency

        _myDependency.WhatGenericTypeIAm();
    }
}

public class SomeA : SomeBaseClass<Person>
{
    public SomeA(IMyDependency myDependency) : base(myDependency) //myDependency T is Person
    {

    }
}

public class SomeB : SomeBaseClass<OtherPerson>
{
    public SomeB(IMyDependency myDependency) : base(myDependency)      //myDependency T is OtherPerson
    {

    }
}

}

2 个答案:

答案 0 :(得分:2)

必须将MyDependency类声明为泛型类。然后你可以使用泛型的类型来做你想做的事情:

{ roles: [] }, :email, ...

现在的方式,MyDependency是使用AbstractModel静态输入的。

答案 1 :(得分:0)

类型MyDependency没有Person的概念,即使您正在运行它。

如果MyDependency的任何方法碰巧实际使用了Person个对象,那么您就可以从中获取名称"Person",但直到您#&# 39;实际上使用Person对象,您尝试打印的数据根本就不存在。

所以,即使你写了

IMyDependency<Person> dependency = new MyDependency();

您的对象为new MyDependency()IMyDependency<Person>只是您放入的变量的属性。实际对象本身没有Person的概念,并且它没有&#39 ;需要有一个。所有它关心的是它处理的任何对象是来自AbstractModel