Question

here is my T4 template method to generate property

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)),
        "");
}

it's generated code formated like that:

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

I would like to format this to make it display like this:

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

I've tryed to make last parameter of String.Format in e.g({6}):

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

I'm also aware of auto-implemented properties, but need to do this this way.

So what is the proper way to do this ?

Was it helpful?

Solution

Enviroment.NewLine should be correct. It should look like this:

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top