自定义无法运行的地方

时间:2018-07-07 10:20:53

标签: c# .net linq delegates

class Program
{
    private delegate Boolean SomeDelegate(string value);
    static void Main(string[] args)
    {
        var data = new List<string>() { "bill", "david", "john", "daviddd" };

        SomeDelegate AA = A;

        var test2 = data.DoWhere(AA); //This Line Compile is wrong
    }

    public static bool A(string value)
    {
        if (value.StartsWith("d"))
        {
            return true;
        }
        return false;
    } 
}

public static class CustomClass
{
    public static IEnumerable<T> DoWhere<T>(this IEnumerable<T> source, Func<T, Boolean> predicate)
    {            
        foreach (T item in source) 
        {
            if (predicate.Invoke(item))
            {
                yield return item;
            }
        }
    }
}

我想自定义方法并调整所需的数据。但是这段代码的编译在var test2 = data.DoWhere(AA);

中是错误的
  

无法从“ SomeDelegate”转换为'System.Func<string, bool>'

我不知道如何解决它。请查看我的代码。

3 个答案:

答案 0 :(得分:1)

您不能将SomeDelegate的实例强制转换为Func<string, bool>

var test2 = data.DoWhere(AA); //This Line Compile is wrong

尝试以下方法:

var test2 = data.DoWhere(c => AA(c)); 

或使用Invoke

var test2 = data.DoWhere(AA.Invoke);

或使用具有相同签名的方法,例如A

var test2 = data.DoWhere(A);

答案 1 :(得分:1)

SomeDelegate(AA)不是Func<string, bool>。它应该只是代码中的DoWhere(A)。即:

class Program
{
    static void Main(string[] args)
    {
        var data = new List<string>() { "bill", "david", "john", "daviddd" };      
        var test2 = data.DoWhere(A); //This Line Compile is wrong
    }

    public static bool A(string value)
    {
        if (value.StartsWith("d"))
        {
            return true;
        }
        return false;
    } 
}

一种更简单的编写方式:

class Program
{
    static void Main(string[] args)
    {
        var data = new List<string>() { "bill", "david", "john", "daviddd" };
        Func<string, bool> A = value => value.StartsWith("d"); 

        var test2 = data.DoWhere(A);       
    }
}

注意:这样的函数应该检查该值是否为null。

答案 2 :(得分:0)

您在评论中说

  

我只想自定义自己的条件方法

因此,与编写的代码相比,代码可以容易得多

JsonArrayRequest request = new JsonArrayRequest (Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(JSONArray response) {
            if (!response.equals(null)) {

                Log.e("Your Array Response", response);
            } else {
                Log.e("Your Array Response", "Data Null");
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error is ", "" + error);
        }
    }) {

        //This is for Headers If You Needed
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            //Map<String, String> params = new HashMap<String, String>();
           // params.put("Content-Type", "application/json; charset=UTF-8");
           // params.put("Authorization", "bearer  " + token);
            return params;
        }

        //Pass Your Parameters here
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            /*params.put("User", UserName);
            params.put("Pass", PassWord);*/
            return params;
        }
    };
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    queue.add(request);

对于您正在使用的一种简单测试,您也可以避免创建public class Program { public static void Main(string[] args) { var data = new List<string>() { "bill", "david", "john", "daviddd" }; var stringsStartingWithD = data.Where (s => StarstWithD(s)).ToList(); var anotherOne = data.Where (s => SomeOtherTest(s)).ToList(); } public static bool StarstWithD(string str) { if (str.StartsWith("d")) { return true; } return false; } public static bool SomeOtherTest(string str) { bool result = false; // apply desired logic and return true/false //... return result; } } 方法,而只需使用以下方法:

StartsWithD

您的整个代码现在只有两行