Inventing The Wheel, Simple Practice; Simulate(Of T As IDisposable)
Public Structure Simulate(Of T As IDisposable)
Public Sub New(value As T)
Func = Function() CType(value, IDisposable)
End Sub
Public ReadOnly Property Func As Func(Of IDisposable)
End Structure
Usage:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim graphicsSimulator As Simulate(Of Graphics) = New Simulate(Of Graphics)(Me.CreateGraphics)
Using CType(graphicsSimulator.Func().Invoke(), Graphics)
CType(graphicsSimulator.Func().Invoke(), Graphics).FillRectangle(SystemBrushes.WindowFrame, New Rectangle(30, 30, 100, 0))
End Using
Dim sqlConnectionSimulator As Simulate(Of System.Data.SqlClient.SqlConnection) = New Simulate(Of System.Data.SqlClient.SqlConnection)(New System.Data.SqlClient.SqlConnection)
Using CType(sqlConnectionSimulator.Func().Invoke(), System.Data.SqlClient.SqlConnection)
Dim simulate = CType(sqlConnectionSimulator.Func().Invoke(), System.Data.SqlClient.SqlConnection)
simulate.ConnectionString = "your connection string"
simulate.Open()
' do something with your connection
End Using
End Sub
Public Sub New(value As T)
Func = Function() CType(value, IDisposable)
End Sub
Public ReadOnly Property Func As Func(Of IDisposable)
End Structure
Usage:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim graphicsSimulator As Simulate(Of Graphics) = New Simulate(Of Graphics)(Me.CreateGraphics)
Using CType(graphicsSimulator.Func().Invoke(), Graphics)
CType(graphicsSimulator.Func().Invoke(), Graphics).FillRectangle(SystemBrushes.WindowFrame, New Rectangle(30, 30, 100, 0))
End Using
Dim sqlConnectionSimulator As Simulate(Of System.Data.SqlClient.SqlConnection) = New Simulate(Of System.Data.SqlClient.SqlConnection)(New System.Data.SqlClient.SqlConnection)
Using CType(sqlConnectionSimulator.Func().Invoke(), System.Data.SqlClient.SqlConnection)
Dim simulate = CType(sqlConnectionSimulator.Func().Invoke(), System.Data.SqlClient.SqlConnection)
simulate.ConnectionString = "your connection string"
simulate.Open()
' do something with your connection
End Using
End Sub
Comments
Post a Comment