0

这是我生成属性的 T4 模板方法

public string Property(EdmProperty edmProperty)
{
    var fieldName = String.Format(CultureInfo.InvariantCulture,"_{0}{1}",_code.Escape(edmProperty).Substring(0,1).ToLower(),_code.Escape(edmProperty).Substring(1));
    var propertyType = _typeMapper.GetTypeName(edmProperty.TypeUsage);
    return string.Format(
        CultureInfo.InvariantCulture,
        @"{0} {1} {2} {6}{{{6}{3} get{{ return {4}; }}{6}{5} set{{ {4} = value; }}{6} }}",
        Accessibility.ForProperty(edmProperty),
        propertyType,
        _code.Escape(edmProperty),
        _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
        fieldName,
        _code.SpaceAfter(Accessibility.ForSetter(edmProperty)),
        "");
}

它生成的代码格式如下:

public string SomeProperty{ get{ return _someProperty; } set{ _someProperty = value; }}

我想对其进行格式化以使其显示如下:

public string SomeProperty
{
     get{ return _someProperty; }
     set{ _someProperty = value; }
}

我试图String.Format在 eg( {6}) 中制作最后一个参数:

  1. Environment.NewLine
  2. "\r\n"
  3. "\n"

我也知道自动实现的属性,但需要这样做。

那么这样做的正确方法是什么?

4

1 回答 1

2

Enviroment.NewLine应该是正确的。它应该如下所示:

return string.Format(
    CultureInfo.InvariantCulture,
    @"{0} {1} {2} {6}{{{6}{3} get{{ return {4}; }}{6}{5} set{{ {4} = value; }}{6} }}",
    Accessibility.ForProperty(edmProperty),
    propertyType,
    _code.Escape(edmProperty),
    _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
    fieldName,
    _code.SpaceAfter(Accessibility.ForSetter(edmProperty)),
    Environment.NewLine);
于 2013-09-25T08:43:35.500 回答