How to automatically insert a code fragment when implementing an interface, for example, when implementing IDisposable

Is it possible to automatically insert a code fragment when implementing the interface? If so, how do you do it? I am looking for something similar when you implement IDispoable in VB.

eg.

I have the following interface

Public Interface ITransferable

    ReadOnly Property TransferParameters() As Object

End Interface

I also have a code snippet as follows

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Transfer Parameter Snippet</Title>
      <Author>RMC</Author>
      <Description>Transfer Parameter Snippet used when implementing the ITRansferable Interface</Description>
    </Header>
    <Snippet>
      <Code Language="VB" Kind="" Delimiter="$">
        <![CDATA[
          Private m_oTransferParameters As Object

          Public Class InputParameters
              'Add Input Parameter declaration/s here
          End Class

          Public Class OutputParameters
              'Add Output Parameter declaration/s here
          End Class

          Public ReadOnly Property TransferParameters() As Object Implements ITransferable.TranferParameters

              Get
                  Return m_oTransferParameters 
              End Get

          End Property
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

So, what I want, when the ITransferable interface is implemented, it injects the code in the code fragment into a class that implements the interface, for example

Public Class NewClass()
    Implements ITransferable

    Private m_oTransferParameters As Object

    Public Class InputParameters
        'Add Input Parameter declaration/s here
    End Class

    Public Class OutputParameters
        'Add Output Parameter declaration/s here
    End Class

    Public ReadOnly Property TransferParameters() As Object Implements ITransferable.TranferParameters

        Get
             Return m_oTransferParameters 
         End Get

    End Property

End Class

This will be used by web forms when passing parameters from one page to another using Server.Transfer

+3
source share
1 answer

, , , , , , , - ITransferable Interface, , -, " ", - , . , - .

+1

Source: https://habr.com/ru/post/1758902/


All Articles