ListContractAdapter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / AddIn / AddIn / System / Addin / Pipeline / ListContractAdapter.cs / 1305376 / ListContractAdapter.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
/*============================================================
** 
** Class: ListContractAdapter 
**
** 
===========================================================*/
using System;
using System.Collections.Generic;
using System.AddIn.Contract; 
using System.Diagnostics.Contracts;
 
namespace System.AddIn.Pipeline 
{
    internal class ListContractAdapter : ContractBase, IListContract 
    {
        private IList m_list;
        private Converter m_wrapper;
        private Converter m_unwrapper; 

        public ListContractAdapter(IList source, Converter wrapper, Converter unwrapper) 
        { 
            if (source == null)
                throw new ArgumentNullException("source"); 
            if (wrapper == null)
                throw new ArgumentNullException("wrapper");
            if (unwrapper == null)
                throw new ArgumentNullException("unwrapper"); 
            if (!typeof(U).IsSerializable && !typeof(IContract).IsAssignableFrom(typeof(U)))
                throw new ArgumentException(Res.TypeShouldBeSerializableOrIContract, typeof(U).Name); 
            System.Diagnostics.Contracts.Contract.EndContractBlock(); 

            m_list = source; 
            m_wrapper = wrapper;
            m_unwrapper = unwrapper;
        }
 
        public void Add(U item)
        { 
            m_list.Add(m_unwrapper(item)); 
        }
 
        public void Clear()
        {
            m_list.Clear();
        } 

        public bool Contains(U item) 
        { 
            return m_list.Contains(m_unwrapper(item));
        } 

        public int IndexOf(U item)
        {
            return m_list.IndexOf(m_unwrapper(item)); 
        }
 
        public void Insert(int index, U item) 
        {
            m_list.Insert(index, m_unwrapper(item)); 
        }

        public bool Remove(U item)
        { 
            return m_list.Remove(m_unwrapper(item));
        } 
 
        public void RemoveAt(int index)
        { 
            m_list.RemoveAt(index);
        }

        public U GetItem(int index) 
        {
            return m_wrapper(m_list[index]); 
        } 

        public void SetItem(int index, U item) 
        {
            m_list[index] = m_unwrapper(item);
        }
 
        public int GetCount()
        { 
            return m_list.Count; 
        }
 
        public bool GetIsReadOnly()
        {
            return m_list.IsReadOnly;
        } 

        public IEnumeratorContract GetEnumeratorContract() 
        { 
            return new ListEnumeratorAdapter(m_list.GetEnumerator(), m_wrapper);
        } 
    }

    internal class ListEnumeratorAdapter : ContractBase, IEnumeratorContract
    { 
        IEnumerator m_enumerator;
        Converter m_wrapper; 
 
        public ListEnumeratorAdapter(IEnumerator enumerator, Converter wrapper)
        { 
            if (enumerator == null)
                throw new ArgumentNullException("enumerator");
            if (wrapper == null)
                throw new ArgumentNullException("wrapper"); 
            System.Diagnostics.Contracts.Contract.EndContractBlock();
 
            m_enumerator = enumerator; 
            m_wrapper = wrapper;
        } 

        public U GetCurrent()
        {
            return m_wrapper(m_enumerator.Current); 
        }
 
        public bool MoveNext() 
        {
            return m_enumerator.MoveNext(); 
        }

        public void Reset()
        { 
            m_enumerator.Reset();
        } 
 
        public void Dispose()
        { 
            m_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