【问题标题】:Unity.RegistrationByConvention.WithXXX members are accessed like properties when they are actually methodsUnity.RegistrationByConvention.WithXXX 成员在实际上是方法时像属性一样被访问
【发布时间】:2014-08-01 07:34:19
【问题描述】:
在大多数示例 here、here 和 here 中,我看到程序集 Microsoft.Practices.Unity.RegistrationByConvention.dll 版本 3.5.0 中的类 WithMappings、WithLifetime 和 WithName 上的方法被访问为虽然它们是属性,但是当我在 Reflector 中查找它们时,我发现它们是方法,并且它们期望(主要)是 System.Type 或 System.Type 的数组。
有什么关系?这些例子是错的还是我遗漏了什么?
【问题讨论】:
标签:
c#
.net
inversion-of-control
unity-container
ioc-container
【解决方案1】:
如果查看container.RegisterTypes 方法的定义,您会看到getFromTypes、getName、getLifetimeManager 和getInjectionMembers 都期望Func 类型,这意味着它们期望一个方法。例如 getName 定义为:
Func<Type, string> getName = null
这允许您传入一个接受 Type 参数并返回字符串的方法,例如标准的string WithName.TypeName(Type type) 方法。但是,如果您想为 Unity 或示例提供自定义注册命名逻辑,它也为您提供了定义具有相同签名的自己的方法的灵活性。