/// <summary> /// Converts to read only <see cref="IPropertyContainer"/> if needed. /// Returns read only copy of <paramref name="propertyContainer"/> if it is <see cref="IMutablePropertyContainer"/>. /// Or returns the same container. /// </summary> /// <param name="propertyContainer">Source property container.</param> /// <param name="flattenHierarchy">Flatten container hierarchy.</param> /// <returns><see cref="IPropertyContainer"/>.</returns> public static IPropertyContainer ToReadOnly(this IPropertyContainer propertyContainer, bool flattenHierarchy = true) { propertyContainer.AssertArgumentNotNull(nameof(propertyContainer)); if (propertyContainer is IMutablePropertyContainer) { if (flattenHierarchy && propertyContainer.ParentSource != null && propertyContainer.ParentSource.Count > 0) { return(propertyContainer .Flatten() .ToReadOnly(flattenHierarchy: false)); } return(new PropertyContainer( sourceValues: propertyContainer.Properties, parentPropertySource: propertyContainer.ParentSource, searchOptions: propertyContainer.SearchOptions)); } return(propertyContainer); }