title: VB.Net Initialising a class using System.Reflection and System.Type to create a session based singleton extension method
body: <p>I have had several occasions recently to access a specific class several times over a relatively small time frame.</p>
  
  <p>So I've been storing the value of the class in Session and trying to access it on page load, if it's not available creating a new instance and storing that in session.</p>
  
  <p>So instead of constantly replicating the same code for different classes on different pages I'm trying to create an extension method to do this for me.</p>
  
  <p>I want to use it like this</p>
  
  <pre><code>Dim objName as MyClass
  
  objName.SessionSingleton()
  </code></pre>
  
  <p>So far this is what I have for my extension method:</p>
  
  <pre><code>    &lt;Extension()&gt; _
      Public Sub SessionSingleton(ByRef ClassObject As Object)
          Dim objType As Type = ClassObject.GetType
          Dim sessionName As String = objType.FullName
          If TypeOf HttpContext.Current.Session(sessionName) Is objType And HttpContext.Current.Session(sessionName) &lt;&gt; "" Then
              ClassObject = HttpContext.Current.Session(sessionName)
          Else
              Dim singleton As Object = New objType???????
  
              HttpContext.Current.Session(sessionName) = singleton
              ClassObject = singleton
  
          End If
      End Sub
  </code></pre>
  
  <p>I'm stuck on what to do when I make my new instance of my class (it would have to have a New() sub)</p>
  
  <p>I'm not sure where to go from here... or even if this is the best way to do it. </p>