2
如何使用CodeDom.CodeMemberMethod
来装饰方法签名async
?使用CodeMemberMethod创建异步方法
我想有结果:
public async Task SomeMethodAsync()
{
}
有没有办法做到这一点WHIS CodeDom中。我结束了使用regex
。
public static class GenCodeParser
{
private const string AsyncKeyWordPattern = @"(?<=public class DynamicClass(\r\n)*\s*{(\r\n)*\s*public)(?=.*\s*SomeMethodAsync{1})";
private const string AsyncKeyWordReplacementPattern = @" async ";
public static string AddAsyncKeyWordToMethodDeclaration(string sourceCode)
{
if (string.IsNullOrWhiteSpace(sourceCode)) return null;
try
{
var regex = new Regex(AsyncKeyWordPattern);
return regex.Replace(sourceCode, AsyncKeyWordReplacementPattern);
}
catch
{
return null;
}
}
}
看来你找到了一个解决方案,你应该张贴它作为一个答案,然后你能接受它。 – svick 2014-10-29 23:50:44