预处理C# - 检测方法 [英] Preprocessing C# - Detecting Methods

查看:190
本文介绍了预处理C# - 检测方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所需要的能力来预处理一个号码C#的文件作为对一个项目一个预生成步骤,检测方法的开始,并且在该方法的开始,插入生成的代码的任何现有代码之前。我,但是,有一个问题,检测方法的开口。我最初尝试正则表达式匹配,但是结束了太多太多的误报。

I require the ability to preprocess a number of C# files as a prebuild step for a project, detect the start of methods, and insert generated code at the start of the method, before any existing code. I am, however, having a problem detecting the opening of a method. I initially tried a regular expression to match, but ended up with far too many false positives.

我会用反射,但MethodInfo的类不引用原始的源点。

I would use reflection, but the MethodInfo class does not reference the point in the original source.

修改:什么我真的想在这里做的是支持的方法前置条件,通过对方法的属性来确定前置条件代码。我最初的想法是,我可以看看该方法的开头,然后将生成的代码来处理的先决条件。

EDIT: What I am really trying to do here is to support pre-conditions on methods, that pre-condition code being determined by attributes on the method. My initial thought being that I could look for the beginning of the method, and then insert generated code for handling the pre-conditions.

有没有更好的方式来做到这一点?我愿意创建一个Visual Studio加载项如果需要的话。

Is there a better way to do this? I am open to creating a Visual Studio Addin if need be.

这是一个.NET 2.0的项目。

This is a .NET 2.0 project.

干杯

推荐答案

PostSharp或的 Mono.Cecil能做到会让你做这干净通过改变生成的代码,而不进入编写C#编译器这是不太可能的核心业务为你...

PostSharp or Mono.Cecil will let you do this cleanly by altering the generated code without getting into writing a C# parser which is unlikely to be core business for you...

还没有做的后果任何与PostSharp反而会猜测它比单声道更适合实施类似的先决条件或AOP。或者你也许可以做一些AOPy像 Ninject

Havent done anything of consequence with PostSharp but would be guessing its more appropriate than Mono for implementing something like preconditions or AOP. Alternately you might be able to do something AOPy with a DI container like Ninject

不过,当然这种想法的适用性取决于 - 你没有说太多,其他比你想的方法开始...

But of course the applicability of this idea Depends - you didnt say much other than that you wanted to insert code at the start of methods...

插入代码编辑:在您的愿望,做的前提条件光......在.NET 4 代码契约肯定是在那个方向。

In light of your desire to do preconditions... Code Contracts in .net 4 is definitely in that direction.

你有什么样的工具呢?什么是错与具有单Mono.Cecil.dll DLL发货?比解析器其他的方式要么东西是适合工作的工具。

What sort of a tool do you have? Whats wrong with having a single Mono.Cecil.dll DLL shipped? Either way something other than a parser is the tool for the job.

这篇关于预处理C# - 检测方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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