The Labs \ Source Viewer \ SSCLI \ System.Windows.Forms \ ImageListUtils

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ImageListUtils.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.Windows.Forms
  18. {
  19.     using System;
  20.     using System.ComponentModel;
  21.     using System.Diagnostics;
  22.    
  23.     // Miscellaneous utilities
  24.     static internal class ImageListUtils
  25.     {
  26.        
  27.         public static PropertyDescriptor GetImageListProperty(PropertyDescriptor currentComponent, ref object instance)
  28.         {
  29.             if (instance is object[])
  30.                 //multiple selection is not supported by this class
  31.                 return null;
  32.            
  33.             PropertyDescriptor imageListProp = null;
  34.             object parentInstance = instance;
  35.            
  36.             RelatedImageListAttribute relILAttr = currentComponent.Attributes[typeof(RelatedImageListAttribute)] as RelatedImageListAttribute;
  37.             if (relILAttr != null) {
  38.                 string[] pathInfo = relILAttr.RelatedImageList.Split('.');
  39.                 for (int i = 0; i < pathInfo.Length; i++) {
  40.                     if (parentInstance == null) {
  41.                         Debug.Fail("A property specified in the path is null or not yet instanciated at this time");
  42.                         break;
  43.                     }
  44.                     PropertyDescriptor prop = TypeDescriptor.GetProperties(parentInstance)[pathInfo[i]];
  45.                     if (prop == null) {
  46.                         Debug.Fail("The path specified to the property is wrong");
  47.                         break;
  48.                     }
  49.                     if (i == pathInfo.Length - 1) {
  50.                         // we're on the last one, look if that's our guy
  51.                         if (typeof(ImageList).IsAssignableFrom(prop.PropertyType)) {
  52.                             instance = parentInstance;
  53.                             imageListProp = prop;
  54.                             break;
  55.                         }
  56.                     }
  57.                     else {
  58.                         parentInstance = prop.GetValue(parentInstance);
  59.                     }
  60.                 }
  61.             }
  62.            
  63.             return imageListProp;
  64.         }
  65.     }
  66. }

Developer Fusion