解析C#,寻找方法,并把try / catch语句的所有方法 [英] Parsing C#, finding methods and putting try/catch to all methods

查看:575
本文介绍了解析C#,寻找方法,并把try / catch语句的所有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来很奇怪,但我需要把一个包装try catch块的每一个方法来捕获所有异常。我们有成千上万的方法,我需要做的是在一个自动化的方式。你有什么建议?



我打算解析所有的CS文件和检测方法,并与应用程序中插入一个try catch块。您能否给我建议,我可以很容易地使用任何解析器?或任何会帮我...



每一个方法都有其独特的数字,如5006

 公共静态logEntry进行身份验证(....)
{
logEntry logEntry = NULL;

{
....
返回logEntry;
}

赶上(CompanyException)
{
扔;
}

赶上(异常前)
{
logEntry =新LogEntry(
5006,
RC.GetString(5006 ),EventLogEntryType.Error,
LogEntryCategory.Foo);

抛出新CompanyException(logEntry,除息);
}
}



我创造了这个本;
http://thinkoutofthenet.com/的index.php / 2009/01/12 /批次代码的方法操纵/


解决方案

我不得不

做一些事情有点类似八九不离十(添加一些东西来了很多行代码);我用正则表达式。



我想创建一个找到的每个函数的开头一个正则表达式脚本,并开始后立即插入try catch块。然后我会创造另一个正则表达式脚本来找到函数结束时(通过发现之后它的函数的开头),并在年底插入try catch块。这会不会让你的存在方式100%,但会打,也许80%,这将有望足够接近,你可以做的边缘情况没有太多的更多的工作。


I know it sounds weird but I am required to put a wrapping try catch block to every method to catch all exceptions. We have thousands of methods and I need to do it in an automated way. What do you suggest?

I am planning to parse all cs files and detect methods and insert a try catch block with an application. Can you suggest me any parser that I can easily use? or anything that will help me...

every method has its unique number like 5006

public static LogEntry Authenticate(....)
        {
            LogEntry logEntry = null;
            try
            {
                ....
                return logEntry;
            }

            catch (CompanyException)
            {
                throw;
            }

            catch (Exception ex)
            {
                logEntry = new LogEntry(
                    "5006",
                    RC.GetString("5006"), EventLogEntryType.Error,
                    LogEntryCategory.Foo);

                throw new CompanyException(logEntry, ex);
            }
        }

I created this for this; http://thinkoutofthenet.com/index.php/2009/01/12/batch-code-method-manipulation/

解决方案

I had to do something kinda sorta similar (add something to a lot of lines of code); I used regex.

I would create a regex script that found the beginning of each function and insert the try catch block right after the beginning. I would then create another regex script to find the end of the function (by finding the beginning of the function right after it) and insert the try catch block at the end. This won't get you 100% of the way there, but it will hit maybe 80% which would hopefully be close enough that you could do the edge cases without too much more work.

这篇关于解析C#,寻找方法,并把try / catch语句的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆