C# 解析服务表单WindsorContainer时如何注入依赖项?

C# 解析服务表单WindsorContainer时如何注入依赖项?,c#,castle-windsor,C#,Castle Windsor,以下是我的设想: 我有一个ISampleProvider接口: public interface ISampleProvider<TEntity> { TEntity Entity{get;} } public class SampleProvider<TEntity>:ISampleProvider<TEntity> { public SampleProvider(TEntity entity) { Entity=entity; } public TEnt

以下是我的设想: 我有一个ISampleProvider接口:

public interface ISampleProvider<TEntity>
{
TEntity Entity{get;}
}
public class SampleProvider<TEntity>:ISampleProvider<TEntity>
{
public SampleProvider(TEntity entity)
{
Entity=entity;
}
public TEntity Entity
{
get;private set;
}
}
公共接口是示例提供程序
{
tenty实体{get;}
}
下面是这个接口的一个实现:

public interface ISampleProvider<TEntity>
{
TEntity Entity{get;}
}
public class SampleProvider<TEntity>:ISampleProvider<TEntity>
{
public SampleProvider(TEntity entity)
{
Entity=entity;
}
public TEntity Entity
{
get;private set;
}
}
public类SampleProvider:ISampleProvider
{
公共样本提供者(TEntity实体)
{
实体=实体;
}
公共权力实体
{
获得;私人设置;
}
}
当我从WindsorContainer解析时,我想将一个实体注入SampleProvider 所以我写了这个:

var container=new WindsorContainer();
container.AddComponent("smaple_provider",typeof(ISampleProvider<Person>),typeof(SampleProvider<Person>));
var arguments=new Hashtable{{"entity",new Person()}};
var sampleProvider=container.Resolve<ISampleProvider<Person>>(arguments);
var容器=新的WindsorContainer();
AddComponent(“smaple_provider”、typeof(ISampleProvider)、typeof(SampleProvider));
var arguments=newhashtable{{“entity”,newperson()};
var sampleProvider=container.Resolve(参数);
但它不起作用,并且引发了一个依赖项解析程序异常,该异常表示“在配置中检测到循环”


显然我做错了什么。

确保你有温莎2.0。。。 这个测试对我来说很好:

[TestFixture]
public class SampleProviderTests {
    public interface ISampleProvider<TEntity> {
        TEntity Entity { get; }
    }

    public class SampleProvider<TEntity> : ISampleProvider<TEntity> {
        public SampleProvider(TEntity entity) {
            Entity = entity;
        }

        public TEntity Entity { get; private set; }
    }

    public class Person {}

    [Test]
    public void test() {
        var container = new WindsorContainer();
        container.AddComponent("smaple_provider", typeof(ISampleProvider<Person>), typeof(SampleProvider<Person>));
        var arguments = new Hashtable { { "entity", new Person() } };
        var sampleProvider = container.Resolve<ISampleProvider<Person>>(arguments);            
    }
}
[TestFixture]
公共类SampleProviderTests{
公共接口ISampleProvider{
tenty实体{get;}
}
公共类SampleProvider:ISampleProvider{
公共样本提供者(TEntity实体){
实体=实体;
}
公共tenty实体{get;private set;}
}
公共类人士{}
[测试]
公开无效测试(){
var container=新的WindsorContainer();
AddComponent(“smaple_provider”、typeof(ISampleProvider)、typeof(SampleProvider));
var arguments=newhashtable{{“entity”,newperson()};
var sampleProvider=container.Resolve(参数);
}
}