<#@ template language="C#" #>
/// *********************<#= Context.GetGlobalProperty("CompanyName") #>*************************************
<# CodeElement codeElement = Context.CurrentCodeElement; #>
/// Author : <#= Context.GetGlobalProperty("UserName") #>
/// Created : <#= Context.DestinationFileCreationDateTime.ToString("yyyy-MM-dd HH:mm:ss") #>
/// <summary>
///<# GenerateSummaryText(); #>
/// </summary>
<# if(codeElement.HasTypeParameters)
{
for(int i = 0; i < codeElement.TypeParameters.Length; i++)
{
TypeParameter typeParameter = codeElement.TypeParameters[i];
#>
/// <typeparam name="<#= typeParameter.Name #>"><# GenerateTypeParamText(typeParameter, i); #></typeparam>
<# }
}
if(codeElement.HasParameters)
{
for(int i = 0; i < codeElement.Parameters.Length; i++)
{
Parameter parameter = codeElement.Parameters[i];
#>
/// <param name="<#= parameter.Name #>"><# GenerateParamText(parameter, i); #></param>
<# }
}
GenerateReturnsText(); #>
/// Last Modified By : <#= Context.GetGlobalProperty("UserName") #>
/// Last Modified On : <#= Context.DestinationFileModificationDateTime.ToString("yyyy-MM-dd HH:mm:ss") #>
/// *********************<#= Context.GetGlobalProperty("CompanyName") #>*************************************
<#= Context.GetNonGeneratedExceptionTags() #>
<#= Context.GetNonGeneratedTags() #>
<# GenerateRemarksText(); #>
<#+
private void GenerateSummaryText()
{
if(Context.HasExistingTagText("summary"))
{
this.WriteLine(Context.GetExistingTagText("summary"));
}
else if(IsAsyncMethod())
{
this.WriteLine(Context.ExecMacro("$(MethodName.Words.ExceptLast)") + " as an asynchronous operation.");
}
else if(IsMainMethod())
{
this.WriteLine("Defines the entry point of the application.");
}
else if(IsGetHashCodeMethod())
{
this.WriteLine("Returns a hash code for this instance.");
}
else if(IsEqualsMethod())
{
this.WriteLine("Determines whether the specified <see cref=\"System.Object\" /> is equal to this instance.");
}
else if(IsToStringMethod())
{
this.WriteLine("Returns a <see cref=\"System.String\" /> that represents this instance.");
}
else if(Context.HasInheritedTagText("summary"))
{
this.WriteLine(Context.GetInheritedTagText("summary"));
}
else if(IsOnMethod())
{
if(Context.CurrentCodeElement.HasParameters && IsEventArgsParameter(Context.CurrentCodeElement.Parameters[Context.CurrentCodeElement.Parameters.Length - 1]))
{
this.WriteLine(Context.ExecMacro("Handles the <see cref=\"E:$(MethodName.Words.Verbatim.ExceptFirst)\" /> event."));
}
else
{
this.WriteLine(Context.ExecMacro("Called when $(End)[$(MethodName.Words.ExceptFirst)]."));
}
}
else if(IsControlEventHandlerMethod())
{
string name = Context.CurrentCodeElement.Name;
int index = name.LastIndexOf('_');
if(index >= 0)
{
this.WriteLine(string.Format(
"Handles the {0} event of the {1} control.",
name.Substring(index + 1),
name.Substring(0, index)));
}
else
{
this.WriteLine("[ERROR: No underscore found in name]");
}
}
else if(IsFinalizeMethod())
{
// commented for StypeCop compliance (SA1643)
// this.WriteLine(Context.ExecMacro("Releases unmanaged resources and performs other cleanup operations before the $(DeclaringTypeName.ShortNameAsSee) is reclaimed by garbage collection."));
this.WriteLine(Context.ExecMacro("Finalizes an instance of the $(DeclaringTypeName.ShortNameAsSee) class."));
}
else if(IsStateCheckMethod())
{
bool bWithParameters = Context.CurrentCodeElement.HasParameters;
Words words = Context.GetWords(Context.CurrentCodeElement.Name);
bool bSingleWord = (words != null && words.Count == 2);
if (bWithParameters)
{
if (bSingleWord)
{
this.WriteLine(Context.ExecMacro("Determines whether the specified $(Parameters.First.Name.Words.All) $(MethodName.Words.All)."));
}
else
{
this.WriteLine(Context.ExecMacro("Determines whether $(End)[$(MethodName.Words.All)] [the specified $(Parameters.First.Name.Words.All)]."));
}
}
else if (bSingleWord)
{
this.WriteLine(Context.ExecMacro("Determines whether this instance $(MethodName.Words.All)."));
}
else
{
this.WriteLine(Context.ExecMacro("Determines whether $(End)[$(MethodName.Words.All)]."));
}
}
else if(IsCapabilityCheckMethod())
{
bool bWithParameters = Context.CurrentCodeElement.HasParameters;
Words words = Context.GetWords(Context.CurrentCodeElement.Name);
bool bSingleWord = (words != null && words.Count == 2);
if (bWithParameters)
{
if (bSingleWord)
{
this.WriteLine(Context.ExecMacro("Determines whether this instance $(MethodName.Words.All) the specified $(Parameters.First.Name.Words.All)."));
}
else
{
this.WriteLine(Context.ExecMacro("Determines whether this instance $(End)[$(MethodName.Words.All)] the specified $(Parameters.First.Name.Words.All)."));
}
}
else if (bSingleWord)
{
this.WriteLine(Context.ExecMacro("Determines whether this instance $(MethodName.Words.All)."));
}
else
{
this.WriteLine(Context.ExecMacro("Determines whether this instance $(End)[$(MethodName.Words.All)]."));
}
}
else if(IsImplicitOperatorMethod())
{
this.WriteLine(Context.ExecMacro("Performs an implicit conversion from $(Parameters.First.TypeName.FullNameAsSee) to $(ReturnTypeName.FullNameAsSee)."));
}
else if(IsExplicitOperatorMethod())
{
this.WriteLine(Context.ExecMacro("Performs an explicit conversion from $(Parameters.First.TypeName.FullNameAsSee) to $(ReturnTypeName.FullNameAsSee)."));
}
else if(Context.CurrentCodeElement.IsOperator)
{
this.WriteLine(Context.ExecMacro("Implements the $(MethodName.ActualName)."));
}
else if(IsDisposeMethod())
{
this.WriteLine("Releases unmanaged and - optionally - managed resources.");
}
else if(IsSingleWordMethod())
{
if(Context.CurrentCodeElement.HasParameters)
{
this.WriteLine(Context.ExecMacro("$(MethodName.Words.FirstAsVerbStartingSentence) the specified $(Parameters.First.Name.Words.All)."));
}
else
{
this.WriteLine(Context.ExecMacro("$(MethodName.Words.FirstAsVerbStartingSentence) this instance."));
}
}
else if(Context.ContainsOfTheReordering(Context.CurrentCodeElement.Name))
{
this.WriteLine(Context.ExecMacro("$(OfTheReordering)"));
}
else
{
if(Context.CurrentCodeElement.IsMethod)
{
this.WriteLine(Context.ExecMacro("$(DefaultMethodName)"));
}
else
{
this.WriteLine(Context.ExecMacro("$(End)"));
}
}
}
private void GenerateTypeParamText(TypeParameter typeParameter, int index)
{
if(Context.HasExistingTagText("typeparam", index))
{
this.Write(Context.GetExistingTagText("typeparam", index));
}
else if(Context.HasInheritedTagText("typeparam", index))
{
this.Write(Context.GetInheritedTagText("typeparam", index));
}
else
{
string typeParameterName = typeParameter.Name;
if(typeParameterName != null)
{
if(typeParameterName.Length == 1)
{
this.Write("");
}
else
{
this.Write("The type of " + Context.ExecMacro(typeParameterName, "$(TheAndAll)") + ".");
}
}
}
}
private void GenerateParamText(Parameter parameter, int index)
{
if(Context.HasExistingTagText("param", index))
{
this.Write(Context.GetExistingTagText("param", index));
}
else if(Context.HasInheritedTagText("param", index))
{
this.Write(Context.GetInheritedTagText("param", index));
}
else if(IsEqualsMethod() && index == 0)
{
this.Write("The <see cref=\"System.Object\" /> to compare with this instance.");
}
else if(IsDisposeMethod() && index == 0)
{
this.Write(Context.ExecMacro("$(True) to release both managed and unmanaged resources; $(False) to release only unmanaged resources."));
}
else if(IsControlEventHandlerMethod() && index == 0)
{
this.Write("The source of the event.");
}
else if(IsBooleanParameter(parameter))
{
this.Write("if set to " + Context.ExecMacro("$(True)") + " [" + Context.ExecMacro(parameter.Name, "$(All)") + "].");
}
else if(IsEventArgsParameter(parameter))
{
this.Write("The " + Context.ExecMacro(parameter.ReturnType.FullName, "$(TypeName.FullNameAsSee)") + " instance containing the event data.");
}
else if(Context.ContainsOfTheReordering(parameter.Name))
{
this.Write(Context.ExecMacro(parameter.Name, "$(OfTheReordering)"));
}
else
{
this.Write(Context.ExecMacro(parameter.Name, "$(TheAndAllAsSentence)") + ".");
}
}
private void GenerateReturnsText()
{
if(Context.HasExistingTagText("returns"))
{
#>
/// <returns><#= Context.GetExistingTagText("returns") #></returns>
<#+
}
else if(IsGetHashCodeMethod())
{
#>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
<#+
}
else if(IsEqualsMethod())
{
#>
/// <returns><#= Context.ExecMacro("$(True) if the specified <see cref=\"System.Object\" /> is equal to this instance; otherwise, $(False).") #></returns>
<#+
}
else if(IsToStringMethod())
{
#>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
<#+
}
else if(Context.HasInheritedTagText("returns"))
{
#>
/// <returns><#= Context.GetInheritedTagText("returns") #></returns>
<#+
}
else if(IsStateCheckMethod())
{
if(!IsBooleanMethod())
{
return;
}
bool bWithParameters = Context.CurrentCodeElement.HasParameters;
Words words = Context.GetWords(Context.CurrentCodeElement.Name);
bool bSingleWord = (words != null && words.Count == 2);
if (bWithParameters)
{
if (bSingleWord)
{
#>
/// <returns><#= Context.ExecMacro("$(True) if the specified $(Parameters.First.Name.Words.All) $(MethodName.Words.All); otherwise, $(False).") #></returns>
<#+
}
else
{
#>
/// <returns><#= Context.ExecMacro("$(True) if [$(MethodName.Words.All)] [the specified $(Parameters.First.Name.Words.All)]; otherwise, $(False).") #></returns>
<#+
}
}
else if (bSingleWord)
{
#>
/// <returns><#= Context.ExecMacro("$(True) if this instance $(MethodName.Words.All); otherwise, $(False).") #></returns>
<#+
}
else
{
#>
/// <returns><#= Context.ExecMacro("$(True) if [$(MethodName.Words.All)]; otherwise, $(False).") #></returns>
<#+
}
}
else if(IsCapabilityCheckMethod())
{
if(!IsBooleanMethod())
{
return;
}
bool bWithParameters = Context.CurrentCodeElement.HasParameters;
Words words = Context.GetWords(Context.CurrentCodeElement.Name);
bool bSingleWord = (words != null && words.Count == 2);
if (bWithParameters)
{
if (bSingleWord)
{
#>
/// <returns><#= Context.ExecMacro("$(True) if this instance $(MethodName.Words.All) the specified $(Parameters.First.Name.Words.All); otherwise, $(False).") #></returns>
<#+
}
else
{
#>
/// <returns><#= Context.ExecMacro("$(True) if this instance [$(MethodName.Words.All)] the specified $(Parameters.First.Name.Words.All); otherwise, $(False).") #></returns>
<#+
}
}
else if (bSingleWord)
{
#>
/// <returns><#= Context.ExecMacro("$(True) if this instance $(MethodName.Words.All); otherwise, $(False).") #></returns>
<#+
}
else
{
#>
/// <returns><#= Context.ExecMacro("$(True) if this instance [$(MethodName.Words.All)]; otherwise, $(False).") #></returns>
<#+
}
}
else if(IsImplicitOperatorMethod())
{
#>
/// <returns>The result of the conversion.</returns>
<#+
}
else if(IsExplicitOperatorMethod())
{
#>
/// <returns>The result of the conversion.</returns>
<#+
}
else if(Context.CurrentCodeElement.IsOperator)
{
#>
/// <returns>The result of the operator.</returns>
<#+
}
else if(Context.CurrentCodeElement.HasReturnType &&
Context.CurrentCodeElement.ReturnType.FullName.Length > 0 &&
String.Compare( Context.CurrentCodeElement.ReturnType.FullName, "System.Boolean", true) == 0)
{
#>
/// <returns><#= Context.ExecMacro("$(True)") #> if XXXX, <#= Context.ExecMacro("$(False)") #> otherwise.</returns>
<#+
}
else if(Context.CurrentCodeElement.HasReturnType &&
!(Context.CurrentCodeElement.ReturnType.FullName.Length == 0 ||
String.Compare( Context.CurrentCodeElement.ReturnType.FullName, "void", true) == 0))
{
#>
/// <returns><#= Context.ExecMacro("$(ReturnTypeName.FullName).") #></returns>
<#+
}
}
private bool IsBooleanParameter(Parameter parameter)
{
return (bool)(parameter.ReturnType != null &&
parameter.ReturnType.FullName != null &&
parameter.ReturnType.FullName.Length > 0 &&
(string.Compare( parameter.ReturnType.FullName, "System.Boolean", true) == 0 ||
string.Compare( parameter.ReturnType.FullName, "System.Nullable<bool>", true) == 0));
}
private bool IsEventArgsParameter(Parameter parameter)
{
return (bool)(parameter.ReturnType != null &&
parameter.ReturnType.FullName != null &&
parameter.ReturnType.FullName.Length >= "EventArgs".Length &&
parameter.ReturnType.FullName.EndsWith("EventArgs"));
}
private bool IsBooleanMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.ReturnType != null &&
codeElement.ReturnType.FullName != null &&
codeElement.ReturnType.FullName.Length > 0 &&
string.Compare( codeElement.ReturnType.FullName, "System.Boolean", true) == 0);
}
private bool IsGetHashCodeMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name == "GetHashCode" &&
codeElement.ReturnType != null &&
codeElement.ReturnType.FullName != null &&
codeElement.ReturnType.FullName.Length > 0 &&
string.Compare( codeElement.ReturnType.FullName, "System.Int32", true) == 0);
}
private bool IsEqualsMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name == "Equals" &&
codeElement.ReturnType != null &&
codeElement.ReturnType.FullName != null &&
codeElement.ReturnType.FullName.Length > 0 &&
string.Compare( codeElement.ReturnType.FullName, "System.Boolean", true) == 0 &&
codeElement.HasParameters && codeElement.Parameters.Length == 1 &&
codeElement.Parameters[0].ReturnType != null &&
codeElement.Parameters[0].ReturnType.FullName != null &&
codeElement.Parameters[0].ReturnType.FullName.Length > 0 &&
string.Compare( codeElement.Parameters[0].ReturnType.FullName, "System.Object", true) == 0);
}
private bool IsToStringMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name == "ToString" &&
codeElement.ReturnType != null &&
codeElement.ReturnType.FullName != null &&
codeElement.ReturnType.FullName.Length > 0 &&
string.Compare( codeElement.ReturnType.FullName, "System.String", true) == 0);
}
private bool IsOnMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
Words words = Context.GetWords(codeElement.Name);
return (bool)(words != null && words.Count > 1 && codeElement.Name != null &&
codeElement.Name.Length > "On".Length && codeElement.Name.StartsWith("On"));
}
private bool IsControlEventHandlerMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.HasParameters && codeElement.Parameters.Length == 2 &&
codeElement.Parameters[0].ReturnType != null &&
codeElement.Parameters[0].ReturnType.FullName != null &&
codeElement.Parameters[0].ReturnType.FullName.Length > 0 &&
string.Compare( codeElement.Parameters[0].ReturnType.FullName, "System.Object", true) == 0 &&
codeElement.Parameters[1].ReturnType != null &&
codeElement.Parameters[1].ReturnType.FullName != null &&
codeElement.Parameters[1].ReturnType.FullName.Length >= "EventArgs".Length &&
codeElement.Parameters[1].ReturnType.FullName.EndsWith("EventArgs") &&
codeElement.Name.LastIndexOf('_') != -1);
}
private bool IsFinalizeMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name != null && codeElement.Name.Length > 1 &&
codeElement.Name.StartsWith("~"));
}
private bool IsStateCheckMethod()
{
string[] triggerWords = new string[] { "is", "has", "contains" };
CodeElement codeElement = Context.CurrentCodeElement;
Words words = Context.GetWords(codeElement.Name);
return (bool)(words != null && Array.IndexOf(triggerWords, words.First) >= 0);
}
private bool IsCapabilityCheckMethod()
{
string[] triggerWords = new string[] { "can" };
CodeElement codeElement = Context.CurrentCodeElement;
Words words = Context.GetWords(codeElement.Name);
return (bool)(words != null && Array.IndexOf(triggerWords, words.First) >= 0);
}
private bool IsImplicitOperatorMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name != null && codeElement.Name.Length > "implicit operator".Length &&
codeElement.Name != null && codeElement.Name.StartsWith("implicit operator"));
}
private bool IsExplicitOperatorMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name != null && codeElement.Name.Length > "explicit operator".Length &&
codeElement.Name.StartsWith("explicit operator"));
}
private bool IsDisposeMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name == "Dispose" &&
codeElement.HasParameters && codeElement.Parameters.Length == 1 &&
codeElement.Parameters[0].ReturnType != null &&
codeElement.Parameters[0].ReturnType.FullName != null &&
codeElement.Parameters[0].ReturnType.FullName.Length > 0 &&
string.Compare( codeElement.Parameters[0].ReturnType.FullName, "System.Boolean", true) == 0);
}
private bool IsSingleWordMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
Words words = Context.GetWords(codeElement.Name);
return (bool)(words != null && words.Count == 1);
}
private bool IsOfTheSuffixMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
Words words = Context.GetWords(codeElement.Name);
return (bool)(words != null && words.Count == 1);
}
private bool IsAsyncMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.IsAsync && codeElement.Name.Length > "Async".Length && codeElement.Name.EndsWith("Async"));
}
private bool IsMainMethod()
{
CodeElement codeElement = Context.CurrentCodeElement;
return (bool)(codeElement.Name == "Main" && codeElement.IsStatic &&
((codeElement.HasParameters && codeElement.Parameters.Length == 1) || !codeElement.HasParameters));
}
private void GenerateRemarksText()
{
if(Context.HasInheritedTagText("remarks"))
{ #>
/// <remarks><#= Context.GetInheritedTagText("remarks") #></remarks>
<#+ }
else if(Context.HasExistingTagText("remarks"))
{ #>
/// <remarks><#= Context.GetExistingTagText("remarks") #></remarks>
<#+ }
else if (!string.IsNullOrEmpty(Context.GetGlobalProperty("DefaultBlankRemarksText")))
{
// Should you require a default comment, set it in
// Options -> Global Properties -> DefaultBlankRemarksText
#>
/// <remarks><#= Context.GetGlobalProperty("DefaultBlankRemarksText") #></remarks>
<#+ }
}
#>
步骤如图:
默认注释快捷键:shift+ctrl+D 效果如下图: