Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When you have a class that implements IDisposable and you press return after IDisposable, the IDE adds a load of code into the class like:-

VB
#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region


is there any way to get a similar behaviour with my own built interfaces?

Note - I don't want to do anything with IDisposable itself. I just want to do to my interfaces what the IDE does with IDisposable - all of the above is put in by the IDE when I hit return after "Implements Disposable" (comments , region etc.) - I want my interface to do the same so that usage hints can be put in when a developer implements my interface.

Note 2: OK - I have decided to use code snippets instead - it is just as easy from the developer point of view.
Posted
Updated 16-Jul-15 22:15pm
v3
Comments
[no name] 16-Jul-15 11:39am    
You mean like create your own code snippet?
Duncan Edwards Jones 16-Jul-15 11:40am    
Yes - the code snippet that automatically triggers when someone implements my interface. (Like what IDisposable does)
[no name] 16-Jul-15 11:54am    
Sorry I misunderstood. I use code snippets for inserting code like that. I am thinking that you are asking how to hook into the refactoring stuff inside VS.
Sergey Alexandrovich Kryukov 16-Jul-15 12:29pm    
Automatically triggers — on what event, call, or code fragment?
"Similar behavior" — in what aspects similar?
—SA
Duncan Edwards Jones 16-Jul-15 12:41pm    
When you are in Visual Studio with a VB class open and you type "Implements IDisposable [return]" it pastes in all that stuff above.

if you type in "Implements IDuncansInterfaceThing" it only pastes in the method/property signatures - no comments or other boilerplate.

(Maybe it is not possible for me to do)

1 solution

Please see my comments to the question. From your question, it's hard to understand if you really understand how even the existing syntactic sugar works.

I suspect that you don't. I think so because you did not even try to demonstrate how the usage of syntactic sugar should work. For this kind, the essence is not just the interface itself, but the VB.NET or C# using statement:
https://msdn.microsoft.com/en-us/library/htd05whh.aspx[^],
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^].

If I'm mistaken about you; and you are really do understand how it works, I would be only very glad.

See also: http://en.wikipedia.org/wiki/Syntactic_sugar[^].

You never showed anything like Using... End using. Besides, your Protected Overridable Sub Dispose(disposing As Boolean) is totally irrelevant.

As you can see from the code sample, "End using" take place in the code. This is the point on code where the IDisposable.Dispose methods of "resourcelist | resourceexpression" are called. Note: nothing else. All you can do is to implement IDisposable in different ways. If, instead of IDisposable, you derive your own interface instead from this interface, everything will work exactly the same, because you will still have to implement IDisposable.Dispose, and this implementation will be called exactly in the same way. You only will add some more members and will have to implement them.

Now, pay attention that Using... End using is the part of the language syntax. And you cannot alter the language syntax. You can only change the syntax if you implement your own language, which is also possible. Otherwise, your question makes little sense.

This way, you got the answer. If you have some other ideas, you have to explain that. Right now, it's pretty much nothing. Sorry, but I hope you got some food for thought.

—SA
 
Share this answer
 
v4
Comments
Duncan Edwards Jones 17-Jul-15 2:47am    
No - I obviously needed to clarify my question. I don't want to do anything with IDisposable - I just want to do what it does in the IDE.
Sergey Alexandrovich Kryukov 17-Jul-15 2:58am    
Then clarify it. :-)

By the way, in my answer I mostly address the case where you have nothing to do with System.IDisposable. You cannot use another interface this way, because you cannot modify the language syntax.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900