0

I have created a project and in this project, I have lots of classes for example: States, Cities, Companies, Buildings, Animals, Forests...

My problem is that I can do this:

$"On class {className[0]} you created {numberOfInstances[0]}"

I find all class names via reflection, but I don´t know how to count instances without changing constructor in all created classes.

Or how can I read all new() operators(modifiers) in my project?

I will be happy for any constructive answer

UPDATE:

I understand that count instances without change constructor is inposible. Can You help me to find app or working code, with I can change all constructors?

AndyEXP
  • 3
  • 4
  • Related or even a duplicate: https://stackoverflow.com/q/302542/9363973 – MindSwipe Jul 16 '19 at 07:09
  • or https://stackoverflow.com/questions/12276641/count-instances-of-the-class – TheGeneral Jul 16 '19 at 07:09
  • Why do you need to do this (maybe there's a better way to do whatever you want to)? – ProgrammingLlama Jul 16 '19 at 07:14
  • [TheGeneral] I cant use counters on class, because I have lots of classes. [MindSwipe] I was seen this duplicate but this not works for me or I not use this correctly :( – AndyEXP Jul 16 '19 at 07:15
  • 2
    The accepted answer on the question I linked states that this is impossible with reflection, and the other way is to do it by intercepting the object creation call using a container, which you probably can't do as this would need you to rewrite a large part of the application and of the classes. P.S. use @UserName (e.g @MindSwipe) for mentioning people in comments, we get a notification if we do get mentioned and so we can respond faster – MindSwipe Jul 16 '19 at 07:24
  • @John I can block all users who create more instances of the same classes. For Example if somebody will create 100 000 instances of Class Building the program automatically hide AddInstanceCommand. Is any change to do this? – AndyEXP Jul 16 '19 at 07:26
  • Beyond MindSwipe's suggestion, which was the same idea I came up with, I'm not sure there is. – ProgrammingLlama Jul 16 '19 at 07:32
  • @MindSwipe Thank You for your time, with helping me. And if I can find all new() operators in my program? I know that is stupid question, but if something for finding name of operators(modifiers) exists I will happy. – AndyEXP Jul 16 '19 at 07:36
  • Using a IDE this is quite simple: In Visual Studio and Visual Studio Code you can search through your entire project by pressing **Ctrl + Shift + F** – MindSwipe Jul 16 '19 at 07:42
  • @MindSwipe thank You :D, I think some code. After I can count all new(), or count all new operators(modifiers) founded in Class1 and Class2..... Is this possible?? I can find namespaces, classes, access operators(modifiers), but I have problem find class operators(modifiers). Can You help with this? – AndyEXP Jul 16 '19 at 08:16
  • 1
    `And if I can find all new() operators in my program?` Change the constructor to `private`. Then build the solution. The compiler will now helpfully find all of the places you try and `new` up the type, since they'll all be broken. _I am not sure how that is helpful to you, but it will work._ – mjwills Jul 16 '19 at 08:42

0 Answers0