简体   繁体   English

将参数传递给方法并分配给 Func 类型<bool></bool>

[英]Passing parameter to a method and assigning to type Func<bool>

I currently have a method like that resembles this我目前有一个类似的方法

 public static bool test(string str);

I would like to assign this method to this type我想将此方法分配给这种类型

 Func<bool> callback

I am trying to do this (which is incorrect)我正在尝试这样做(这是不正确的)

callback = test("Str");

The above is incorrect as C# things i am calling a method.以上是不正确的,因为 C# 我正在调用一个方法。 How can I tell it to call that method with the parameter Str?如何告诉它使用参数 Str 调用该方法? in C++ we can do this在 C++ 我们可以做到这一点

std::function<...,..,"str">

How do we do something similar in C#?我们如何在 C# 中做类似的事情?

If your goal is to invoke the callback always using the same string argument (and not a different argument each time), you can declare it like:如果您的目标是始终使用相同的字符串参数(而不是每次都使用不同的参数)调用回调,您可以将其声明为:

Func<bool> callback = () => test("Str");
var result = callback();

But if you are intending to pass a different string value each time, then you need a Func<string, bool> :但是,如果您打算每次传递不同的字符串值,那么您需要一个Func<string, bool>

Func<string, bool> callback = s => test(s);
var result = callback("Str");

you should declare the function as:您应该将 function 声明为:

Func<string, bool> callback

to indicate the method it references consumes a string and returns a bool .指示它引用的方法使用一个string并返回一个bool

then at a later point you can do:然后稍后您可以执行以下操作:

callback = s => test(s);

or inline:或内联:

Func<string, bool> callback = s => test(s);

Let me see if I know what you're getting at.让我看看我是否知道你在说什么。 In C++ we have 'function pointers' and they are declared with a particular signature.在 C++ 中,我们有“函数指针”,它们用特定的签名声明。 To make the equivalent of that in C# use the delegate keyword.要使 C# 中的等效项使用委托关键字。

Here's a quick functional code example:这是一个快速的功能代码示例:

    class DelegateExample
    {
        // A delegate is a prototype for a function signature.
        // Similar to a function pointer in C++.
        delegate bool MyDelegate(string[] strings);

        // A method that has that signature.
        bool ListStrings(string[] strings)
        {
            foreach (var item in strings)
            {
                Console.WriteLine(item);
            }
            return strings.Length > 0; // ... for example
        }

        // Different method, same signature.
        bool Join(string[] strings)
        {
            Console.WriteLine(string.Join(", ", strings));
            return strings.Length > 0; // ... for example
        }

        public void TryIt()
        {
            string[] testData = { "Apple", "Orange", "Grape" };

            // Think of this as a list of function pointers...
            List<MyDelegate> functions = new List<MyDelegate>();

            functions.Add(ListStrings); // This one points to the ListStrings method
            functions.Add(Join);        // This one points to the Join method

            foreach (var function in functions)
            {
                bool returnVal = function(testData);
                Console.WriteLine("The method returned " + returnVal + Environment.NewLine);
            }
        }
    }

You can run the sample in a console app like so:您可以像这样在控制台应用程序中运行示例:

class Program
{
    static void Main(string[] args)
    {
        new DelegateExample().TryIt();
        Console.ReadKey();
    }
}

Which gives this output:这给出了这个 output:

Apple
Orange
Grape
The method returned True

Apple, Orange, Grape
The method returned True

Hope this is helpful!希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传入func时出错 <type, bool> 作为异步方法的参数 - Error passing in func<type, bool> as a parameter to an async method 传递GetWhere查询(功能 <entityDTO,bool> )到需要(Func <entity,bool> )参数起作用 - Passing a GetWhere query (Func<entityDTO,bool>) to a data layer method which needs a (Func<entity,bool>) parameter to work 如何修改Expression的类型参数 <Func<???, bool> &gt;? - How to modify type parameter of Expression<Func<???, bool>>? 将参数传递给返回 Func 的方法 - Passing parameter to method returning Func 传递带有参数Func的方法 <T> 并获得TResult - Passing a method with parameter Func<T> and getting TResult 使用Func &lt;&gt;参数将Delegate对象传递给方法 - Passing Delegate object to method with Func<> parameter 是否可以更改Func的泛型类型参数 <T, bool> 代表? - Is it possible to change the generic type parameter of a Func<T, bool> delegate? 如何传递表达式<Func<T, bool> &gt; 谓词作为方法的参数? - How to pass Expression<Func<T, bool>> predicate as a parameter to a method? 如何使用Func类型的Expression作为参数调用方法 - How to call method with Expression of type Func as parameter 将条件传递给Func <bool> 一个元组 <string, string, Func<bool> &gt; - Passing a condition into Func<bool> of a Tuple<string, string, Func<bool>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM