Substituted FormattableString specific string region

Some software systems for the world to develop, so the need to make some string specific treatment based on different languages in different regions. If the amount of code written in string processing methods for different regions with different languages, then each is enormous. So this time we can use string interpolation characteristic deep, string interpolation results in C # will implicitly converted to a string or FormattableString.
For example the following example, the interpolation result will be a string type string:

string message = $"我的名字叫 {name} ";

The results of the interpolation following code string will be converted to FormattableString objects:

FormattableString message= $"我的名字叫 {name} ";

When we declare implicitly typed local variables, and the result is inserted in the string assigned to it, in fact, the result of interpolation string implicitly converted to a string type. The compiler to generate different codes depending on the type of operation of the program information has to be outputted. To create a string of program code string format may be generated according to the position of the region where the computer executes the program. Developers can use the compiler generated type determination mechanism to write or FormttableString stritg code.

public static string ToGerman(FormattableString fts)
{
    return string.Format(null,System.Globalization("de-de"),fts.Format.fts.GetArguments());
}

We define a method of the above code will German string into a format that takes a parameter of type FormattableString, returns a string. What can I string interpolation on the results directly to call this method. When we write a similar method requires two things to note:

  1. Do not write overloaded to type string as a parameter to these methods, because if there is string parameter type of overloading the compiler will give priority to the method of type string as a parameter;
  2. These methods can not be designed as a scalable approach, because the compiler is determined to be generated when the string is FormattableString string which side will consider the operator, if the left side is generated string.
  3. For more articles please scan code number of public attention: "Meow t-Yo" 3Fn2bd.jpg

Published 204 original articles · won praise 101 · Views 350,000 +

Guess you like

Origin blog.csdn.net/gangzhucoll/article/details/104713390