Wrap an enumerable so that clients can't get to the underlying implementation via a down-case

image_pdfimage_print
   
 
        
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2007, Wintellect
//
// Use and restribution of this code is subject to the license agreement 
// contained in the file "License.txt" accompanying this file.
//******************************

using System;
using System.Collections;
using System.Collections.Generic;


namespace Wintellect.PowerCollections
{
  /// <summary>
  /// A holder class for various internal utility functions that need to be shared.
  /// </summary>
    internal static class Util
    {

        /// <summary>
        /// Wrap an enumerable so that clients can&#039;t get to the underlying
        /// implementation via a down-case
        /// </summary>
        /// <param name="wrapped">Enumerable to wrap.</param>
        /// <returns>A wrapper around the enumerable.</returns>
        public static IEnumerable<T> CreateEnumerableWrapper<T>(IEnumerable<T> wrapped)
        {
            return new WrapEnumerable<T>(wrapped);
        }
        /// <summary>
        /// Wrap an enumerable so that clients can&#039;t get to the underlying 
        /// implementation via a down-cast.
        /// </summary>
        class WrapEnumerable<T> : IEnumerable<T>
        {
            IEnumerable<T> wrapped;

            /// <summary>
            /// Create the wrapper around an enumerable.
            /// </summary>
            /// <param name="wrapped">IEnumerable to wrap.</param>
            public WrapEnumerable(IEnumerable<T> wrapped)
            {
                this.wrapped = wrapped;
            }

            public IEnumerator<T> GetEnumerator()
            {
                return wrapped.GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return ((IEnumerable)wrapped).GetEnumerator();
            }
        }
   }
}        

   
     


This entry was posted in Data Types. Bookmark the permalink.