【问题标题】:Write text to a .CS file just before end of the two braces在两个大括号结束之前将文本写入 .CS 文件
【发布时间】:2013-01-31 19:27:14
【问题描述】:

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

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

例如类文件是:

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

        }

【问题讨论】:

  • 那么你的问题是什么?你想让别人为你做吗?
  • 展示你的作品。你已经尝试过什么?
  • 您是否努力完成此任务或只是决定在此处发布问题?
  • 我试过了...这要花很多时间来写,有没有其他方法可以做到这一点。
  • 对我来说听起来像是 CodeDOM 的案例。

标签: c# .net winforms file-io


【解决方案1】:

不要修改现有文件,而是动态生成第二个文件和use the partial keyword 以向类添加新成员。

静态文件:

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

生成的文件:

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

【讨论】:

  • 这个功能的需要是我们有几个方法可以动态添加到一个文件中,我们不确定有多少,它甚至可以扩展到 30-35 个方法......
  • 到目前为止,这是一种更好的方法
  • @UserM:那就搞清楚这30-35个方法是什么,同时将它们全部写入文件。反复向文件附加更多方法不是一个好主意。
  • @UserM 我想更多地了解这个要求背后的动机。看起来很诡异,不知道是不是可以通过继承或者扩展方法或者其他方式来实现?
  • 我不明白“注入”方法的数量有多重要。
【解决方案2】:

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

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    相关资源
    最近更新 更多