0

I'm sure there's a word for this but have had no luck finding anything on it. I have several classes within 'Namespace.Objects' and i want to run a static function called 'RunThis' on all the classes within the namespace without having to type out 'Objects.Class1.RunThis()' for each class. I want to run this from the Namespace.MainClass object and get the value the function returns.I also need to be able to check what the name of the object is to avoid trying to run it on a class that doesn't have it since there is a base class object in there that should be ignored that all the others inherit from.

Namespace(namespace)
....Objects(namespace)
........BaseClass(class)
........Class1(class)
........Class2(class)
........Class3(class)
........Class4(class)
....MainClass(class)

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • Is the method you need to run a Static Method or a instance class method ? If its an instance method, then you are at least going to need to create an instance of all of the classes in order to call it. In order to make sure all those classes have such a method, they should implement an interface that delcares the method. What actual problem are you facing ? – PhillipH Feb 10 '18 at 20:12
  • im aware of this but its not what im looking for. that would require me to add a new line every time i add a new class. so that id have a bunch of 'class1.runthis' 'class2.runthis' which is what i want to avoid. I want something to basically loop through the classes within the namespace and run the 'runthis' methods without me having to type it out for each individual class within the main class file. – user5426405 Feb 10 '18 at 20:17
  • The method i need to run is static – user5426405 Feb 10 '18 at 20:18
  • @user5426405, just use `using` directive at the beginning of your file. Like `using class1;` and methods from class1 would be available for you. Take a second and read about it [here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive) – Alex Feb 10 '18 at 20:23
  • 1
    The word you are looking for is "reflection" :) https://stackoverflow.com/questions/79693/getting-all-types-in-a-namespace-via-reflection – Miklós Tóth Feb 10 '18 at 20:27
  • If I understand your problem right: You want to itterate over all classes in a namespace and call a specific static function. This sounds like something reflection could do. But I have to wonder if your whole idea might be a case of the "XY Problem", where you somehow picked this is as your solution to a much easier problem: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Christopher Feb 10 '18 at 22:41

1 Answers1

1

First: please show us what you have accomplished till now

Second:You might consider to call the method by it's string name

Check the following answer on stackoverflow :

https://stackoverflow.com/a/540075/7120278

If you use the example i mentioned you can create any set of objects and call the method

Ahmad.Tr
  • 756
  • 7
  • 22