Click here to Skip to main content
15,885,872 members
Articles / Programming Languages / C#

A .NET Wizard control

Rate me:
Please Sign up or sign in to vote.
4.86/5 (89 votes)
24 Apr 2003CPOL7 min read 660.4K   8.7K   216  
A .NET Wizard control for the VS.IDE and client apps
using System;
using System.Collections;
using System.Reflection;


namespace UtilityLibrary.General
{
  /// <summary>
  /// Special method which can be useful in work with arrays
  /// </summary>
  public class ArrayUtils
  {
    /// <summary>
    /// Private constructor to prevent creation of class instance
    /// </summary>
    private ArrayUtils(){}
    /// <summary>
    /// Copy property value of object stored in source into destination array.
    /// </summary>
    /// <param name="source">List of object which property must be read</param>
    /// <param name="array">Destination array</param>
    /// <param name="property">Source Object Property Name</param>
    /// <param name="startIndx">Start index of source object</param>
    public static void CopyTo( IList source, Array array, string property, int index )
    {
      ArrayList list = new ArrayList( source.Count );
      PropertyInfo keyProp = null;

      int iCount = 0;
      foreach( object obj in source )
      {
        if( keyProp == null )
        {
          keyProp =  obj.GetType().GetProperty( property );
          if( keyProp == null )
            throw new ArgumentException( "Property Name of object is wrong", "property" );
        }

        if( iCount >= index )
        {
          list.Add( keyProp.GetValue( obj, null ) );
        }

        iCount++;
      }

      list.CopyTo( array );
      list.Clear();
    }
    /// <summary>
    /// Copy property value of object stored in source into destination array.
    /// </summary>
    /// <param name="source">List of object which property must be read</param>
    /// <param name="array">Destination array</param>
    /// <param name="property">Source Object Property Name</param>
    public static void CopyTo( IList source, Array array, string property )
    {
      CopyTo( source, array, property, 0 );
    }
    /// <summary>
    /// Copy property value of object stored in source into destination array.
    /// </summary>
    /// <param name="source">Source array of objects</param>
    /// <param name="array">Destination array</param>
    /// <param name="property">Source Object Property Name</param>
    /// <param name="index">Start index in source array</param>
    public static void CopyTo( ICollection source, Array array, string property, int index )
    {
      ArrayList list = new ArrayList( source.Count );
      IEnumerator enums = source.GetEnumerator();
      int iCount = 0;
      
      PropertyInfo keyProp = null;

      while( enums.MoveNext() )
      {
        if( keyProp == null )
        {
          keyProp = enums.Current.GetType().GetProperty( property );
          
          if( keyProp == null )
            throw new ArgumentException( "Property Name of object is wrong", "property" );
        }

        if( iCount >= index )
          list.Add( keyProp.GetValue( enums.Current, null ) );

        iCount++;
      }

      list.CopyTo( array );
      list.Clear();
    }
    /// <summary>
    /// Copy property value of object stored in source into destination array.
    /// </summary>
    /// <param name="source">Source array of objects</param>
    /// <param name="array">Destination array</param>
    /// <param name="property">Source Object Property Name</param>
    public static void CopyTo( ICollection source, Array array, string property )
    {
      CopyTo( source, array, property, 0 );
    }
    /// <summary>
    /// Create from array of objects and dictionary.
    /// </summary>
    /// <param name="source">Source array of objects</param>
    /// <param name="output">Dictionary which will be filled in excution</param>
    /// <param name="key">Name of key propererty</param>
    /// <param name="value">Name of Value property</param>
    public static void CopyTo( ICollection source, IDictionary output, string key, string value )
    {
      IEnumerator enums = source.GetEnumerator();
      PropertyInfo keyProp = null, valProp = null;

      while( enums.MoveNext() )
      {
        if( keyProp == null )
        {
          keyProp = enums.Current.GetType().GetProperty( key );
          valProp = enums.Current.GetType().GetProperty( value );
          
          if( keyProp == null )
            throw new ArgumentException( "Property Name of object is wrong", "key" );
          
          if( valProp == null )
            throw new ArgumentException( "Property Name of object is wrong", "value" );
        }

        object keyVal = keyProp.GetValue( enums.Current, null );
        object valVal = valProp.GetValue( enums.Current, null );
        
        output[ keyVal ] = valVal;
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO ArtfulBits Inc.
Ukraine Ukraine
Name:Kucherenko Oleksandr

Born:September 20, 1979

Platforms: Win32, Linux; - well known and MS-DOS; Win16; OS/2 - old time not touched;

Hardware: IBM PC

Programming Languages: Assembler (for Intel 80386); Borland C/C++; Borland Pascal; Object Pascal; Borland C++Builder; Delphi; Perl; Java; Visual C++; Visual J++; UML; XML/XSL; C#; VB.NET; T-SQL; PL/SQL; and etc.

Development Environments: MS Visual Studio 2001-2008; MS Visual C++; Borland Delphi; Borland C++Builder; C/C++ any; Rational Rose; GDPro; Together and etc.

Libraries: STL, ATL, WTL, MFC, NuMega Driver Works, VCL; .NET 1.0, 1.1, 2.0, 3.5; and etc.

Technologies: Client/Server; COM; DirectX; DirectX Media; BDE; HTML/DHTML; ActiveX; Java Servlets; DCOM; COM+; ADO; CORBA; .NET; Windows Forms; GDI/GDI+; and etc.

Application Skills: Databases - design and maintain, support, programming; GUI Design; System Programming, Security; Business Software Development. Win/Web Services development and etc.

Comments and Discussions