This is a slight deviation in this question: Can I use a singleton with a non-default constructor in C #?
I have a class that takes parameters for its constructor. I would like to make this singleton so that the parameters are taken when the singlet was initialized and, therefore, it did not need to be passed every time the instance is retrieved.
My solution (which is not elegant) for this is to have a static CreateInstance() method that takes parameters and constructs a singleton instance. Then I would have another static GetInstance() method, which would be without parameters to get a singleton instance. In the code, I would need to provide the logical calls to CreateInstance before any calls to GetInstance. However, I cannot provide this at compile time. However, I can check at runtime by throwing an exception in GetInstance if it is called before CreateInstance .
In any case, can I achieve this behavior while compiling time? Or at least is there a better way to do the same?
parameter-passing c # singleton
millie
source share