如何使用NRefactory修改C#代码

如何使用NRefactory修改C#代码,c#,code-generation,sharpdevelop,nrefactory,C#,Code Generation,Sharpdevelop,Nrefactory,我试图使用NRefactory修改现有的一段C代码。我已尝试使用中描述的方法。我遇到了一个异常,抱怨重复的更改。有人能指出我做错了什么,也许能给我指出正确的方向吗 我有以下代码部分: // contains the source code that needs to be modified var text = "..."; // is the name of the file in which the target class exists var fileName = "Foo"; // i

我试图使用NRefactory修改现有的一段C代码。我已尝试使用中描述的方法。我遇到了一个异常,抱怨重复的更改。有人能指出我做错了什么,也许能给我指出正确的方向吗

我有以下代码部分:

// contains the source code that needs to be modified
var text = "...";
// is the name of the file in which the target class exists
var fileName = "Foo";
// is the name of the target class
var className = "Foo.cs"; 

// FormattingOptions and TextEditorOptions are properties available to
// the class containing this code...

var parser = new CSharpParser();

var syntaxTree = parser.Parse(text, fileName);

syntaxTree.Freeze();

var document = new StringBuilderDocument();

document.Text = syntaxTree.ToString(FormattingOptions);

using (var documentScript = new DocumentScript(document, FormattingOptions, TextEditorOptions))
{
    var typeDeclarations = syntaxTree.Descendants.OfType<TypeDeclaration>().Where(typeDeclaration => typeDeclaration.Name == className);
    var templateProviderDeclaration = typeDeclarations.SingleOrDefault();

    if (templateProviderDeclaration != null)
    {
        var constructorDeclarations = templateProviderDeclaration.Descendants.OfType<ConstructorDeclaration>();

        foreach (var constructorDeclaration in constructorDeclarations)
        {
            var assignmentExpressions = constructorDeclaration.Descendants.OfType<AssignmentExpression>();

            foreach (var assignmentExpression in assignmentExpressions)
            {
                var leftExpression = assignmentExpression.Left;

                if (leftExpression != null && leftExpression.ToString(FormattingOptions) == "this.templates")
                {
                    var rightExpression = assignmentExpression.Right;

                    foreach (var arrayInitializerExpression in rightExpression.Children.OfType<ArrayInitializerExpression>())
                    {
                        // uses AddTemplate(), which is an extension method
                        // which takes in an expression and modifies it, and
                        // returns an expression
                        var newExpression = ((ArrayInitializerExpression)arrayInitializerExpression).AddTemplate();

                        documentScript.Replace(arrayInitializerExpression, newExpression);
                    }
                }
            }
        }
    }

    // text retrieved here is *clobbered*, by a few characters
    text = documentScript.CurrentDocument.Text;
}
更新:

我试图改变这样一句话:

this.templates = new Dictionary<string, ITemplate>
{
    { "Foo", new FooTemplate() }
};
this.templates=新字典
{
{“Foo”,新的FooTemplate()}
};
致:

this.templates=新字典
{
{“Foo”,新的FooTemplate()},
{“Bar”,新的BarTemplate()}
};

就这个问题而言,我试图做出的改变的具体细节并不重要。我怀疑我有一些构造不完全正确的东西,但我不知道这是否是问题所在。

你的问题不清楚。您可以添加更多的细节吗?您正在尝试什么类型的重构,在什么样的示例中?谢谢您的关注。这不是样品。这是我自己的代码,没关系。你没有提到你想通过重构做什么样的改变。如果有帮助的话,我已经更新了这个问题。
this.templates = new Dictionary<string, ITemplate>
{
    { "Foo", new FooTemplate() }
};
this.templates = new Dictionary<string, ITemplate>
{
    { "Foo", new FooTemplate() },
    { "Bar", new BarTemplate() }
};