ContravarianceAdapter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / AddIn / AddIn / System / Addin / Hosting / ContravarianceAdapter.cs / 1305376 / ContravarianceAdapter.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
/*============================================================
** 
** Value Type:  ContravarianceAdapter 
**
** Purpose: To convert an IEnumerable to an IEnumerable 
**
===========================================================*/
using System;
using System.Collections; 
using System.Collections.Generic;
using System.Diagnostics.Contracts; 
 
namespace System.AddIn.Hosting
{ 
    internal struct ContravarianceAdapter : IEnumerable where Derived : Base
    {
        private IEnumerable _enumerable;
 
        internal ContravarianceAdapter(IEnumerable collection)
        { 
            _enumerable = collection; 
        }
 
        public IEnumerator GetEnumerator()
        {
            return new ContravariantEnumerator(_enumerable.GetEnumerator());
        } 

        IEnumerator IEnumerable.GetEnumerator() 
        { 
            return new ContravariantEnumerator(_enumerable.GetEnumerator());
        } 

        internal struct ContravariantEnumerator : IEnumerator
        {
            IEnumerator _enumerator; 

            internal ContravariantEnumerator(IEnumerator enumerator) 
            { 
                _enumerator = enumerator;
            } 

            public bool MoveNext()
            {
                return _enumerator.MoveNext(); 
            }
 
            public Derived Current 
            {
                get { return (Derived)_enumerator.Current; } 
            }

            Object IEnumerator.Current
            { 
                get { return ((IEnumerator)_enumerator).Current; }
            } 
 
            public void Reset()
            { 
                _enumerator.Reset();
            }

            public void Dispose() 
            {
                _enumerator.Dispose(); 
            } 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
/*============================================================
** 
** Value Type:  ContravarianceAdapter 
**
** Purpose: To convert an IEnumerable to an IEnumerable 
**
===========================================================*/
using System;
using System.Collections; 
using System.Collections.Generic;
using System.Diagnostics.Contracts; 
 
namespace System.AddIn.Hosting
{ 
    internal struct ContravarianceAdapter : IEnumerable where Derived : Base
    {
        private IEnumerable _enumerable;
 
        internal ContravarianceAdapter(IEnumerable collection)
        { 
            _enumerable = collection; 
        }
 
        public IEnumerator GetEnumerator()
        {
            return new ContravariantEnumerator(_enumerable.GetEnumerator());
        } 

        IEnumerator IEnumerable.GetEnumerator() 
        { 
            return new ContravariantEnumerator(_enumerable.GetEnumerator());
        } 

        internal struct ContravariantEnumerator : IEnumerator
        {
            IEnumerator _enumerator; 

            internal ContravariantEnumerator(IEnumerator enumerator) 
            { 
                _enumerator = enumerator;
            } 

            public bool MoveNext()
            {
                return _enumerator.MoveNext(); 
            }
 
            public Derived Current 
            {
                get { return (Derived)_enumerator.Current; } 
            }

            Object IEnumerator.Current
            { 
                get { return ((IEnumerator)_enumerator).Current; }
            } 
 
            public void Reset()
            { 
                _enumerator.Reset();
            }

            public void Dispose() 
            {
                _enumerator.Dispose(); 
            } 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK