静态上下文中的C#字符串插值

时间:2018-10-22 12:57:13

标签: c# string dynamic compilation interpolation

我目前正在尝试生成具有某些属性的 string 类,在编译代码之前必须设置这些属性。

让我们看一下我的代码,我使用的是静态字符串:

    public class NewHumanSource
    {
            static readonly string template =
                "internal class {ClassName} : IHuman " + 
                "{ " +
                     // bunch of private fields here;
                     "private int age; " + Environment.NewLine +

                     "public int Age" + Environment.NewLine +
                     "{" + Environment.NewLine +
                          "get =>" + $"{int.Parse("{Age}")}" + ";" + Environment.NewLine +
                          "set" + Environment.NewLine +
                          "{" + Environment.NewLine +
                               "age= value;" + Environment.NewLine +
                               "OnPropertyChanged(nameof(age));" +Environment.NewLine +      
                          "}" + Environment.NewLine +
                     "}" + Environment.NewLine + Environment.NewLine +

                      // Other properties and members
                ";"

用于为源代码模板设置值的静态成员:

 public static string GetSourceCode(IHuman newHuman, string className)
 {
     code = code.Replace("{ClassName}", className.ToLowerInvariant().Replace(" ", ""));
     code = code.Replace("{Age}", newHuman.Age.ToString()); //Age is an integer.
     //...
 }

然后从外部类调用:

 var code = NewHumanSource.GetSourceCode(newHuman, className);

在该行抛出异常。静态方法甚至不会加载:

System.TypeInitializationException: 'The type initializer for 'DynamicCompilerTest.Classes.DynamicCompiler.NewHumanSource' threw an exception.'

InnerException  {"Input string was not in a correct format."}   System.Exception {System.FormatException}

如果所有属性都是字符串,那会很好用,但是此字符串插值会引发异常:

 "get =>" + $"{int.Parse("{Age}")}" + ";" +

关于如何处理非字符串类型的任何想法?我需要处理诸如DateTime之类的内置类型。还是有一种更优雅的方法来创建一个具有属性值的文本类?我可能会喜欢格式化。

非常感谢您的帮助/提示!

1 个答案:

答案 0 :(得分:2)

这将无法以这种方式工作。 int.Parse("{Age}")将在类型初始化期间求值,因此不能使用Age。根据您的代码,我相信您只需将其替换为return age

您还可以从使用多行字符串文字中受益:

static readonly string template =
    @"internal class {ClassName} : IHuman 
      {
          // bunch of private fields here
          private int age = {Age};

          public int Age
          {
              get { return age; }
              set
              {
                  age = value;
                  OnPropertyChanged(nameof(Age));
              }
          }

          // Other properties and members
      }";
相关问题