Autofac TScanningActivatorData 和 WithMetadata

发布于 2024-10-01 06:54:11 字数 975 浏览 0 评论 0原文

我正在尝试做这样的事情。然而 WithMetadata 方法不允许我这么做。

这是 Autofac 中的问题吗?是否应该将 WithMetadata 重载中的 TScanningActivatorData 更改为 TActivatorData,还是我以错误的方式处理此问题?

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

这给了我 WithMetadata 方法上的错误: 类型 'Autofac.Builder.ConcreteReflectionActivatorData' 不能用作通用类型或方法 'Autofac.RegistrationExtensions.WithMetadata中的类型参数 'TScanningActivatorData' (Autofac.Builder.IRegistrationBuilder, 字符串, System.Func)'。没有从“Autofac.Builder.ConcreteReflectionActivatorData”到“Autofac.Features.Scanning.ScanningActivatorData”的隐式引用转换。

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.ConcreteReflectionActivatorData' to 'Autofac.Features.Scanning.ScanningActivatorData'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-10-08 06:54:11

对于您想要实现的目标,有更合适的重载。传递给委托的 t 参数与 myType 相同 - 因此等效代码是:

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

您一直在查看的重载用于扫描注册,例如当使用 RegisterAssemblyTypes() 而不是 RegisterType() 时。

希望这有帮助。
缺口

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文