/// <summary> /// Converts <see cref="IPropertyContainer"/> to <see cref="IPropertyContainer"/> of type <paramref name="outputType"/>. /// </summary> /// <param name="propertyContainer">Source property container.</param> /// <param name="outputType">Result type.</param> /// <param name="returnTheSameIfNoNeedToConvert">Returns the same instance if source container is already matches <paramref name="outputType"/>.</param> /// <returns>Result property container.</returns> public static IPropertyContainer ToPropertyContainerOfType(this IPropertyContainer propertyContainer, Type outputType, bool returnTheSameIfNoNeedToConvert = true) { if (returnTheSameIfNoNeedToConvert && propertyContainer.IsAssignableTo(outputType)) { return(propertyContainer); } if (outputType == typeof(IMutablePropertyContainer) || outputType == typeof(MutablePropertyContainer)) { return(new MutablePropertyContainer(propertyContainer.Properties, propertyContainer.ParentSource, propertyContainer.SearchOptions)); } if (outputType == typeof(IPropertyContainer) || outputType == typeof(PropertyContainer)) { return(new PropertyContainer(propertyContainer.Properties, propertyContainer.ParentSource, propertyContainer.SearchOptions)); } if (outputType.IsConcreteType()) { /* * public PropertyContainer( * IEnumerable<IPropertyValue>? sourceValues = null, * IPropertyContainer? parentPropertySource = null, * SearchOptions? searchOptions = null) */ object?[] ctorArgs = { propertyContainer.Properties, propertyContainer.ParentSource, propertyContainer.SearchOptions }; IPropertyContainer resultContainer = (IPropertyContainer)Activator.CreateInstance(outputType, args: ctorArgs); return(resultContainer); } // Return ReadOnly PropertyContainer as a result. return(new PropertyContainer(propertyContainer.Properties, propertyContainer.ParentSource, propertyContainer.SearchOptions)); }