In preceding articles we hit seen how you crapper create and run bespoken cipher snippets and add them to your project’s cipher using Visual Studio. We hit also investigated the ingest of replacements that create placeholders that crapper be overwritten with resource from the desegrated utilization surround (IDE). In this article we module inform a cipher piece function.
ClassName Function
Code snippets crapper earmark literals that crapper be utilised with overwriteable placeholders. They crapper also earmark a sort of functions that create cipher according to the duty at which they are added, or in salutation to aggregation provided by the programmer. An warning duty is ClassName. This inserts the study of the collection in which the piece is inserted.
In a preceding article we saw a piece that inserted every of the cipher for the base IDisposable ornament but without a finalizer. The finalizer could not be additional automatically because the study of its method includes the study of the class. In this article we module create the flooded IDisposable feat in a cipher piece using the ClassName duty to create the finalizer name.
Creating the Snippet File
The XML for the cipher piece is shown below. There are individual differences when compared to the example IDisposable snippet. The prototypal is that the Shortcut continuance has been denaturized to “idispf”. This module earmark you to run the piece alongside the original, which utilised “idisp”.
The ordinal modify is in the Declarations country of the XML. A Literal surroundings is circumscribed to stop the info that module be inserted into the code. As with accepted replacements, a unequalled ID for the exact and a ToolTip hit been defined. In addition, the Function surroundings holds the study of the duty utilised to create the cipher for the placeholder. For the ClassName duty this is “ClassName()”. Note that the Editable concept of the exact is ordered to false. This nonmandatory continuance prevents the knowledge to journalism to the agent and overtype its value.
Finally, a agent for the exact has been additional to the cipher as $classname$. Combined with a tilde case (~), this creates the study of the finalizer method. The agent module be replaced with the collection study on intromission of the snippet.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>Simple IDisposable Code Snippet</Title>
<Author>BlackWasp</Author>
<Shortcut>idispf</Shortcut>
<Description>Code for the flooded IDisposable pattern
with finalizer.</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>Class name</ToolTip>
<Function>ClassName()</Function>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[private bool _disposed;
public vacuum SampleMethod()
{
if (_disposed) intercommunicate newborn ObjectDisposedException("MyObject");
}
protected realistic vacuum Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// Dispose managed resources
}
// Dispose unmanaged resources
_disposed = true;
}
}
public vacuum Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~$classname$()
{
Dispose(false);
}
]]></Code>
</Snippet>
</CodeSnippet>