Add a list of items into the dictionary. : DictionaryBase « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » DictionaryBase 




Add a list of items into the dictionary.
  

#region License and Copyright
/* -------------------------------------------------------------------------
 * Dotnet Commons Collections
 *
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 * for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this library; if not, write to the 
 
 * Free Software Foundation, Inc., 
 * 59 Temple Place, 
 * Suite 330, 
 * Boston, 
 * MA 02111-1307 
 * USA 
 
 * -------------------------------------------------------------------------
 */
#endregion

using System;
using System.Collections;
using System.Text;


namespace Dotnet.Commons.Collections
{

  ///  
  /// <summary>  
  /// 
  /// This utility class extends the operations for manipulating 
  /// Dictionary collections.
  ///
  ///
  /// </summary>  
  /// 
  public class DictionaryUtils
  {
    private DictionaryUtils()
    {      
    }

    /// <summary>
    /// Add a list of items into the dictionary. 
    /// 
    /// </summary>
    /// <param name="list">List that contains items that are to be added to the dictionary</param>
    /// <param name="dictionary">dictionary to be added with items from list</param>
    /// <param name="propertyNameKey">Name of property to be use as key</param>
    public static void AddToDictionary(IList list, 
                      ref IDictionary dictionary,
                      string propertyNameKey)
    {
      foreach (object obj in list)
      {

        //Generate a key for the object

        object objKey = null;

        if (propertyNameKey != null)
        {
                    object propertyValue = obj.GetType().GetProperty(propertyNameKey);

          // only Property value as key if not null
          if (propertyValue != null)
            objKey = propertyValue;          
        }        
        
        if (objKey == null)
        {        
          objKey = obj.GetType().FullName + "@" + obj.GetHashCode().ToString();          
        }


        // if a similar object already exists, simply replace it. Otherwise add
        // to the dictionary

        if (dictionary[objKey!= null)
          dictionary[objKey= obj;          
        else
          dictionary.Add(objKey, obj);
      }
    }

    }
}

   
    
  














Related examples in the same category
1.extends DictionaryBase
2.Copy/Clone all the element from a dictionary to another.
3.Put an element into the Dictionary
4.Add one dictionary items into another. Copy all the first dictionary's items into the second dictionary.
5.A dictionary with keys of type string and values of type String
6.A dictionary with keys of type string and values of type Type
7.Dictionary to anonymous type
8.Two Key Dictionary
9.DictionaryBase Class Provides the abstract base class for a strongly typed collection of key/value pairs.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.