I have a class
with a number of methods that return
same object
type. I want to all return
values of all methods from that class
. For example: I have class called Class1 with Method1, Method2 and MapActivity, each method1 and 2 has switch
case
returning certain enum values. I want to retrieve all that return
values from both methods.
public static class Class1
{
public static string MapActivity(Human human, Activity activity)
{
switch (human)
{
case Human.Me:
return Method1(activity);
case Human.Him:
return Method2(activity);
default:
return "error";
}
}
public enum Activity
{
Eat,
Sleep,
Game,
Drink,
Poop,
Slack,
}
private static string Method1(Activity myActivity)
{
switch (myActivity)
{
case Activity.Eat:
return "one";
case Activity.Sleep:
return "two";
case Activity.Drink:
return "three";
case Activity.Game:
return "four";
case Activity.Poop:
return "five";
case Activity.Eat:
return "six";
default:
return "error";
}
}
private static string Method2(Activity hisActivity)
{
switch (hisActivity)
{
case Activity.Eat:
return "seven";
case Activity.Sleep:
return "eight";
case Activity.Drink:
return "nine";
case Activity.Slack:
return "ten";
case Activity.Poop:
return "eleven";
case Activity.Game:
return "twelve";
default:
return "error";
}
}
}
public class Class2
{
public void CheckReturnValues()
{
//this is where i want to do my thing
//get all private methods from Class1
//get all return values from those methods
}
}
First of all, your code is not compilable - you are returning string instead of int and you have duplicate case values in the switch.
Secondly. You don't want to do that at all. The only way you can achieve this is through parsing the body of the method (and maybe also another parts of the code). But this works under certain conditons only - you have to use only local or static values, etc. You will never be able to get any dynamic value unless you'll compile and run the code.
You can solve your problem if you restrict the switch variable to some kind of finite enumeration. In this case, you are able to iterate through the enumeration, call the methods with each value of the enumeration and then you will get all possible return values. If this is your case, looping through the enumeration values can be made like this:
var values = Enum.GetValues(typeof(Foos));
or in typed version:
var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();
And then you just call the method inside the loop:
var results = new List<string>();
foreach (Foos val in Enum.GetValues(typeof(Foos)).Cast<Foos>())
{
results.Add(Class1.Method1(val));
}
The same goes for Method2.
After editing the question the code should be like this:
var myActivities = new List<string>();
var hisActivities = new List<string>();
foreach (Activity activity in Enum.GetValues(typeof(Activity)).Cast<Activity>())
{
myActivities.Add(Class1.Method1(activity));
hisActivities.Add(Class1.Method2(activity));
}
Then you will have two lists of my/his activities and you can check if the code is matching your database values like this:
bool myActivitiesHasError = myActivities.Any(x => x == "error")
bool hisActivitiesHasError = hisActivities.Any(x => x == "error")
Or you can check the value just inside the loop:
bool myActivitiesHasError = false;
bool hisActivitiesHasError = false;
foreach (Activity activity in Enum.GetValues(typeof(Activity)).Cast<Activity>())
{
if (myActivities.Add(Class1.Method1(activity)) == "error") myActivitiesHasError = true;
if (hisActivities.Add(Class1.Method2(activity)) == "error") hisActivitiesHasError = true;
}
To cover the whole question. If you want to access private methods of the class, then you have to use the reflection.
MethodInfo method1 = typeof(Class1).GetMethod("Method1", BindingFlags.NonPublic);
MethodInfo method2 = typeof(Class1).GetMethod("Method2", BindingFlags.NonPublic);
foreach (Activity activity in Enum.GetValues(typeof(Activity)).Cast<Activity>())
{
method1.Invoke(null, activity);
method2.Invoke(null, activity);
}
Just merge the last code (with the reflection) with the previous ones...it depends on what you want to do exactly with the output.
You should also use some kind of variable for the "error" value and then use it in the condition statements:
private static string ErrorValue = "error";