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. C#
  4. How to implement a singleton in c# generic classes

How to implement a singleton in c# generic classes

Scheduled Pinned Locked Moved C#
csharplinuxtutorialquestion
6 Posts 3 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.
  • C Offline
    C Offline
    Chris Richner
    wrote on last edited by
    #1

    Hi, My class looks like

    public abstract class ShellApplicationBase<TModule, TModuleLoader> : BaseNet.Icarus.Shell.Interfaces.IShellApplication
    {
    }

    Every class in my app should now be able to access the shell class. Therefore I wanted to implement a singleton where one could access Shell.Instance.DoNothingAndWaitInfite(); to do so i did

    static IShellApplication _instance;
    public IShellApplication Instance
    {
    get
    {
    return _instance;
    }
    }

    so far so good, there is a static public IShellApplication Property on my shell class. Now one would have the idea to access this singelton inside the app

    ShellApplicationBase<???>.Instance.DoNothingAndWaitInfite();

    How can one access the generic class .Instance without knowing the concrete Types ( ??? ) that have been used to make a new instance of the generic class? I just like to access this shell class from everywhere in my app, any ideas?

    myMsg.BehindDaKeys = "Chris Richner";

    J D C 3 Replies Last reply
    0
    • C Chris Richner

      Hi, My class looks like

      public abstract class ShellApplicationBase<TModule, TModuleLoader> : BaseNet.Icarus.Shell.Interfaces.IShellApplication
      {
      }

      Every class in my app should now be able to access the shell class. Therefore I wanted to implement a singleton where one could access Shell.Instance.DoNothingAndWaitInfite(); to do so i did

      static IShellApplication _instance;
      public IShellApplication Instance
      {
      get
      {
      return _instance;
      }
      }

      so far so good, there is a static public IShellApplication Property on my shell class. Now one would have the idea to access this singelton inside the app

      ShellApplicationBase<???>.Instance.DoNothingAndWaitInfite();

      How can one access the generic class .Instance without knowing the concrete Types ( ??? ) that have been used to make a new instance of the generic class? I just like to access this shell class from everywhere in my app, any ideas?

      myMsg.BehindDaKeys = "Chris Richner";

      J Offline
      J Offline
      Jun Du
      wrote on last edited by
      #2

      You can code this way:

      ShellApplicationBase<SomeConcretClassType>.GetInstance().DoNothingAndWaitInfite();

      or

      ShellApplicationBase<SomeConcretClassType> myInstance = ShellApplicationBase<SomeConcretClassType>.GetInstance();
      myInstance.DoNothingAndWaitInfite();

      Note that GetInstance() has been changed from Property to a public static method. I'm not sure about if you can use Property before any instance is constructed. -- modified at 12:43 Friday 8th September, 2006

      Best, Jun

      C 1 Reply Last reply
      0
      • J Jun Du

        You can code this way:

        ShellApplicationBase<SomeConcretClassType>.GetInstance().DoNothingAndWaitInfite();

        or

        ShellApplicationBase<SomeConcretClassType> myInstance = ShellApplicationBase<SomeConcretClassType>.GetInstance();
        myInstance.DoNothingAndWaitInfite();

        Note that GetInstance() has been changed from Property to a public static method. I'm not sure about if you can use Property before any instance is constructed. -- modified at 12:43 Friday 8th September, 2006

        Best, Jun

        C Offline
        C Offline
        Chris Richner
        wrote on last edited by
        #3

        Hi Jun Du, One have to know the SomeConcretClassType to access the class type to get the Instance property. What happens now if the class instance was made by

        new ShellApplicationBase();

        but one access the singleton inside the application as

        ShellApplicationBase.Instance.DoNothingAndWaitInfite();

        as far as I could see this is not the same but it would work, right? There must be a way to retrieve this Instance property on this generic class without giving types to access just the class type, am I wrong?

        myMsg.BehindDaKeys = "Chris Richner";

        J 1 Reply Last reply
        0
        • C Chris Richner

          Hi Jun Du, One have to know the SomeConcretClassType to access the class type to get the Instance property. What happens now if the class instance was made by

          new ShellApplicationBase();

          but one access the singleton inside the application as

          ShellApplicationBase.Instance.DoNothingAndWaitInfite();

          as far as I could see this is not the same but it would work, right? There must be a way to retrieve this Instance property on this generic class without giving types to access just the class type, am I wrong?

          myMsg.BehindDaKeys = "Chris Richner";

          J Offline
          J Offline
          Jun Du
          wrote on last edited by
          #4

          Chris Richner wrote:

          One have to know the SomeConcretClassType to access the class type to get the Instance property.

          Yes, that is what you used generic class for. If you don't or needn't know SomeConcretClassType, why do you use a generic class?

          Chris Richner wrote:

          What happens now if the class instance was made by new ShellApplicationBase();

          No, you can't call Singleton's contructor directly. It's protected. You have to get the instance via static call GetInstance().

          Chris Richner wrote:

          There must be a way to retrieve this Instance property on this generic class without giving types to access just the class type, am I wrong?

          Give a try, but I never thought we could call any method of a generic class without defining a "concret" class first.

          Best, Jun

          1 Reply Last reply
          0
          • C Chris Richner

            Hi, My class looks like

            public abstract class ShellApplicationBase<TModule, TModuleLoader> : BaseNet.Icarus.Shell.Interfaces.IShellApplication
            {
            }

            Every class in my app should now be able to access the shell class. Therefore I wanted to implement a singleton where one could access Shell.Instance.DoNothingAndWaitInfite(); to do so i did

            static IShellApplication _instance;
            public IShellApplication Instance
            {
            get
            {
            return _instance;
            }
            }

            so far so good, there is a static public IShellApplication Property on my shell class. Now one would have the idea to access this singelton inside the app

            ShellApplicationBase<???>.Instance.DoNothingAndWaitInfite();

            How can one access the generic class .Instance without knowing the concrete Types ( ??? ) that have been used to make a new instance of the generic class? I just like to access this shell class from everywhere in my app, any ideas?

            myMsg.BehindDaKeys = "Chris Richner";

            D Offline
            D Offline
            Daniel Grunwald
            wrote on last edited by
            #5

            public class Test { public static int member; } Test.member = 2; Test.member = 3; Console.WriteLine(Test.member); // outputs 2 Every specialisation of Test will have it's own static fields, so you always need a concrete type. If you just want one instance for all Test, you have to create another non-generic class to hold the field and property.

            1 Reply Last reply
            0
            • C Chris Richner

              Hi, My class looks like

              public abstract class ShellApplicationBase<TModule, TModuleLoader> : BaseNet.Icarus.Shell.Interfaces.IShellApplication
              {
              }

              Every class in my app should now be able to access the shell class. Therefore I wanted to implement a singleton where one could access Shell.Instance.DoNothingAndWaitInfite(); to do so i did

              static IShellApplication _instance;
              public IShellApplication Instance
              {
              get
              {
              return _instance;
              }
              }

              so far so good, there is a static public IShellApplication Property on my shell class. Now one would have the idea to access this singelton inside the app

              ShellApplicationBase<???>.Instance.DoNothingAndWaitInfite();

              How can one access the generic class .Instance without knowing the concrete Types ( ??? ) that have been used to make a new instance of the generic class? I just like to access this shell class from everywhere in my app, any ideas?

              myMsg.BehindDaKeys = "Chris Richner";

              C Offline
              C Offline
              Chris Richner
              wrote on last edited by
              #6

              Hi, Thanks for your ansswers, don't get confused about the singelton pattern. I know that I didn't presented the whole code here that the singleton pattern would make sense and run. It isn't that much about the singleton pattern, just about accessing a static field on a generic class. It seems to me that the big feature I really like about generics is going to make me some troubles accessing a single running instance of class within the application code. Maybe this is just a indicator that the app design isn't that good yet. Thanks any way

              myMsg.BehindDaKeys = "Chris Richner";

              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