在两个大括号结束之前将文本写入.CS文件

时间:2013-01-31 19:27:15

标签: c# .net winforms file-io

我有一个要求,其中我必须做以下事情:

  1. 动态生成代码
  2. 将代码写入现有的.cs文件
  3. 我必须在类文件的最后两个大括号之前添加代码。
  4. 例如类文件是:

    namespace Stackoverflow
    {
        public class AskQuestion
        {
            public void Ask()
            {
            }
    
        //Add the generated code here.
        }
    }
    

    我尝试了以下代码: 创建了一个FindBraceLocation类

    namespace DBInfo.Class
    {
        public class FindBraceLocation
        {
            private int _bracePositionInLine;
            private int _noOfBraceFound;
            private int _lineNoIndex;
            private readonly string[] _fs;
    
            public int LineNoIndex
            {
                get { return _lineNoIndex; }
                set { _lineNoIndex = value; }
            }
    
            public int BracePositionInLine
            {
                get { return _bracePositionInLine; }
                set { _bracePositionInLine = value; }
            }
    
            public int NoOfBraceFound
            {
                get { return _noOfBraceFound; }
                set { _noOfBraceFound = value; }
            }
    
            public FindBraceLocation(string[] allLines)
            {
                _bracePositionInLine = -1;
                _noOfBraceFound = 0;
                _lineNoIndex = 0;
                _fs = allLines;
            }
    
            public void SearchFileStringIndex()
            {
                int noOfLines = _fs.Length;
                string line;
                int lineCounter;
                int pos2 = -1;
    
                for (lineCounter = noOfLines - 1; lineCounter >= 0; lineCounter--)
                {
                    line = _fs[lineCounter];
                    if (line.Trim().Length == 0)
                    {
                        continue;
                    }
                    pos2 = FindIndexOfBrace(line);
                    if (pos2 != -1)
                        break;
                }
    
                _lineNoIndex = lineCounter;
                _bracePositionInLine = pos2;
            }
    
            public int FindIndexOfBrace(string line)
            {
                //int braceNo = _noOfBraceFound;
    
                for (int counter = line.Length - 1; counter >= 0; counter--)
                {
                    if (line[counter] == '}' && (++_noOfBraceFound == 2))
                    {
                        return counter;
                    }
                }
                return -1;
            }
        }
    
    }
    

    并使用以下方法将其写入文件:

            protected void WriteToExistingGeneratedFile(string strInfo, string strPath)
            {
                string[] allLines = File.ReadAllLines(strPath);
                FindBraceLocation fp = new FindBraceLocation(allLines);
                fp.SearchFileStringIndex();
                string lineForInsertion = allLines[fp.LineNoIndex];
                string tempLine = lineForInsertion.Substring(0, fp.BracePositionInLine) + "\n" + strInfo + "\n" + lineForInsertion.Substring(fp.BracePositionInLine);
                allLines[fp.LineNoIndex] = tempLine;
                File.WriteAllLines(strPath, allLines);
    
            }
    

2 个答案:

答案 0 :(得分:8)

不是修改现有文件,而是动态生成第二个文件,并use the partial keyword将新成员添加到类中。

静态文件:

namespace Stackoverflow
{
    public partial class AskQuestion
    {
        public void Ask()
        {
        }
    }
}

生成的文件:

namespace Stackoverflow
{
    partial class AskQuestion
    {
        // Dynamically generated methods and properties
    }
}

答案 1 :(得分:0)

如果使用流式阅读器,则可以使用普通的字符串函数。像这样的东西会起作用:

System.IO.StreamReader myFile =
new System.IO.StreamReader("c:\\test.cs");
string myString = myFile.ReadToEnd();

// This will error if there are not at least 2 parentheses.
string UpToLastParan = myString.Text.Substring(0, myString.LastIndexOf("}"));
int SecondToLast = UpToLastParan.LastIndexOf("}");
string UpToSecondToLastParan = myString.Substring(0, SecondToLast);
string CorrectedString = UpToSecondToLastParan + "Your Code Here" + myString.Substring(SecondToLast, myString.Length - SecondToLast);

// Write back to file.