Click here to Skip to main content
15,883,901 members
Articles / Programming Languages / C#

Linkify Add-in for Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.59/5 (23 votes)
2 Aug 2008CPOL9 min read 170.4K   433   99  
Link source code comments to your bug tracker, MSDN, development Wiki and more.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

namespace Linkify
{

    class Utility
    {
      public class TaggedString
      {
        string m_label;
        object m_tag;

        public TaggedString()
        {
          m_label = "";
        }

        public TaggedString(string label, object tag)
        {
          m_label = label;
          m_tag = tag;
        }

        public object Tag { get { return m_tag; } set { m_tag = value; } }
        public string Label { get { return m_label; } set { m_label = value; } }

        public override string ToString() { return m_label; }
      }

      public class ComboBoxTagger
      {
        /*
        T m_defaultTag;

        public ComboBoxTagger(ComboBox cb, T defaultTag)
        {
           m_cb = cb;
           m_defaultTag = defaultTag;
        }

        public T DefaultTag { get { return m_defaultTag; } set { m_defaultTag = value; } }
        */

        ComboBox m_cb;

        public ComboBoxTagger()
        {
          m_cb = null;
        }

        public ComboBoxTagger(ComboBox cb)
        {
          m_cb = cb;
        }

        public System.Windows.Forms.ComboBox Control { get { return m_cb; } set { m_cb = value; } }


        public int Add(TaggedString item) { return m_cb.Items.Add(item); }
        public void Insert(int at, TaggedString item) { m_cb.Items.Insert(at, item); }
        public int Add(string s, object tag) { return m_cb.Items.Add(new TaggedString(s, tag)); }
        public void Insert(int at, string s, object tag) { m_cb.Items.Insert(at, new TaggedString(s, tag)); }

        public object SelectedItem
        {
          get
          {
            object selectedItem = m_cb.SelectedItem;
            if (selectedItem == null)
              return null;
            return (selectedItem as TaggedString).Tag;

          }
          set
          {
            int indexToSelect = IndexOf(value);
            m_cb.SelectedIndex = indexToSelect;
          }
        }

        public int IndexOf(object value) { return IndexOf(value, 0); }

        public int IndexOf(object value, int startIdx)
        {
          IComparable valcmp = value as IComparable;

          if (valcmp != null)  // use IComparable if implemented
          {
            for (int i = startIdx; i < m_cb.Items.Count; ++i)
            {
              object tag = (m_cb.Items[i] as TaggedString).Tag;
              if (valcmp.CompareTo(tag) == 0)
                return i;
            }
          }
          else
          {
            for (int i = startIdx; i < m_cb.Items.Count; ++i)
            {
              object tag = (m_cb.Items[i] as TaggedString).Tag;
              if (value == tag)
                return i;
            }
          }
          return -1;
        }

      };


        /*

        /// <summary>
        /// Clone the object, and returning a reference to a cloned object.
        /// </summary>
        /// <returns>Reference to the new cloned 
        /// object.</returns>
        public static object DeepClone(Object o)
        {
            //First we create an instance of this specific type.
            object newObject = Activator.CreateInstance(o.GetType());

            //We get the array of fields for the new type instance.
            FieldInfo[] fields = newObject.GetType().GetFields();

            int i = 0;

            foreach (FieldInfo fi in o.GetType().GetFields())
            {
                //We query if the fields support the ICloneable interface.
                Type ICloneType = fi.FieldType.
                            GetInterface("ICloneable", true);

                if (ICloneType != null)
                {
                    //Getting the ICloneable interface from the object.
                    ICloneable IClone = (ICloneable)fi.GetValue(o);

                    //We use the clone method to set the new value to the field.
                    fields[i].SetValue(newObject, IClone.Clone());
                }
                else
                {
                    // If the field doesn't support the ICloneable 
                    // interface then just set it.
                    fields[i].SetValue(newObject, fi.GetValue(o));
                }

                //Now we check if the object support the 
                //IEnumerable interface, so if it does
                //we need to enumerate all its items and check if 
                //they support the ICloneable interface.
                Type IEnumerableType = fi.FieldType.GetInterface
                                ("IEnumerable", true);
                if (IEnumerableType != null)
                {
                    //Get the IEnumerable interface from the field.
                    IEnumerable IEnum = (IEnumerable)fi.GetValue(o);

                    //o version support the IList and the 
                    //IDictionary interfaces to iterate on collections.
                    Type IListType = fields[i].FieldType.GetInterface
                                        ("IList", true);
                    Type IDicType = fields[i].FieldType.GetInterface
                                        ("IDictionary", true);

                    int j = 0;
                    if (IListType != null)
                    {
                        //Getting the IList interface.
                        IList list = (IList)fields[i].GetValue(newObject);

                        foreach (object obj in IEnum)
                        {
                            //Checking to see if the current item 
                            //support the ICloneable interface.
                            ICloneType = obj.GetType().
                                GetInterface("ICloneable", true);

                            if (ICloneType != null)
                            {
                                //If it does support the ICloneable interface, 
                                //we use it to set the clone of
                                //the object in the list.
                                ICloneable clone = (ICloneable)obj;

                                list[j] = clone.Clone();
                            }

                            //NOTE: If the item in the list is not 
                            //support the ICloneable interface then in the 
                            //cloned list this item will be the same 
                            //item as in the original list
                            //(as long as this type is a reference type).

                            j++;
                        }
                    }
                    else if (IDicType != null)
                    {
                        //Getting the dictionary interface.
                        IDictionary dic = (IDictionary)fields[i].
                                            GetValue(newObject);
                        j = 0;

                        foreach (DictionaryEntry de in IEnum)
                        {
                            //Checking to see if the item 
                            //support the ICloneable interface.
                            ICloneType = de.Value.GetType().
                                GetInterface("ICloneable", true);

                            if (ICloneType != null)
                            {
                                ICloneable clone = (ICloneable)de.Value;

                                dic[de.Key] = clone.Clone();
                            }
                            j++;
                        }
                    }
                }
                i++;
            }
            return newObject;
        }
         */

        /*
        /// <summary>
        /// Implements IClonable, using Reflection
        ///  see http://www.csharpfr.com/code.aspx?ID=34850. Does not work for circular references and similar cases!
        /// </summary>
        public static object DeepClone(object vObj)
        {
            if (vObj.GetType().IsValueType || vObj.GetType() == Type.GetType("System.String"))
                return vObj;
            else
            {
                object newObject = Activator.CreateInstance(vObj.GetType());

                foreach (PropertyInfo Item in newObject.GetType().GetProperties())
                {
                    if (Item.GetType().GetInterface("ICloneable") != null)
                    {
                        ICloneable IClone = (ICloneable)Item.GetValue(vObj, null);
                        Item.SetValue(newObject, IClone.Clone(), null);
                    }
                    else
                        Item.SetValue(newObject, DeepClone(Item.GetValue(vObj, null)), null);
                }

                foreach (FieldInfo Item in newObject.GetType().GetFields())
                {
                    if (Item.GetType().GetInterface("ICloneable") != null)
                    {
                        ICloneable IClone = (ICloneable)Item.GetValue(vObj);
                        Item.SetValue(newObject, IClone.Clone());
                    }
                    else
                        Item.SetValue(newObject, DeepClone(Item.GetValue(vObj)));
                }
                return newObject;
            }
        } 
         * */

    }
}

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
Klippel
Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.

Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.

In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.

Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.


Where are you from?[^]



Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

Comments and Discussions