I have a class. In c#, is there some way (which i can implement in a static method) which can identify an object of this class, instantiated by some other (calling) class?
Scenario
Actually I have a class of Country, which contains a member Rectangle (having 4 coordinates). I want to write a static method which, when passed a point or vector2d, searches for that point in each of the instantiated objects of Country and can return ref of that object.
I have Rectangle.Contains(point) method which tells if that point lies in calling rectangle.
You would have to store all instances of your class in a collection, and search the collection to find the specific object you need.
You could make this collection a static member of your class, but it doesn’t need to be. It could instead be stored elsewhere as an ordinary member of another class. For example if you have a
CountryFactorythat you use to construct yourCountryobjects then each time the factory constructs a country it could also store a reference to that country in a collection inside the factory.Remember also to remove the country from the collection when you no longer need it, otherwise it won’t be garbage collected (or use a
WeakReference).