温莎城堡:如何注册内部实施 [英] Castle Windsor: How to register internal implementations

查看:45
本文介绍了温莎城堡:如何注册内部实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当所有IService的实现都公开时,此注册有效:

This registration works when all the implementations of IService are public:

AllTypes
    .Of<IService>()
    .FromAssembly(GetType().Assembly)
    .WithService.FirstInterface()

例如:

public interface IService {}
public interface ISomeService : IService {}
public class SomeService : ISomeService {}

解析ISomeService返回SomeService的实例。

Resolving ISomeService returns an instance of SomeService.

但是,如果SomeService类是内部的,则容器会抛出一个异常,指出未注册ISomeService。

But if the SomeService class is internal, the container throws an exception that states that ISomeService is not registered.

那么有什么办法吗为公共接口注册内部实现?

So is there any way to register internal implementations for public interfaces?

推荐答案

UPDATE2:
这不是一个好主意,您可以使用以下代码来实现。

UPDATE2: While I consider this not a good idea, you can achieve that with the following code.

AllTypes.FromAssembly(GetType().Assembly)
    .IncludeNonPublicTypes()
    .BasedOn<IService>()
    .WithService.FirstInterface();






更新:
这是一个非常合乎逻辑的行为,如果不是这样,我会认为这是一个错误。 AllTypes仅公开公共类型,因为它假定,如果您将类型设置为内部,则是有原因的,如果公开,则可能是安全问题。


UPDATE: This is a very logical behavior and I would consider this a bug if it was otherwise. AllTypes exposes only public type, because it assumes, if you made your type internal, you did it for a reason, and if it exposed it it could be a security issue.

另一方面,当您显式注册类型时,它是有效的,因为Windsor假定,因为您明确要求它使用内部类型,所以您会更了解。

When you register type explicitly on the other hand, it works because Windsor assumes, since you're explicitly asking it to use an internal type, you know better.

这篇关于温莎城堡:如何注册内部实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆