Is there a way for a static method to access all non-static instances of a class?

This is probably a dumb question, but I’ll ask him anyway ... I am programming in C # .NET. I have a class that contains a non-static instance of EventHandler. Is it possible to trigger this EventHandler for every instance of a class that exists from a static method? I know this is a long shot!

+4
source share
4 answers

You can do this, but you need to create a static collection of all your objects:

public class Thing { public static List<Thing> _things = new List<Thing>(); public Thing() { _things.Add(this); } public static void SomeEventHandler(object value, EventHandler e) { foreach (Thing thing in _things) { // do something. } } } 

You will want to monitor the accumulation, there may also be β€œThings”. Make sure you remove them from the list when you no longer need them.

+5
source

No no. Basically, there is no way to find all instances of a class unless you have written your own code for this.

EDIT: Intrigued why this is being subverted. In any case, to add a little more detail, you should avoid the need for this. You can make your implementation type IDisposable , and then register against the static event handler in the constructor and unregister in the Dispose method. Hell, you might even have a finalizer that does this for you, which will cost you performance, but at least there won't be a leak if you don't want to dispose of the instance.

However, all of these are somewhat grim options. It would be much better to try redesigning to avoid the requirement. Perhaps you can provide us with more information on what you are trying to do, and we can come up with a workaround?
+4
source

You can do the following:

  public class MyClass{ private static List<MyClass> Instances = new List<MyClass>(); public MyClass(){ lock(typeof(MyClass)){ Instances.Add(this); } }} 

After that, you can do whatever you want with instances.

+1
source

I could be wrong in understanding what you mean, but it should just be ...

This is the main file.

 using System; using IdeaClass; namespace TestIdeas { class Program { static void Main(string[] args) { Ideas i = new Ideas(); Ideas.triggerMany(); Console.ReadLine(); } } } 

Then there is the Ideas class:

 using System; namespace IdeaClass { public class Ideas { static OtherClass oc = new OtherClass(); public static void triggerMany() { oc.runThing("textual"); } public Ideas() { Ideas.oc.ThingEvent += DoThingHandler; } public void DoThingHandler(string thing) { System.Console.WriteLine(thing); } } } 

And then another class.

 using System; namespace IdeaClass { class OtherClass { public delegate void DoThing(string text); public event DoThing ThingEvent; public void runThing(string text) { if (ThingEvent != null) { ThingEvent(text); } } } } 

This leads to an unsuccessful connection between the class that raises the event and the class with a static call, but it seems to do what you want.

+1
source

Source: https://habr.com/ru/post/1302753/


All Articles