Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. How does IDisposable Interface Generate Dispose sub?

How does IDisposable Interface Generate Dispose sub?

Scheduled Pinned Locked Moved .NET (Core and Framework)
regextutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Polymorpher
    wrote on last edited by
    #1

    Ok, I have never understood this. Its an interface and therefore has no code in it...only 'stubs'. So when you implement it, it generates the Dispose method...and the Dispose method has code in it...how does this work? Id like to have my interface do something like this. for example, create a class and implement IDisposable, press enter and it will generate the following VARIABLES and code blocks, not just empty subs.             Private disposedValue As Boolean = False            ' To detect redundant calls             ' IDisposable             Protected Overridable Sub Dispose(ByVal disposing As Boolean)                   If Not Me.disposedValue Then                         If disposing Then                               ' TODO: free other state (managed objects).                         End If                         ' TODO: free your own state (unmanaged objects).                         ' TODO: set large fields to null.                   End If                   Me.disposedValue = True             End Sub #Region " IDisposable Support "             ' 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(ByVal disposing As Boolean) above.                   Dispose(True)                   GC.SuppressFinalize(Me)             End Sub

    P 1 Reply Last reply
    0
    • P Polymorpher

      Ok, I have never understood this. Its an interface and therefore has no code in it...only 'stubs'. So when you implement it, it generates the Dispose method...and the Dispose method has code in it...how does this work? Id like to have my interface do something like this. for example, create a class and implement IDisposable, press enter and it will generate the following VARIABLES and code blocks, not just empty subs.             Private disposedValue As Boolean = False            ' To detect redundant calls             ' IDisposable             Protected Overridable Sub Dispose(ByVal disposing As Boolean)                   If Not Me.disposedValue Then                         If disposing Then                               ' TODO: free other state (managed objects).                         End If                         ' TODO: free your own state (unmanaged objects).                         ' TODO: set large fields to null.                   End If                   Me.disposedValue = True             End Sub #Region " IDisposable Support "             ' 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(ByVal disposing As Boolean) above.                   Dispose(True)                   GC.SuppressFinalize(Me)             End Sub

      P Offline
      P Offline
      Paulo Zemek
      wrote on last edited by
      #2

      It's simple. The interface says that the object can be disposed before all references to it dies. The interface only says that. But, the common all-purposes implementation (the Dispose pattern) says that: Dispose() methods calls a virtual method Dispose that receives a boolean (true) and then calls GC.SuppressFinalize(this) and the destructor calls Dispose(false). So, if you want your code to have somme implementation, you will at least need to implement a class (say MyDisposable) that implements IDisposable (so the using keyword can work with it) and implements Dispose to call: Dispose(true); GC.SupressFinalize(this); And then, you make only the Dispose(disposing) virtual. You can even make your "only call once" approach inside the non-virtual Dispose().

      P 1 Reply Last reply
      0
      • P Paulo Zemek

        It's simple. The interface says that the object can be disposed before all references to it dies. The interface only says that. But, the common all-purposes implementation (the Dispose pattern) says that: Dispose() methods calls a virtual method Dispose that receives a boolean (true) and then calls GC.SuppressFinalize(this) and the destructor calls Dispose(false). So, if you want your code to have somme implementation, you will at least need to implement a class (say MyDisposable) that implements IDisposable (so the using keyword can work with it) and implements Dispose to call: Dispose(true); GC.SupressFinalize(this); And then, you make only the Dispose(disposing) virtual. You can even make your "only call once" approach inside the non-virtual Dispose().

        P Offline
        P Offline
        Polymorpher
        wrote on last edited by
        #3

        I dont know if I'm misunderstanding, or if maybe i posted my qustion wrong...basicly im trying to figure out something like this (VB) public interface ISomeInterface sub DoSomeStuff() end interface now when a sub class implements this i want DoSomeStuff() to have code already in it public class MyDoSomeStuffClass implements ISomeInterface private mCanDoStuff as boolean = true ***i want this to be generated*** public sub DoSomeStuff() Implements ISomeInterface.DoSomeStuff ***Start Generated*** if mCanDoStuff Then       msgbox("WoOT") End If ***end generated*** end sub end class

        -- "Keyboard not found. Press < F1 > to RESUME. " Source unknown (appears in many common BIOSes as a real error message)

        M P 2 Replies Last reply
        0
        • P Polymorpher

          I dont know if I'm misunderstanding, or if maybe i posted my qustion wrong...basicly im trying to figure out something like this (VB) public interface ISomeInterface sub DoSomeStuff() end interface now when a sub class implements this i want DoSomeStuff() to have code already in it public class MyDoSomeStuffClass implements ISomeInterface private mCanDoStuff as boolean = true ***i want this to be generated*** public sub DoSomeStuff() Implements ISomeInterface.DoSomeStuff ***Start Generated*** if mCanDoStuff Then       msgbox("WoOT") End If ***end generated*** end sub end class

          -- "Keyboard not found. Press < F1 > to RESUME. " Source unknown (appears in many common BIOSes as a real error message)

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Polymorpher wrote:

          ***i want this to be generated***

          I would ask on the Visual Studio board[^]... There's probably an easy way to make a macro or something that does this....I have no experience with that.

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • P Polymorpher

            I dont know if I'm misunderstanding, or if maybe i posted my qustion wrong...basicly im trying to figure out something like this (VB) public interface ISomeInterface sub DoSomeStuff() end interface now when a sub class implements this i want DoSomeStuff() to have code already in it public class MyDoSomeStuffClass implements ISomeInterface private mCanDoStuff as boolean = true ***i want this to be generated*** public sub DoSomeStuff() Implements ISomeInterface.DoSomeStuff ***Start Generated*** if mCanDoStuff Then       msgbox("WoOT") End If ***end generated*** end sub end class

            -- "Keyboard not found. Press < F1 > to RESUME. " Source unknown (appears in many common BIOSes as a real error message)

            P Offline
            P Offline
            Paulo Zemek
            wrote on last edited by
            #5

            It's impossible. Interfaces only says: The method exist. You can, of course, do: IMyInterface (with some methods) BaseClassThatImplementsIMyInterface A class with virtual methods that implements the interface and, so, can create the basic functionality. You can also create a helper class. For example: IMyInterface -> MyMethod If your class comes from some other base class, it can implement IMyInterface and, in MyMethod, call MyInterfaceBasicImplementation.MyMethod() And, finally, you can create extension methods using interfaces. So, for example: IDictionary has the TryGetValue method. I implemented a method called GetValueOrDefault as an extension method. It is NOT virtual, but as the TryGetValue is, it is enough for me.

            P 1 Reply Last reply
            0
            • P Paulo Zemek

              It's impossible. Interfaces only says: The method exist. You can, of course, do: IMyInterface (with some methods) BaseClassThatImplementsIMyInterface A class with virtual methods that implements the interface and, so, can create the basic functionality. You can also create a helper class. For example: IMyInterface -> MyMethod If your class comes from some other base class, it can implement IMyInterface and, in MyMethod, call MyInterfaceBasicImplementation.MyMethod() And, finally, you can create extension methods using interfaces. So, for example: IDictionary has the TryGetValue method. I implemented a method called GetValueOrDefault as an extension method. It is NOT virtual, but as the TryGetValue is, it is enough for me.

              P Offline
              P Offline
              Polymorpher
              wrote on last edited by
              #6

              It's not impossible, IDisposable does it. There is a way to do it, im just trying to figure out what that way is.

              -- "Keyboard not found. Press < F1 > to RESUME. " Source unknown (appears in many common BIOSes as a real error message)

              T 1 Reply Last reply
              0
              • P Polymorpher

                It's not impossible, IDisposable does it. There is a way to do it, im just trying to figure out what that way is.

                -- "Keyboard not found. Press < F1 > to RESUME. " Source unknown (appears in many common BIOSes as a real error message)

                T Offline
                T Offline
                The Man from U N C L E
                wrote on last edited by
                #7

                I think it is implemented using the CodeDOM. Visual Studio will be calling a custom code generator for inserting that snippet of code. Can't find it though. Found the stuff for generating the default constuctor code, typed datasets, and all sorts by using reflector, but not Dispose. Must be hidden away in the bowels of the framework. A good article on using the CodeDOM can be found here[^]

                If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                P 1 Reply Last reply
                0
                • T The Man from U N C L E

                  I think it is implemented using the CodeDOM. Visual Studio will be calling a custom code generator for inserting that snippet of code. Can't find it though. Found the stuff for generating the default constuctor code, typed datasets, and all sorts by using reflector, but not Dispose. Must be hidden away in the bowels of the framework. A good article on using the CodeDOM can be found here[^]

                  If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                  P Offline
                  P Offline
                  Polymorpher
                  wrote on last edited by
                  #8

                  Thanks!

                  -- "Keyboard not found. Press < F1 > to RESUME. " Source unknown (appears in many common BIOSes as a real error message)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups