我正在将我们的网络服务更改为异步模型。为此,我必须更改一百多种方法。
手动执行是一个(没有吸引力的)选项。有没有办法以编程方式解析和更改多个函数/代码文件?
例子:
[Webmethod]
public void MyWebservice (string parameter1, string parameter2, string parameter3)
{
//Logic here
}
并将其更改为:
public void InternalMyWebservice (string parameter1, string parameter2, string parameter3, AsyncCallback callback)
{
//Logic here
}
[Webmethod]
public void BeginMyWebservice (string parameter1, string parameter2, string parameter3, AsyncCallback callback, object asyncState)
{
//Queue InternalMyWebservice in a threadpool
}
public void EndMyWebservice(IAsyncResult asyncResult)
{
//Set return values
}
这基本上是我必须为每个网络服务做的事情。将名称更改为“InternalX”,添加一个参数并创建开始和结束方法。
最佳答案
您应该能够使用 CSharpCodeProvider.Parse
方法生成一个 CodeCompileUnit
实例,它是您的代码的面向对象表示。有了它,您可以深入研究您的方法、更改参数、添加新方法和内容,完成后您可以将代码保存回文本文件。您通过调用传递修改后的 CodeCompileUnit
CodeDomProvider.GenerateCodeFromCompileUnit
生成代码
Provides access to instances of the C# code generator and code compiler.
关于c# - 以编程方式更改代码文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4603106/