Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Snippet to Generate a Public Property + Private Backing Field + Documentation Tags inside #regions

0.00/5 (No votes)
14 Jan 2008 1  
Code snippet to generate a property with private backing field, linked documentations tags, inside #regions. Ability to config type, field and property's name and the default init value

Introduction

The problem with the majority of the snippets that I found is that none, absolutely none goes beyond the code generation! Sometimes I found very good snippets, well coded, but finish, nothing more. No generation of documentation, no #regions directives! Yes, I know! Nobody likes documentation, but when you need to explain what the hell your class/method/etc. does to your buddy, or the same explanation needs to be re-made sometime after the code is ready, and you are working on another project, you'll remember the documentation that you did not make!

#regions are another problem. Nobody likes the mix "snippets plus #regions". Inside Visual Studio, #regions enable blocks of code that have affinity to be visually put together, and with the collapse and expansion features, have the ability to focus on different levels of abstractions of the same code.

After this, I did decide to write a code snippet to a common task to every program's day: a property generation snippet. This snippet isn't a big one, far from this! This snippet aims only to show how even a small problem, when well solved can be of great help. ("this is not a big one", this snippet has up-to 46 lines. I've another snippet that generates a class inside your own namespace, with a custom exception class, eventlog helper class, performance counters, dispose methods, and documentation, that generates about 3.000 lines of code!!!)

Background

You can develop a snippet event with text editor or inside Visual Studio, but when you decide to delve in the code snippet world, the first smart thing to do is look for a snippet editor. In my experience, two free snippet editors are worth mentioning: "Snippet Editor" and "SnippetBuilder", the first one is what I use majority of the time.

When you develop snippets with a text editor, you'll need to work directly with the snippet's XML tag, which can be error prone. An editor will be of great help because this tends to hide the details. Provide some level of syntax coloring (very poor! but better than nothing!) and then publish the snippet. A snippet is basically a .xml file, but when you publish the snippet file, it is transformed into a .vsi that you or another person can import directly into Visual Studio only by double-clicking the .vsi file. And the most important feature: the editor is of great help when your snippet enables customization (replacements)! What is the ability to change the contents of the replacement variables with the user desired values at the moment the snippet is activated.

Using the Code

The following code shows the first part of the snippet:

#region _$field$ backing field

#region Documentation

/// <summary>_$field$ is a backing variable to $property$ property. 
/// (Describe better)</summary>
/// <example>
/// 	<code lang="CS" title="." description=".">
/// $type$ $field$ = this._$field$;
///     </code>
/// </example>
/// <remarks>This field is marked with DebuggerBrowsableState.Never attribute.</remarks>
/// <requirements>.</requirements>
/// <seealso cref="$property$" cat="Fronting Property">$property$</seealso>

#endregion Documentation

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private $type$ _$field$ = $defaultValue$;

#endregion _$field$ backing field

#region $property$ Fronting property

#region Documentation

/// <summary>$property$ of type $type$ (Describe better).</summary>
/// <value>Returns $type$.</value>
/// <exception cref="" caption=""></exception>
/// <example>
/// 	<para>$type$ _$property$ = this.$property$;</para>
/// </example>
/// <remarks>This property is marked with XmlIgnore and 
/// DebuggerStepThrough attributes.</remarks>
/// <requirements>.</requirements>
/// <seealso cref="_$field$" cat="Backing Field">_$field$.</seealso>

#endregion Documentation

[XmlIgnore]
public $type$ $property$
{
   [DebuggerStepThrough] get { return this._$field$;}
   [DebuggerStepThrough] set { this._$field$ = value;}
}

#endregion $property$ Fronting property
$end$	

This block generates a backing variable named: _$field$ of type $type$ with an init default value $defaultValue$, and property named: $property$ of the same type. These replacement variables will be changed to the desired values, by example:

  • $field$..............= workedDays
  • $type$................= int
  • $defaultValue$..= 0
  • $property$.......= WorkedDays

It will produce this:

  #region _workedDays backing field
  
  #region Documentation
  
  /// <summary>_workedDays is a backing variable to WorkedDays property. 
  /// (Describe better)</summary>
  /// <example>
  /// 	<code lang="CS" title="." description=".">
  /// int workedDays = this._workedDays;
  ///     </code>
  /// </example>
  /// <remarks>This field is marked with DebuggerBrowsableState.Never attribute.
  /// </remarks>
  /// <requirements>.</requirements>
  /// <seealso cref="WorkedDays" cat="Fronting Property">WorkedDays</seealso>
  
  #endregion Documentation
  
  [DebuggerBrowsable(DebuggerBrowsableState.Never)] private int _workedDays = 0;
  
  #endregion _workedDays backing field
  
  #region WorkedDays Fronting property
  
  #region Documentation
  
  /// <summary>WorkedDays of type int (Describe better).</summary>
  /// <value>Returns int.</value>
  /// <exception cref="" caption=""></exception>
  /// <example>
  /// 	<para>int _WorkedDays = this.WorkedDays;</para>
  /// </example>
  /// <remarks>This property is marked with XmlIgnore and 
  /// DebuggerStepThrough attributes.</remarks>
  /// <requirements>.</requirements>
  /// <seealso cref="_workedDays" cat="Backing Field">_workedDays.</seealso>
  
  #endregion Documentation
  
  [XmlIgnore]
  public int WorkedDays
  {
     [DebuggerStepThrough] get { return this._workedDays; }
     [DebuggerStepThrough] set { this._workedDays = value; }
  }
  
  #endregion WorkedDays Fronting property

Points of Interest

Beyond the generated code, the documentation with references between the backing field and the property (see the XML tag <seealso>!), and the #regions generated are the main point here.

The documentation is almost ready, with a very little effort on summary tag, this can be finished. Any one that comes to use this code will be able to see the IntelliSense showing the contents of the summary tag. And the #regions will enable to expand/collapse the blocks of code to the desired level.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here