How To: Auto Instantiate Singleton in C# -


I have to be a singleton which will be automatically prompt when the program starts.

What do I mean "automatic instant" is that the code in singleton should be done only by any call or other code without any announcements or declarations, only when the program is started.

So I want to do something like the following and start the "MySingleton Instantiated" program (nothing without the main code) ...

  Fixed class MySstoning {Private constant MyScittington Self = New MyScottington (); MySingTone Protected () {System.Console.WriteLine ("MySstington Instant"); }}  

This does not work because C # only starts static members of a class as needed, that is when they are accessed / etc

< P> So what do I do? Can it be done?

I did not do this individually with C ++ (have not used C ++ for some time), but I am pretty sure that it can be done in C ++ But not sure about C #.

Any help is appreciated. Thank you.


What I'm actually doing with it ... Many of these single classes will be (more can be added as time), all of which are a common (abstract) Is obtained from the square (aka piculs).

There will be a static member in PCLs, which is a collection of PCLas ... and a constructor to add itself to the collection ...

Then in the theory of all the songwriters Automatically will be added to the collection (since they make up the original PCOS constructor and add new objects to the collection) ... then the collection can be used

Unfortunately I have children Can not get (Any) classes have been implemented, and new children (singleton) classes can be added at any time. To instantiate myself ... screwing up my small plan, resulting in this post.

Hope I explained this enough.


Yes I know there are bad feelings about the use of singletons and their surroundings ... but they are sometimes useful, and even if Satan himself If you have made a singleton, then I would still like to know that my problem can be achieved in C #. Please thank everyone.

The IOC approach mentioned by Chris is probably the best, but if the failure is the "best" solution I Some fiku have to do with reflections and attributes with those lines:

  public class InitOnLoad: attribute {public static zero start () {// Get a list of the input properties type Are marked with type in App Domains. T. Centum Gate Assembly (). Selection Mono (A = & gt; A.GetType ()) where t.GetCustomAttributes (typeof (InitOnLoad), incorrect). ) & Gt; 0 select; // Type every process to force it to start (type in type) {// Try to find a static field that says declared class var field = type.GetFields (System.Reflection.BindingFlags.Static | System.Reflections. Baddingsflags.bb | system | reflections.badingsflags.noppolic). Where (F => F. Field Type == type). FirstOver Default (); // Evaluate the static field if found (field! = Zero) field Gate value (empty); }}} [InitOnLoad] public square fu {public stationary fu x = new fu (); Private Foo () {Console.WriteLine ("FU is automatically initial"); }} Public square bar {public static bar x = new bar (); Private Bar () {Console.WriteLine ("The bar has only begun essentially"); }}  

InitOnLoad.Initialize () has been added to your main method of calling.

You can remove from the attribute, but this can be due to the unnecessary type can be started and the use of useless memory (such as the bar in the code above).

It is also worth noting that it will not handle the type of types contained in any assemblies, which are loaded dynamically, but depending on your query (" But automatically ") which does not sound like any problem for you.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -