Returns a value indicating whether a collection is read-only. - CSharp System.Collections.ObjectModel

CSharp examples for System.Collections.ObjectModel:ReadOnlyCollection

Description

Returns a value indicating whether a collection is read-only.

Demo Code

// (c) Copyright Microsoft Corporation.
using System.Reflection;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.ComponentModel;
using System.Collections;
using System;//from w  w w  .  j a  v  a2s .c o m

public class Main{
        /// <summary>
        /// Returns a value indicating whether a collection is read-only.
        /// </summary>
        /// <param name="collection">The collection to examine.</param>
        /// <returns>A value indicating whether a collection is read-only.</returns>
        public static bool IsReadOnly(this IEnumerable collection)
        {
            return
                collection.GetType().IsArray
                    || EnumerableExtensions
                       .Iterate(collection.GetType(), type => type.BaseType)
                       .TakeWhile(type => type != null)
                       .Any(type => type.FullName.StartsWith("System.Collections.ObjectModel.ReadOnlyCollection`1", StringComparison.Ordinal));
        }
}

Related Tutorials