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

Use*_*r M 1 .net c# file-io winforms

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

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

例如,类文件是:

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

    //Add the generated code here.
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下代码:创建了一个类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;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

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

        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);

        }
Run Code Online (Sandbox Code Playgroud)

dtb*_*dtb 8

不是修改现有文件,而是动态生成第二个文件并使用partial关键字向类中添加新成员.

静态文件:

namespace Stackoverflow
{
    public partial class AskQuestion
    {
        public void Ask()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

生成的文件:

namespace Stackoverflow
{
    partial class AskQuestion
    {
        // Dynamically generated methods and properties
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 到目前为止,这是一种更好的方法 (2认同)