
Hello,
I want to replace part of C# code in a string like this :
public partial class SomeClass {
//Comment.
#region Some region
[Attribute("", "", "", "", "", "", "", "")]
public string Name{get;set;}
#endregion
}
First I get the AST tree of the code I have say in the Content string;
//Say I have a C# ISyntaxLanguage instance in the Language property
return ((DotNetParseData)Language.Parse(Content)).Ast;
From this AST I search the IAstNode I want to replace; once located I use the StartOffset and EndOffset property to replace the string part :
//Say the REPLACED string is in the replace string
Content = Content.Substring(0, astNode.StartOffset.Value) + replace +
sourceFile.Content.Substring(astNode.EndOffset.Value);
I my example I retrieve the IAstNode with value "Attribute" but it does'nt work, because if the Content string has formatted code, with breaklines or tabs the StartOffset and EndOffset property are wrong, and the replaced string is in the wrong position. I got this result :
public partial class SomeClass {
//Comment.
#region Some region
REPLACED"")]
public string Name{get;set;}
#endregion
}
Like this I suppose it is a bug.
[Modified 13 years ago]