2010-11-09 61 views
1

我正在尝试这样做。但WithMetadata方法不会让我。Autofac TScanningActivatorData和WithMetadata

这是Autofac中的一个问题,并且WithMetadata重载中的TScanningActivatorData应该更改为TActivatorData,还是我以错误的方式处理?

builder.RegisterType(myType).As<IMyType().AsSelf().WithMetadata("somekey", delegate(Type t) 
       { 
        //dosomething 
        return t; 
       }); 

这给我的错误就为metadata方法:The type 'Autofac.Builder.ConcreteReflectionActivatorData' cannot be used as type parameter 'TScanningActivatorData' in the generic type or method 'Autofac.RegistrationExtensions.WithMetadata<TLimit,TScanningActivatorData,TRegistrationStyle>(Autofac.Builder.IRegistrationBuilder<TLimit,TScanningActivatorData,TRegistrationStyle>, string, System.Func<System.Type,object>)'. There is no implicit reference conversion from 'Autofac.Builder.ConcreteReflectionActivatorData' to 'Autofac.Features.Scanning.ScanningActivatorData'.

回答

1

有一个为你想实现什么更合适的过载。在传递给委托的t参数是一样的myType - 这样的等效代码为:

var someValue = DoSomething(myType); 
builder.RegisterType(myType) 
    .As<IMyType>() 
    .AsSelf() 
    .WithMetadata("somekey", someValue); 

你一直在寻找的过载是用于扫描登记,例如使用当使用RegisterAssemblyTypes()而不是RegisterType()

希望这会有所帮助。 尼克

+0

这工作thx !.你提到扫描注册。是否可以扫描一组类型而不是一个Assembly?那会更完美。 – Danthar 2010-11-10 08:07:16

+0

foreach(var myTypes){builder.RegisterType(t)...} :) – 2010-11-12 23:22:43