views:

43

answers:

2

Having used optional parameters in a few classes here and there, I'm starting to dislike them immensely for the trouble they cause in certain cases with overload resolution, i.e. difficulties in binding delegates to them due to signature conflicts, as well as dynamic invocation problems with regard to method argument count.

How can I search in all files in my Visual Studio IDE (2010) project and locate all optional parameter usage? Would there be a clever regex I could use perhaps? Or perhaps using an older version of Visual Studio where optional parameters are not supported? I'm trying to avoid the hassle of manually scanning files in the project as it can be tiresome and error-prone. Thanks!

+1  A: 

Whilst this isn't probably the best way I tend to look at the Class View in visual studio. The types that are shown in square brackets are the optional parameters

DaveHogan
+3  A: 

Your best bet may be reflection - it should be easy enough to loop through all members of all types where they are methods and they have at least one optional parameter.

That wouldn't do the substitution for you, but could give you a list of all offending members.

Something like:

foreach (Type tp in currentAssembly.GetTypes())
    foreach (MethodInfo func in tp.GetMethods())
        if(func.GetParameters().Any(p=>p.IsOptional))
            Console.WriteLine(func.ToString());
Keith