Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将方法作为参数传递_C#_Asp.net - Fatal编程技术网

C# 将方法作为参数传递

C# 将方法作为参数传递,c#,asp.net,C#,Asp.net,如何将方法作为参数传递? 我一直在用Javascript执行此操作,并且需要使用匿名方法来传递参数。我怎么用c#做呢 看看C#代表 Tutorial您的paramethod是Func类型,因为它接受一个字符串参数并返回一个字符串(请注意,尖括号中的最后一项是返回类型) 因此,在本例中,您的代码将如下所示: protected void MyMethod(){ RunMethod(ParamMethod, "World"); } protected void RunMethod(Fun

如何将方法作为参数传递? 我一直在用Javascript执行此操作,并且需要使用匿名方法来传递参数。我怎么用c#做呢

看看C#代表


Tutorial

您的paramethod是Func类型,因为它接受一个字符串参数并返回一个字符串(请注意,尖括号中的最后一项是返回类型)

因此,在本例中,您的代码将如下所示:

protected void MyMethod(){
    RunMethod(ParamMethod, "World");
}

protected void RunMethod(Func<String,String> ArgMethod, String s){
    MessageBox.Show(ArgMethod(s));
}

protected String ParamMethod(String sWho){
    return "Hello " + sWho;
}
protectedvoid MyMethod(){
RunMethod(paramethod,World);
}
受保护的void运行方法(Func ArgMethod,字符串s){
MessageBox.Show(ArgMethod);
}
受保护的字符串参数方法(字符串sWho){
返回“Hello”+sWho;
}
提供此机制。对于您的示例,在C#3.0中实现这一点的快速方法是使用where
TResult
is
string
和lambdas

您的代码将变成:

protected void MyMethod(){
    RunMethod(() => ParamMethod("World"));
}

protected void RunMethod(Func<string> method){
    MessageBox.Show(method());
}

protected String ParamMethod(String sWho){
    return "Hello " + sWho;
}
受保护的字符串参数方法(字符串sWho)
{
返回“Hello”+sWho;
}
受保护的void运行方法(Func ArgMethod)
{
Show(ArgMethod());
}
受保护的void MyMethod()
{
RunMethod(()=>paramethod(“World”);
}

这很重要。它从paramethod的
Func
创建一个匿名
Func

这不会编译。RunMethod接受传递给它的Func,谢谢你的回答。我得到一个编译错误“…RunMethod(Func,String)有一些无效参数。您使用的是哪个版本的C#?非常好的链接,谢谢。这是其中之一:
protected delegate String MyDelegate(String str);

protected void MyMethod()
{
    MyDelegate del1 = new MyDelegate(ParamMethod);
    RunMethod(del1, "World");
}

protected void RunMethod(MyDelegate mydelegate, String s)
{
    MessageBox.Show(mydelegate(s) );
}

protected String ParamMethod(String sWho)
{
    return "Hello " + sWho;
}
// Declare a delegate for the method we're passing.
delegate string MyDelegateType();

protected void MyMethod(){
    RunMethod(delegate
    {
        return ParamMethod("World");
    });
}

protected void RunMethod(MyDelegateType method){
    MessageBox.Show(method());
}

protected String ParamMethod(String sWho){
    return "Hello " + sWho;
}
protected String ParamMethod(String sWho)
{
    return "Hello " + sWho;
}

protected void RunMethod(Func<string> ArgMethod)
{
    MessageBox.Show(ArgMethod());
}

protected void MyMethod()
{
    RunMethod( () => ParamMethod("World"));
}
protected delegate String MyDelegate(String str);

protected void MyMethod()
{
    MyDelegate del1 = new MyDelegate(ParamMethod);
    RunMethod(del1, "World");
}

protected void RunMethod(MyDelegate mydelegate, String s)
{
    MessageBox.Show(mydelegate(s) );
}

protected String ParamMethod(String sWho)
{
    return "Hello " + sWho;
}