Calling Shared Constructors/Finalizers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xenomech@gmail.com

    #1

    Calling Shared Constructors/Finalizers

    Is there a way to explicitly call a shared constructor or shared
    finalizer in a static class?

  • lord.zoltar@gmail.com

    #2
    Re: Calling Shared Constructors/Finalizers


    Xenomech@gmail. com wrote:
    Is there a way to explicitly call a shared constructor or shared
    finalizer in a static class?
    I'm not sure I even understand why it would be necessary... can you
    explain a little more please?

    Comment

    • Brian Gideon

      #3
      Re: Calling Shared Constructors/Finalizers

      Xenomech@gmail. com wrote:
      Is there a way to explicitly call a shared constructor or shared
      finalizer in a static class?
      Hi,

      No, there is not. Also, shared (static) finalizers do not exist. Can
      you explain a bit more about what you're trying to accomplish?

      Brian

      Comment

      • timexsinclair2068@hotmail.com

        #4
        Re: Calling Shared Constructors/Finalizers

        Hi,

        The original poster and I work together. I've read (i.e.
        http://www.csharpfriends.com/Article...?articleID=116)
        that it is possible. But we could't find how to actually do it.

        We need to restart our application. So instead of quitting and
        re-executing it, we thought we could programatically invoke the class
        constructors to re-initialize everything.

        Paul


        Brian Gideon wrote:
        Xenomech@gmail. com wrote:
        Is there a way to explicitly call a shared constructor or shared
        finalizer in a static class?
        >
        Hi,
        >
        No, there is not. Also, shared (static) finalizers do not exist. Can
        you explain a bit more about what you're trying to accomplish?
        >
        Brian

        Comment

        • Brian Gideon

          #5
          Re: Calling Shared Constructors/Finalizers


          timexsinclair20 68@hotmail.com wrote:
          Hi,
          >
          The original poster and I work together. I've read (i.e.
          http://www.csharpfriends.com/Article...?articleID=116)
          that it is possible. But we could't find how to actually do it.
          >
          We need to restart our application. So instead of quitting and
          re-executing it, we thought we could programatically invoke the class
          constructors to re-initialize everything.
          >
          Paul
          >

          Paul,

          My guess is that the article is referring to the possibility of calling
          the type initializer via reflection. I'm not sure I would use the word
          explicit to describe it though.

          Module Module1

          Sub Main()
          Dim t As Type = GetType(Test)
          t.TypeInitializ er.Invoke(Nothi ng, Nothing)
          End Sub

          End Module

          Public Class Test

          Shared Sub New()
          Console.WriteLi ne("It worked!")
          End Sub

          End Class

          Note that "It worked!" is printed twice. Also, this a pretty flagrant
          hack. I recommend considering a different approach to reinitializes
          the application's state.

          Brian

          Comment

          • timexsinclair2068@hotmail.com

            #6
            Re: Calling Shared Constructors/Finalizers

            Thanks a lot Brian.

            I know doing this is definitely not elegant. However, I'm not sure what
            could go wrong?


            Paul

            Brian Gideon wrote:
            timexsinclair20 68@hotmail.com wrote:
            Hi,

            The original poster and I work together. I've read (i.e.
            http://www.csharpfriends.com/Article...?articleID=116)
            that it is possible. But we could't find how to actually do it.

            We need to restart our application. So instead of quitting and
            re-executing it, we thought we could programatically invoke the class
            constructors to re-initialize everything.

            Paul
            >
            >
            Paul,
            >
            My guess is that the article is referring to the possibility of calling
            the type initializer via reflection. I'm not sure I would use the word
            explicit to describe it though.
            >
            Module Module1
            >
            Sub Main()
            Dim t As Type = GetType(Test)
            t.TypeInitializ er.Invoke(Nothi ng, Nothing)
            End Sub
            >
            End Module
            >
            Public Class Test
            >
            Shared Sub New()
            Console.WriteLi ne("It worked!")
            End Sub
            >
            End Class
            >
            Note that "It worked!" is printed twice. Also, this a pretty flagrant
            hack. I recommend considering a different approach to reinitializes
            the application's state.
            >
            Brian

            Comment

            Working...