-2

Suppose there's a method which returns enum. But it returns only a subset of all the values of the enumeration. Can I find out programmatically which values are obtainable?

Example. I have an enum, which describes color with 100 values. Method GetCurrentTrafficLightsState can return only 3 colors of 100. I want to pass the method GetCurrentTrafficLightsState into some other method and get 3 colors as response.

EzLo
  • 13,780
  • 10
  • 33
  • 38
AlexAlum
  • 347
  • 1
  • 9
  • 19
  • 1
    Give it all possible inputs? – CompuChip Jun 20 '18 at 11:30
  • Unclear what you are asking – TheGeneral Jun 20 '18 at 11:30
  • 1
    Write a unit test for each possible input? – Camilo Terevinto Jun 20 '18 at 11:30
  • Reverse your method (if possible), than return value should be input and if some values aren't obtainable, it should lead to some error, in that case throw exception, than easily you can loop through enum and check for exceptions. – Michał Turczyn Jun 20 '18 at 11:32
  • Suppose there's a method which returns the result of an API call to an unknown API, can we find out which values are obtainable? – ProgrammingLlama Jun 20 '18 at 11:32
  • It cannot be resolved with a unit test cause the set of inputs is infinite, like string parameter. But the set of return values is finite - it is a subset of an enum. – AlexAlum Jun 20 '18 at 11:33
  • What do you mean by "obtainable"? You just want to see all the enum values? Take a look at the enum! – DavidG Jun 20 '18 at 11:34
  • Suppose the method changes, but its return type does not. Of what value is the information you've obtained then? The possible return values are all possible values of the return type. What is the actual problem you're trying to solve here? – David Jun 20 '18 at 11:35
  • Short of using static analysis on the method's IL to find out what values it uses, no. And even that assumes the method loads constants, and doesn't do nasty stuff like compute the number of steps it takes for a particular Turing machine to halt and return that, when casted to an enum. (Or more prosaically, calls a virtual method to cough up the result.) In its most general form, your problem is undecidable, as in, there exists no algorithm that can do this for the general case. – Jeroen Mostert Jun 20 '18 at 11:37
  • Example. I have an enum, which describes color with 100 values. Method GetCurrentTrafficLightsState can return only 3 colors of 100. I want to pass the method GetCurrentTrafficLightsState into some other method and get 3 colors as response. – AlexAlum Jun 20 '18 at 11:37
  • 1
    if you mean to write some code like using Reflection ... well that is not possible – A Khudairy Jun 20 '18 at 11:38
  • You could profile your code with a line-by-line option. So you can see which `code blocks` are present in a function. Every code block represents a possible outcome (exit path). – Neijwiert Jun 20 '18 at 11:39
  • 1
    For now, I see only one solution: parse C# source code and find 'return' keywords. – AlexAlum Jun 20 '18 at 11:39
  • If you have the source code, you could use the Roslyn compiler to do static analysis on the method and extract all uses of that particular enum. https://www.google.nl/search?q=roslyn+find+usages – CompuChip Jun 20 '18 at 11:46
  • I think this is a valid question with a valid answer - it seems that this problem can be reduced to the halting problem, at least to me. I do not think it deserves to be closed. I am not familiar enough with CS theory to answer the question myself, though. – Sander Jun 20 '18 at 11:47
  • 1
    @Aluminium: Your "traffic light" example implies that your modeling is wrong. The type being returned by `GetTrafficLightState` should have only the possible states of a traffic light. Not any color. (Also note that traffic lights are more complex than just three mutually exclusive colors.) You're approaching the problem from the wrong angle. You don't write code to examine the method and determine if it's right, you mix your modeling to guarantee that it's right. – David Jun 20 '18 at 11:51

1 Answers1

1

No there isn't. You cannot even determine if it will return at all, see https://en.wikipedia.org/wiki/Halting_problem