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