当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - What is the best way to make a delegate that accept any return and any parameter?

问题描述:

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.

网友答案:

If you want a type-safe delegate that can take in a number of arguments you can use the Action delegate:

https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx

There are 16 implementations of this delegate that allow for 1 to 16 arguments to be specified.

If your delegate must return a value, then use the Func delegate:

https://msdn.microsoft.com/en-us/library/bb534960(v=vs.110).aspx

This one also has 16 implementations to allow for 1 to 16 arguments.

UPDATE

Here is an example. Setup 2 functions that can be used with Action<string, int>:

static void WriteMessageOriginal(string name, int age)
{
    Console.WriteLine("hi " + name + " i K=know u have " + age + " year old");
}

static void WriteMessageNew(string name, int age)
{
    Console.WriteLine("hi {0}, I know you are {1} years old", name, age);
}

Create a function that can take Action<string, int> and invoke it with a string and an integer.

static void DoMessageDelgate(Action<string, int> mydelegate)
{
    mydelegate("tomas", 26);
}

Call the method with different delegates.

static void Main()
{
    DoMessageDelgate(WriteMessageOriginal);
    DoMessageDelgate(WriteMessageNew);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: