1

I want use a string for my Exception message:

$"Your file size cannot exceeded from {AppSetting.MaxFileSize}"

But I want to put this in a ResourceFile:

"ExceededFileSizeException": $"Your file size cannot exceeded from {AppSetting.MaxFileSize}"

What is the best way to make this string and read it from code?

Boolood
  • 79
  • 9

2 Answers2

2

That is a quite new representation of a string format command. If you are moving this string as it is to a resource file, the compiler won't format it later (im sure you are already getting compile time problems as the context is getting unknown - where should the input AppSetting.MaxFileSize come from?).

The "classical" algo is:

string.Format("Your file size cannot exceeded from {0}", AppSetting.MaxFileSize)

If formatting this way, you could move the template string into a resourcefile, the format method will then do the same magic.

Udontknow
  • 1,472
  • 12
  • 32
0

this is what I usually do

var templateDictionary = new Dictionary<string, string>();
string yourSetting = "mysettings";
//templateDictionary.Add("AppSetting.MaxFileSize", AppSetting.MaxFileSize); // sample
templateDictionary.Add("AppSetting.MaxFileSize", yourSetting);

var inputText = "Your file size cannot exceeded from {AppSetting.MaxFileSize}";
var text = Regex.Replace(inputText, @"\{(.+?)\}", m => templateDictionary["AppSetting.MaxFileSize"]);

I create an extension method that can replace the variable inside a resource file string.

ChizT
  • 677
  • 3
  • 10
  • Well, it may work, but you are introducing new dependencies (a dictionary), are calling potentially expensive Regex methods and are loosing compiler safety (if "AppSetting.MaxFile" is incorrectly registered , you won't notice until it is needed at runtime; if using constants, you will not be able to compile if not spelling the name right). – Udontknow Mar 31 '19 at 11:24