How does IDisposable Interface Generate Dispose sub?
-
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
-
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
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().
-
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().
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)
-
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)
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:
-
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)
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.
-
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.
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)
-
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)
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]
-
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]
Thanks!
-- "Keyboard not found. Press < F1 > to RESUME. " Source unknown (appears in many common BIOSes as a real error message)