开发者

split a .Net code file

开发者 https://www.devze.com 2023-03-12 09:51 出处:网络
This is my first stack overflow quesiton, so if i\'m not posting correctly, or not tagging right or whatever, please let me know.

This is my first stack overflow quesiton, so if i'm not posting correctly, or not tagging right or whatever, please let me know.

For those curious, at the end of this question is a bit of background of why I want to do this.

I'm hoping someone has some ideas of how I can easily take a code file and then get an array of strings with each array element containing the contents of a single method from that code file.

What I want to do is be able to easily parse a .Net code file into it's indivdial methods. Reading the file from disk is quite simple - parsing so that I have individual strings of each of the methods is what I'm looking for a better method of doing. The code files I will be passing through this are VB, but ideally I'd like to see this working with C# also. For the VB side, cycling through each line looking for the keywords of method declaration, then the next instance of the "end sub" and "end function" should work, but that won't easily translate to C#. I also can't get past this feeling there has to be a better way - possibly through reflection.

Now for the background: I have recently found that when windows forms are called with .ShowDialog, the resources aren't cleaned up after your done with the form - you have to specifically dispose of it. This was discovered tracing down memory leaks in an application I work on. So what I need is to track down every instance of where showdialog is called and d开发者_StackOverflow中文版ispose is not called. A code search shows that I have over 300 instances of showdialog. That's a bit tedious to go through each one, and find if the form is correctly disposed. If I have strings of all of the methods, I can just search for methods that include showdialog and not dispose. This will give me very targeted direction to get these things cleaned up, as well as give me a tool I can run regularly and find anyplace that may have introduced this back into the code.

Sorry for the long winded post here. Any and all suggestions are greatly appreciated.


Don't do this. Look into tools like ReSharper from JetBrains or RefactorPro from DevExpress. These tools are designed to highlight code smells such as undisposed resources along with many many other things. Using one of these tools will be far more helpful to you and help you clean your code up.


You can also check out Telerik's JustCode

http://www.telerik.com/products/justcode.aspx


I believe that running Visual Studio's CodeAnalysis (fka FxCop) will warn you about undisposed references and members.


If you are writing c# in visual studio try using the "find all references" option in the right-click menu.

If that doesn't work:

This might seem like a silly answer but you could write your own lex program and run the c# file through there. It's basically a set of regular expressions that compilers use to check your code. I've never used the program outside of a linux environment and don't even know if it's possible but I believe it would do exactly what you wanted.

http://ashimg.tripod.com/Parser.html

http://dinosaur.compilertools.net/lex/index.html

http://tldp.org/HOWTO/Lex-YACC-HOWTO-3.html


I have found Dev Express to be very quick and effective for this. Also they offer there basic re factoring tools for free and their more feature rich packager for about $250.


There's the EnvDTE and Microsoft.VisualStudio.Shell.Interop namespace. You can use it let Visual Studio parse your solution.

It is also very ease to use reflection on compiled sources.


You want to parse the file-- it is quite easy if you can handle a sloppy job, but tricky if you want to do it exactly right

What you really want is a tool that lets you do semantic searches through your code

I've actually written such a tool--

Browse-by-Query an open source code-querying tool

There is a .Net version as well as a Java version-- Browse-by-Query for CIL reads assembly files instead of source files, so it doesn't matter which .Net language you use

The query you would want to use is

((methods containing calls to ~ "ShowDialog") without (methods containing calls to ~ "Dispose")) methods in class "System.Windows.Forms.Form"

You might hit a couple of problems trying to use it though--

Even though it analyzes .Net assemblies, the tool is written in Java, and due to lack of demand for the .Net version the compiled version up on SourceForge is somewhat out of date.

You still might want to give it a try though, since it does do exactly what you want.

Drop me an email and I can hook you up with a build of the latest .Net version.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号