I'm looking for a way to read content of a file at compile time and replace value of a const with it:
public class MyClass {
[LoadFromFile("/myFile.txt")]
public const string MyConst = "";
}
This will read content of file myFile.txt
(probably using File.ReadAllText
or anything else) in directory relative to the working directory, content of this file could be for example:
Hello world
And in compiled assembly MyClass.MyConst
would return Hello world
.
Research led me to https://github.com/Fody/Fody and to "assembly weaving". How would you approach this problem? Are there any other ready to use solutions for this? I'm using .NET 5.