The problem with intellisense

Posted by: Eric Gunnersons C# Compendium, on 15 Jun 2007 | View original | Bookmarked: 0 time(s)

Today I was working with some sample code, and I came across a misspelling. Not a big deal - there was a field that was named "m_postion" rather than "m_position".

But that got me thinking...

In the past, that sort of thing wouldn't have happened. You would have written:

int m_postion;

but then, when you wrote your code, you would have written:

m_position = 5;

and the compiler would have complained. But with intellisense, you now just pick whatever looks right in the popup list, and the mistakes stay around a bit longer.

So, I wrote a bit of code to help with this - it reflects over an assembly, and produces a list of words.

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

namespace IdentifierExtract
{
    class GetIdentifiers
    {
        Dictionary m_identifiers = new Dictionary();

        public GetIdentifiers()
        {
        }

        public void Process(string assemblyFilename)
        {
            Assembly assembly = Assembly.LoadFrom(assemblyFilename);

            foreach (Type type in assembly.GetTypes())
            {
                ProcessType(type);
            }
        }

        void ProcessType(Type type)
        {
            BreakIntoWordsAndAdd(type.Name);

            foreach (MemberInfo memberInfo in type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
            {
                BreakIntoWordsAndAdd(memberInfo.Name);

                MethodBase methodBase = memberInfo as MethodBase;

                if (methodBase != null)
                {
                    foreach (ParameterInfo parameterInfo in methodBase.GetParameters())
                    {
                        BreakIntoWordsAndAdd(parameterInfo.Name);
                    }
                }
            }
        }

        void BreakIntoWordsAndAdd(string identifier)
        {
            List words = BreakPascalCasing(identifier);

            foreach (string word in words)
            {
                AddIdentifer(word);
            }
        }

        List BreakPascalCasing(string identifier)
        {
            Regex regex = new Regex(".[a-z]+");
            
            MatchCollection matches = regex.Matches(identifier);

            List words = new List();
            foreach (Match match in matches)
            {
                words.Add(match.Value);
            }

            return words;
        }

        private void AddIdentifer(string name)
        {
            name = name.ToLower();
            if (!m_identifiers.ContainsKey(name))
            {
                m_identifiers.Add(name, null);
            }
        }

        public void WriteToConsole()
        {
            List identifiers = new List(m_identifiers.Keys);
            identifiers.Sort();

            foreach (string identifier in identifiers)
            {
                Console.WriteLine(identifier);
            }
        }
    }
}

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: Visual Studio | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 839 | Hits: 180

Similar Posts

  • Ambiguous References in DefaultWsdlHelpGenerator.aspx more
  • 12 ASP.NET MVC Best Practices more
  • Postgresql - Day 2 more
  • Naming Anonymous Types with Generate from Usage more
  • No JavaScript IntelliSense in VS 2010 Beta 2? Reset your Settings more
  • Principles, Patterns, and Practices of Mediocre Programming more
  • Debugger Visualizers not working in ASP.NET Medium Trust more
  • Some Really Cool LINQ to Help Performance and Show Line Thickness more
  • The Downside of Transparency more
  • jQuery UI Datepicker and z-Index more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD