是否有一种方法可以复制/导出Main.cs文件,以便常量已被填充?我想要一个新文件(Main.cs-复制),其中已填充常量。
MyConsts.cs
public const string str1 = "field1";
public const string str2 = "field2";
Main.cs
static void Main()
{
DoSomething(str1, "description of field1");
DoSomething(str2, "description of field2");
}
导出后的Main.cs。
static void Main()
{
DoSomething("field1", "description of field1"); //filled in const
DoSomething("field2", "description of field2"); //filled in const
}
为什么我要这样做:我需要制作遵循特定约定的资源,例如键:[ClassName] .field1.Description值:“ field1的描述”。我编写了一个简化复制粘贴工作的脚本,但是我需要文字字符串“ field1”而不是str1 const来制作资源,因此可以使我的脚本正常工作。
您可以尝试以下代码来导出带有填充的const字符串的C#文件(在Visual Studio中)。
初始.cs文件。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
DoSomething(MyConsts.str1, "description of field1");
DoSomething(MyConsts.str2, "description of field2");
Console.ReadKey();
}
static void DoSomething(string field,string description)
{
Console.WriteLine("{0}+****+{1}",field,description);
}
}
}
导出代码:
class Program
{
static void Main(string[] args)
{
string quote = "\"";
var text = File.ReadAllText(@"D:\Program.cs");
text = text.Replace("MyConsts.str1", quote+MyConsts.str1+quote).Replace("MyConsts.str2", quote + MyConsts.str2 + quote);
File.WriteAllText("D:\\test.cs", text);
}
}
最后,您将得到结果: