开发者

Autofac TScanningActivatorData and WithMetadata

开发者 https://www.devze.com 2023-01-23 23:21 出处:网络
I am trying to do something like this. However the WithMetadata method wont let me. Is this a problem in Autofac and should TScanningActivatorData in the WithMetadata overloads be changed to TActivat

I am trying to do something like this. However the WithMetadata method wont let me.

Is this a problem in Autofac and should TScanningActivatorData in the WithMetadata overloads be changed to TActivatorData, or am i approaching this the wrong way?

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

This gives me the error on the WithMetadata method: 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.Concrete开发者_StackOverflow中文版ReflectionActivatorData' to 'Autofac.Features.Scanning.ScanningActivatorData'.


There's a more suitable overload for what you're trying to achieve. The t parameter passed in to the delegate is the same as myType - so the equivalent code is:

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

The overload you've been looking at is for use with scanning registrations, e.g. when using RegisterAssemblyTypes() rather than RegisterType().

Hope this helps. Nick

0

精彩评论

暂无评论...
验证码 换一张
取 消