/// <summary> /// 解析类型参数映射集合(默认以定义参数类型名称为键名)。 /// </summary> /// <param name="accessorTypeImplementation">给定的访问器类型实现。</param> /// <returns>返回 <see cref="TypeParameterMappingCollection"/>。</returns> public static TypeParameterMappingCollection ParseCollection(Type accessorTypeImplementation) { var accessorTypeDefinition = typeof(IContentAccessor <, , , , , , , , ,>); // 因访问器默认服务类型为 IAccessor,所以不强制实现访问器泛型类型定义 if (!accessorTypeImplementation.IsImplementedInterfaceType(accessorTypeDefinition, out var resultType)) { return(null); } return(TypeParameterMappingHelper.ParseCollection(accessorTypeDefinition, resultType)); }
/// <summary> /// 解析类型参数映射集合(默认以定义参数类型名称为键名)。 /// </summary> /// <param name="accessorTypeImplementation">给定的访问器类型实现。</param> /// <param name="accessorMapping">输出访问器 <see cref="TypeParameterMapping"/>。</param> /// <returns>返回 <see cref="TypeParameterMappingCollection"/>。</returns> public static TypeParameterMappingCollection ParseCollection(Type accessorTypeImplementation, out TypeParameterMapping accessorMapping) { var accessorTypeDefinition = typeof(IDataAccessor <, , , ,>); // 因访问器默认服务类型为 IAccessor,所以不强制实现访问器泛型类型定义 if (!accessorTypeImplementation.IsImplementedInterfaceType(accessorTypeDefinition, out var resultType)) { accessorMapping = null; return(null); } accessorMapping = new TypeParameterMapping(accessorTypeDefinition, accessorTypeImplementation); var mappings = TypeParameterMappingHelper.ParseCollection(accessorTypeDefinition, resultType); mappings.TryFindTypeDefinitionFromValuesAndAddMapping(typeof(IGenerativeIdentifier <>)); mappings.TryFindTypeDefinitionFromValuesAndAddMapping(typeof(IIncrementalIdentifier <>)); mappings.TryFindTypeDefinitionFromValuesAndAddMapping(typeof(ICreation <,>)); return(mappings); }
public void ParseAsDictionaryTest() { var baseType = typeof(INotificationHandler <>); var implType = typeof(INotificationHandler <PingNotification>); var parameters = TypeParameterMappingHelper.ParseCollection(baseType, implType); Assert.Single(parameters); Assert.Equal("TNotification", parameters.Keys.First()); Assert.Equal(nameof(PingNotification), parameters.Values.First().ArgumentName); baseType = typeof(IRequestHandler <,>); implType = typeof(IRequestHandler <PingRequest, Pong>); parameters = TypeParameterMappingHelper.ParseCollection(baseType, implType); Assert.Equal(2, parameters.Count); Assert.Equal("TRequest", parameters.Keys.First()); Assert.Equal("TResponse", parameters.Keys.Last()); Assert.Equal(nameof(PingRequest), parameters.Values.First().ArgumentName); Assert.Equal(nameof(Pong), parameters.Values.Last().ArgumentName); }