Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AutoFac和Lazy<;T、 TMetadata>;w/属性_C#_Asp.net Web Api_Autofac - Fatal编程技术网

C# AutoFac和Lazy<;T、 TMetadata>;w/属性

C# AutoFac和Lazy<;T、 TMetadata>;w/属性,c#,asp.net-web-api,autofac,C#,Asp.net Web Api,Autofac,我在使用AutoFacv3.5.2的Asp.Net Web Apiv2中遇到以下问题 我基本上不能让IEnumerable给我任何东西IEnumerable返回信息,IEnumerable返回结果 我不知道还有什么细节需要补充,但我可以根据需要补充 示例属性 public class FooStrategyAttribute: Attribute { public FooStrategyAttribute(StrategyType strategyType) {

我在使用AutoFacv3.5.2的Asp.Net Web Apiv2中遇到以下问题

我基本上不能让
IEnumerable
给我任何东西
IEnumerable
返回信息,
IEnumerable
返回结果

我不知道还有什么细节需要补充,但我可以根据需要补充

示例属性

public class FooStrategyAttribute: Attribute
{
    public FooStrategyAttribute(StrategyType strategyType)
    {
        StrategyType = strategyType;
    }

    public StrategyType StrategyType { get; set; }
}
[FooStrategy(StrategyType.StrategyOne)]
public class Handler: IHandler
{
}
示例:属性的应用

public class FooStrategyAttribute: Attribute
{
    public FooStrategyAttribute(StrategyType strategyType)
    {
        StrategyType = strategyType;
    }

    public StrategyType StrategyType { get; set; }
}
[FooStrategy(StrategyType.StrategyOne)]
public class Handler: IHandler
{
}
示例:接口的注册:

 builder.RegisterAssemblyTypes(assemblies)
            .Where(t => t.GetCustomAttributes<FooStrategyAttribute>().Any())
            .WithMetadataFrom<FooStrategyAttribute>()
            .AsImplementedInterfaces();
builder.RegisterAssemblyTypes(程序集)
.Where(t=>t.GetCustomAttributes().Any())
.WithMetadataFrom()
.a实现接口();
正在注入的构造函数:

public Thingy(IEnumerable<Lazy<IHandler, FooStrategyAttribute>> handlers)
{
    this.handlers= handlers;
}
public Thingy(IEnumerable处理程序)
{
this.handlers=handlers;
}

是的,我试图匿名化代码,但错过了。让我来解决它。您尝试解析
IFoo
,而注册
IHandler
。这正常吗?请参阅工作示例谢谢您的评论,我适当地更新了示例。您是否尝试了我的代码示例?它在我的机器上工作样品在我的机器上不工作。它说的是需要懒惰,显然不认识懒惰,所以我试图弄清楚这是否只是我没有正确地使用dotnetfiddle