im trying to do an delegate that can take anything or return anything.Why simply run and understanding the limit. So far , the only way i know is by using the "object" key word everywhere and passing a list of onbject for parameter. To me it seem pretty unsafe and not realy easy to use.
exemple :
delegate object doAnyFunction(List<object> inputs);
public static object writeMessages(List<object> inputs)
{
Console.WriteLine("hi " + inputs[0] + " i K=know u have " + inputs[1]+ " year old");
return null;
}
static void Main(string[] args)
{
string name = "tomas";
int age = 26;
doAnyFunction doAny = new doAnyFunction(writeMessages);
doAny(new List<object> {name,age});
Console.Read();
}
console output result : hi tomas i know u have 26 year old
So eveything work for shure. Any have an idia on how i can improve this ? more safe more easy to use .... etc
thanks! feel free to debate!
Update:22/07/2015
Good I finally try all possibility and I finally come to a really simple result! Thanks to Mike Hixson and his idea.... he put me on the right track.
So the big thing about action and func<> is you can’t put them as a parameter in a function. So le just show an example real quick....
Public delegate object DoAnything();
Class Program
{
Static int additioner(int num1 , int num2)
{
return num1 + num2;
}
static object dostuff(DoAnything doit)
{
return doit();
}
static object writeMessage(string message)
{
Console.WriteLine(message);
return true;
}
static void Main(string[] args)
{
DoAnything del1 = delegate() { return additioner(1, 4); };
DoAnything del2 = delegate() { return writeMessage("hi it Work! haha"); };
Console.WriteLine(dostuff(del1));
del2();
Console.Read();
}
Console return :
5 Hi it Work! haha
So all result is what expected.
Has u can see i can use the return of the function i use with my delegate and use a function with any number or type of parameter. Goal achieved! The only little thing that is not perfect is ... with this way i have no choice of using a function with a return.... but a little return true on a function never heart anyway.
If any have a better idea or an improvement just say it i will be glad to test it.