English 中文(简体)
如何使用委托作为参数来调用此函数
原标题:how would i call this function with a delegate as a parameter

我有一些重复的代码,我正试图将其重构为一个通用函数,以从对象列表(所有的INamed列表)中生成一个复选框列表。

第二个参数是一个委托,它将调用回一个函数,但我不知道如何实际调用这个方法。使用此委托调用方法的最佳方式是什么?(我正在寻找一个调用Checkboxlist函数的代码示例)

public delegate bool HasHandler(INamed named);

这是通用方法

static public string CheckboxList(IQueryable<INamed> allItems, HasHandler has, string name)
    {
        StringBuilder b = new StringBuilder();
        foreach (var item in allItems)
        {
            if (has(item))
            {
                b.Append("<input type= checkbox  class= checkboxes  name= " + name + "  value=" + item.Id + " checked />" + item.Name);
            }
            else
            {
                b.Append("<input type= checkbox  class= checkboxes  name= " + name + "  value=" + item.Id + " />" + item.Name);
            }
        }
        return b.ToString();
    }
最佳回答

您现在正在执行:

if (has(item))  // This calls the delegate

调用方法中的委托。您的语法是正确的,应该可以使用。


至于调用<code>CheckboxList</code>-听起来您需要定义委托。这可以是任何以“INamed”为参数并返回布尔值的方法。例如,如果您有:

private bool myHandler(INamed named)
{
    return true;
}

你可以这样称呼:

string result = CheckboxList(items, myHandler, "Foo");

或者,您可以在此处传递lambda:

string result = CheckboxList(items, named => { return (named.Foo > 3); }, "Foo");
问题回答
CheckBoxList(yourItems, x => x.SomeProperty == "foo", "yourName");

非常简单的例子:

public delegate bool HasHandler(INamed named);

// delete method matching HasHandler declaration
bool MyHandler(INamed named) {
    return true;
}

// method that passes your implemented delegate method as a parameter
void MyOtherMethod() {
    MyMethod(null, (n) => MyHandler(n)); // using lambda
    MyMethod(null, MyHandler); // not using lambda
}

// method that uses your implemented delegate method
// this would be like your CheckboxList method
void MyMethod(INamed o, HasHandler handler) {
    handler(o);
}

请注意,标识符处理程序被用作函数,其中oINamed的对象)作为参数。

编辑

调用CheckboxList方法的示例:

CheckboxList(myItems, (n) => MyHandler(n), "myName");




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签