2010-11-09 67 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