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

查看:22
本文介绍了预处理 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 做过任何有意义的事情,但猜测它比 Mono 更适合实现诸如先决条件或 AOP 之类的东西.或者,您也可以使用 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天全站免登陆