-3

In C#, I have a set of function candidates that will run in the future: void func1(int); void func2(float); void func3(string);...and many more.

some code will decide which func123... will run (or none will run), and decide the parameter value}

if (ConditionMetToRun){run that function with parameter;}

How can I do this??


ok here's an example

void WearSunStuff(whichSunscreen, whichSunglasses);

void OpenUmbrella(whichUmbrella);

void WearCoat(whichCoat);

Then I look out the windows and see it's raining. I'd better open my umbrella. But I'm at home, I don't need to open my favorite black umbrella YET.

If(FriendingIsCallingMeToGoOutside)
{

    OpenUmbrella(blackUmbrella);
    GoOutSideToMeetMyFriend();
}

or..... Then I look out the windows and see it's really co. I'd better wear my coat. But I'm at home, I don't need to open my favorite yellow coat YET.

If(FriendingIsCallingMeToGoOutside)
{
WearCoat(yelloCoat);
GoOutSideToMeetMyFriend();
}
Community
  • 1
  • 1
  • So what is your question? – Dai Oct 04 '15 at 00:08
  • You want a background thread that polls a list of functions that are queued? – Martin Swanepoel Oct 04 '15 at 00:31
  • Seriously nobody knows what you are asking... Do you want them as thread, just overloaded functions or something completely different. Pls edit your answer to make it more clear. And maybe some sample code/usage and what you already have tried. – SkryptX Oct 04 '15 at 00:35
  • I don't need a queue, just one function will be called eventually. – JohnSquare ReRises Li Oct 04 '15 at 00:35
  • You say eventually, meaning you want to know how to run these on a schedule? Or you want to know how to invoke functions on your FutureCandidates object dynamically? – Martin Swanepoel Oct 04 '15 at 00:39
  • The question here is probably what you are looking for : http://stackoverflow.com/questions/32906332/early-binding-with-generics It is working code; I posted it for a different reason. You can just rewrite it in c# or use any free online convertor to convert it to c#. – Pradeep Kumar Oct 04 '15 at 00:49
  • how to invoke functions on your FutureCandidates functions dynamically – JohnSquare ReRises Li Oct 04 '15 at 00:50

1 Answers1

0
FutureCandidates futureCandidates = new FutureCandidates();

System.Reflection.MethodInfo func = futureCandidates.GetType().GetMethod("OpenUmbrella"); //OR WearCoat
if(FriendingIsCallingMeToGoOutside)
{
    func.Invoke(futureCandidates, new object[] { blackUmbrella });
    GoOutSideToMeetMyFriend();
}