我已创建项目并将Interface和类放在同一文件夹中。我使用Unity容器来解析类对象的接口。现在我有另一个想要访问程序集的程序集。我不希望任何人直接访问我的班级。因此,如果可以注册内部类的类型,这对我有好处。有什么想法吗?
答案 0 :(得分:0)
你可以尝试使用Friend assemblies,但我认为从长远来看这只会给你带来麻烦。
相反,我会考虑另外两种选择。
选项A :使用工厂。
此处建议:Unity 1.2 Dependency injection of internal types。
public class FactoryClass
{
public IMyInterface CreateInstance()
{
return new MyInternalClass();
}
}
internal class MyInternalClass : IMyInterface
{
}
还要考虑使用带有内部构造函数的公共类,这会强制内部类创建实例。更多信息请点击此处:What's the difference between a public constructor in an internal class and an internal constructor?。
public class MyAlmostInternalClass // in lack of better name
{
internal MyAlmostInternalClass() {}
}
选项B :完全重新考虑您的课程
为什么需要内部?不管怎么说其他组件都不使用界面?如果您不希望任何人从中获取,并且除了来自其实现的界面之外的任何公共属性,请将其设为sealed。然后,没有理由在界面上使用该类。