Ejemplo n.º 1
0
        /// <summary>
        ///     Initializes a new instance of the MikValSor.Immutable.ImmutablCollection`1 class that is a immutable wrapper around the specified list.
        /// </summary>
        /// <param name="list">
        ///     The list to wrap.
        ///</param>
        ///<exception cref="ArgumentNullException">
        ///		List is null.
        /// </exception>
        ///<exception cref="ArgumentException">
        ///		List contains mutable element.
        /// </exception>
        public ImmutableCollection(IList <T> list) : base()
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            m_array = Enumerable.ToArray(list);

            var validator = new ImmutableValidator();

            if (!validator.IsImmutable(typeof(T)))
            {
                for (var i = 0; i < m_array.Length; i++)
                {
                    try
                    {
                        validator.EnsureImmutable(m_array[i]);
                    }
                    catch (NotImmutableException e)
                    {
                        throw new ArgumentException($"List element at index {i}, was not immutable.", nameof(list), e);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void ImmutableCollection_Immutable_object_char()
        {
            //Arrange
            var    validator = new MikValSor.Immutable.ImmutableValidator();
            object target    = new ImmutableCollection <char>(new char[0]);

            //Act
            validator.EnsureImmutable(target);
            var actual = validator.IsImmutable(target);

            //Assert
            Assert.IsTrue(actual);
        }