How to implement a singleton in c# generic classes
-
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";
-
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";
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
-
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
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 bynew 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";
-
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 bynew 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";
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
-
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";
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.
-
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";
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";