C#内置泛型委托之Func委托

一、什么是Func委托

Func委托代表有返回类型的委托

二、Func委托定义

查看Func的定义:

using System.Runtime.CompilerServices;

namespace System
{
    //
    // 摘要:
    //     封装一个方法,该方法具有两个参数,并返回由 TResult 参数指定的类型的值。
    //
    // 参数:
    //   arg1:
    //     此委托封装的方法的第一个参数。
    //
    //   arg2:
    //     此委托封装的方法的第二个参数。
    //
    // 类型参数:
    //   T1:
    //     此委托封装的方法的第一个参数的类型。
    //
    //   T2:
    //     此委托封装的方法的第二个参数的类型。
    //
    //   TResult:
    //     此委托封装的方法的返回值类型。
    //
    // 返回结果:
    //     此委托封装的方法的返回值。
    [TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")]
    public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
}

你会发现,Func其实就是有多个输出参数并且有返回值的delegate。

3、示例

Func至少0个输入参数,至多16个输入参数,根据返回值泛型返回。必须有返回值,不可void。

Func<int> 表示没有输入参参,返回值为int类型的委托。

Func<object,string,int> 表示传入参数为object, string ,返回值为int类型的委托。

Func<object,string,int> 表示传入参数为object, string, 返回值为int类型的委托。

Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型),返回值为int类型的委托。

代码示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 无参数,只要返回值
            Func<int> fun1 = new Func<int>(FunWithNoPara);
            int result1= fun1();
            Console.WriteLine(result1);
            Console.WriteLine("----------------------------");
            Func<int> fun2 = delegate { return 19; };
            int result2 = fun2();
            Console.WriteLine(result2);
            Console.WriteLine("----------------------------");
            Func<int> fun3 = () => { return 3; };
            int result3 = fun3();
            Console.WriteLine(result3);
            Console.WriteLine("----------------------------");
            //有一个参数,一个返回值
            Func<int, int> fun4 = new Func<int, int>(FunWithPara);
            int result4 = fun4(4);
            Console.WriteLine($"这里是一个参数一个返回值的方法,返回值是:{result4}");
            Console.WriteLine("----------------------------");
            // 使用委托
            Func<int, string> fun5 = delegate (int i) { return i.ToString(); };
            string result5 = fun5(5);
            Console.WriteLine($"这里是一个参数一个返回值的委托,返回值是:{result5}");
            Console.WriteLine("----------------------------");
            // 使用匿名委托
            Func<int, string> fun6 = (int i) =>
            {
                return i.ToString();
            };
            string result6 = fun6(6);
            Console.WriteLine($"这里是一个参数一个返回值的匿名委托,返回值是:{result6}");
            Console.WriteLine("----------------------------");
            // 多个输入参数
            Func<int, string, bool> fun7 = new Func<int, string, bool>(FunWithMultiPara);
            bool result7 = fun7(2, "2");
            Console.WriteLine($"这里是有多个输入参数的方法,返回值是:{result7}");
            Console.WriteLine("----------------------------");
            // 使用委托
            Func<int, string, bool> fun8 = delegate (int i, string s)
            {
                return i.ToString().Equals(s) ? true : false;
            };
            bool result8 = fun8(2, "abc");
            Console.WriteLine($"这里是有多个输入参数的委托,返回值是:{result8}");
            Console.WriteLine("----------------------------");
            // 使用匿名委托
            Func<int, string, bool> fun9 = (int i, string s) =>
            {
                return i.ToString().Equals(s) ? true : false;
            };
            bool result9 = fun9(45, "ert");
            Console.WriteLine($"这里是有多个输入参数的匿名委托,返回值是:{result9}");
            Console.ReadKey();

        }

        static int FunWithNoPara()
        {
            return 10;
        }

        static int FunWithPara(int i)
        {
            return i;
        }

        static bool FunWithMultiPara(int i,string s)
        {
            return i.ToString().Equals(s) ? true : false;
        }
    }
}

运行结果:

4、真实示例

在下面的示例中,利用Func委托封装数据库通用访问类。

1、定义BaseModel基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication.Model
{
    public class BaseModel
    {
        public int Id { get; set; }
    }
}

2、定义Student类继承自BaseModel基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication.Model
{
    public class Student : BaseModel
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public int Sex { get; set; }

        public string Email { get; set; }
    }
}

3、定义数据库访问方法接口

using FunApplication.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication.IDAL
{
    public interface IBaseDAL
    {
        T Query<T>(int id) where T : BaseModel;

        List<T> QueryAll<T>() where T : BaseModel;

        int Insert<T>(T t) where T : BaseModel;

        int Update<T>(T t) where T : BaseModel;

        int Delete<T>(int id) where T : BaseModel;
    }
}

4、定义属性帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication.AttributeExtend
{
    public static class AttributeHelper
    {
        public static string GetColumnName(this PropertyInfo prop)
        {
            if (prop.IsDefined(typeof(ColumnAttribute), true))
            {
                ColumnAttribute attribute = (ColumnAttribute)prop.GetCustomAttribute(typeof(ColumnAttribute), true);
                return attribute.GetColumnName();
            }
            else
            {
                return prop.Name;
            }
        }
    }
}

5、定义ColumnAttribute类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication.AttributeExtend
{
    [AttributeUsage(AttributeTargets.Property)]
    public class ColumnAttribute : Attribute
    {
        public ColumnAttribute(string name)
        {
            this._Name = name;
        }

        private string _Name = null;
        public string GetColumnName()
        {
            return this._Name;
        }
    }
}

6、定义数据库方法接口实现类

using FunApplication.IDAL;
using FunApplication.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using FunApplication.AttributeExtend;

namespace FunApplication.DAL
{
    public  class BaseDAL : IBaseDAL
    {
        // 数据库链接字符串
        private static string strConn = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
        public  int Delete<T>(int id) where T : BaseModel
        {
            int result = 0;

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                string strSQL = "delete from Student where Id=@Id";
                SqlParameter para = new SqlParameter("Id", id);
                SqlCommand command = new SqlCommand(strSQL, conn);
                command.Parameters.Add(para);
                conn.Open();
                result = command.ExecuteNonQuery();
            }
            return result;
        }

        public int Insert<T>(T t) where T : BaseModel
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                Type type = typeof(T);
                var propArray = type.GetProperties().Where(p => p.Name != "Id");
                string strSQL = "insert into Student Values (@Name,@Age,@Sex,@Email) ";
                SqlCommand command = new SqlCommand(strSQL, conn);
                var parameters = propArray.Select(p => new SqlParameter($"@{p.GetColumnName()}", p.GetValue(t) ?? DBNull.Value)).ToArray();
                command.Parameters.AddRange(parameters);
                conn.Open();
                result = command.ExecuteNonQuery();
            }
                return result;
        }

        public T Query<T>(int id) where T : BaseModel
        {
            Type type = typeof(T);
            string columnString = string.Join(",", type.GetProperties().Select(p => $"[{p.GetColumnName()}]"));
            string sql = $"SELECT {columnString} FROM [{type.Name}] WHERE Id={id}";
            T t = null;// (T)Activator.CreateInstance(type);

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                SqlCommand command = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                List<T> list = this.ReaderToList<T>(reader);
                t = list.FirstOrDefault();
            }
            return t;
        }

        public List<T> QueryAll<T>() where T : BaseModel
        {
            Type type = typeof(T);
            string columnString = string.Join(",", type.GetProperties().Select(p => $"[{p.GetColumnName()}]"));
            string sql = $"SELECT {columnString} FROM [{type.Name}] ";
            List<T> list = new List<T>();
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                SqlCommand command = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                list = this.ReaderToList<T>(reader);
            }
            return list;
        }

        public int Update<T>(T t) where T : BaseModel
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                Type type = typeof(T);
                var propArray = type.GetProperties().Where(p => p.Name != "Id");
                string columnString = string.Join(",", propArray.Select(p => $"[{p.GetColumnName()}]=@{p.GetColumnName()}"));
                var parameters = propArray.Select(p => new SqlParameter($"@{p.GetColumnName()}", p.GetValue(t) ?? DBNull.Value)).ToArray();
                //必须参数化  否则引号?  或者值里面还有引号
                string strSQL = $"UPDATE [{type.Name}] SET {columnString} WHERE Id={t.Id}";
                SqlCommand command = new SqlCommand(strSQL, conn);
                command.Parameters.AddRange(parameters);
                conn.Open();
                result = command.ExecuteNonQuery();
            }
            return result;
        }

        private List<T> ReaderToList<T>(SqlDataReader reader) where T : BaseModel
        {
            Type type = typeof(T);
            List<T> list = new List<T>();
            while (reader.Read())//表示有数据  开始读
            {
                T t = (T)Activator.CreateInstance(type);
                foreach (var prop in type.GetProperties())
                {
                    object oValue = reader[prop.GetColumnName()];
                    if (oValue is DBNull)
                        oValue = null;
                    prop.SetValue(t, oValue);//除了guid和枚举
                }
                list.Add(t);
            }
            return list;
        }
    }
}

7、在Main()方法中调用

using FunApplication.DAL;
using FunApplication.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            #region MyRegion
            BaseDAL dal = new BaseDAL();
            // 查询
            Student student = dal.Query<Student>(2);
            Console.WriteLine($"姓名:{student.Name},年龄:{student.Age},Email地址:{student.Email}");
            Console.WriteLine("----------------------------");
            // 查询所有
            List<Student> list = dal.QueryAll<Student>();
            Console.WriteLine($"集合个数:{list.Count}");
            Console.WriteLine("----------------------------");
            // 插入
            Student studentIns = new Student()
            {
                Name = "小明",
                Age = 20,
                Sex = 2,
                Email = "xiaoming@qq.com"
            };
            bool resultIns = dal.Insert<Student>(studentIns) > 0 ? true : false;
            Console.WriteLine($"插入执行结果:{resultIns}");
            Console.WriteLine("----------------------------");
            // 更新
            Student studentUpd = new Student()
            {
                Id = 1,
                Name = "zhangsan1234",
                Age = 20,
                Sex = 2,
                Email = "zhangsan1234@qq.com"
            };
            bool resultUpd = dal.Update<Student>(studentUpd) > 0 ? true : false;
            Console.WriteLine($"更新执行结果:{resultUpd}");
            Console.WriteLine("----------------------------");
            // 删除
            bool resultDel = dal.Delete<Student>(3) > 0 ? true : false;
            Console.WriteLine($"删除执行结果:{resultDel}");
            #endregion
            Console.ReadKey();
        }
    }
}

8、结果

9、优化

仔细观察上面步骤7中的代码,你会发现在每个方法中都有重复的代码,打开链接,执行SqlCommand命令,那么这些重复的代码能不能提取到一个公共的方法中进行调用呢?答案是可以的,那就是利用Func委托,看下面优化后的代码:

using FunApplication.AttributeExtend;
using FunApplication.IDAL;
using FunApplication.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication.DAL
{
    public class FunBaseDAL : IBaseDAL
    {
        // 数据库链接字符串
        private static string strConn = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;

        public int Delete<T>(int id) where T : BaseModel
        {
            Type type = typeof(T);
            string sql = $"delete from {type.Name} where Id=@Id";
            Func<SqlCommand, int> func = (SqlCommand command) =>
            {
                SqlParameter para = new SqlParameter("Id", id);
                command.Parameters.Add(para);
                return command.ExecuteNonQuery();
            };

            return ExcuteSql<int>(sql, func);
        }

        public int Insert<T>(T t) where T : BaseModel
        {
            int result = 0;
            Type type = typeof(T);
            var propArray = type.GetProperties().Where(p => p.Name != "Id");
            string strSQL = "insert into Student Values (@Name,@Age,@Sex,@Email) ";
            var parameters = propArray.Select(p => new SqlParameter($"@{p.GetColumnName()}", p.GetValue(t) ?? DBNull.Value)).ToArray();
            Func<SqlCommand, int> func = (SqlCommand command) =>
            {
                command.Parameters.AddRange(parameters);
                return command.ExecuteNonQuery();
            };
            result = ExcuteSql<int>(strSQL, func);
            return result;
        }

        public T Query<T>(int id) where T : BaseModel
        {
            Type type = typeof(T);
            string columnString = string.Join(",", type.GetProperties().Select(p => $"[{p.GetColumnName()}]"));
            string sql = $"SELECT {columnString} FROM [{type.Name}] WHERE Id=@Id";
            T t = null;
            DataTable dt = new DataTable();

            Func<SqlCommand, T> func = (SqlCommand command) =>
            {
                SqlParameter para = new SqlParameter("@Id", id);
                command.Parameters.Add(para);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                //SqlDataReader reader = command.ExecuteReader();
                //List<T> list = this.ReaderToList<T>(reader);
                adapter.Fill(dt);
                List<T> list = ConvertToList<T>(dt);
                T tResult = list.FirstOrDefault();
                return tResult;
            };
            t = ExcuteSql<T>(sql, func);
            return t;
        }

        public List<T> QueryAll<T>() where T : BaseModel
        {
            Type type = typeof(T);
            string columnString = string.Join(",", type.GetProperties().Select(p => $"[{p.GetColumnName()}]"));
            string sql = $"SELECT {columnString} FROM [{type.Name}] ";
            T t = null;

            Func<SqlCommand, List<T>> func = (SqlCommand command) =>
            {
                SqlDataReader reader = command.ExecuteReader();
                List<T> list = this.ReaderToList<T>(reader);
                return list;
            };
            return ExcuteSql<List<T>>(sql, func);
        }

        public int Update<T>(T t) where T : BaseModel
        {
            int result = 0;
            Type type = typeof(T);
            var propArray = type.GetProperties().Where(p => p.Name != "Id");
            string columnString = string.Join(",", propArray.Select(p => $"[{p.GetColumnName()}]=@{p.GetColumnName()}"));
            var parameters = propArray.Select(p => new SqlParameter($"@{p.GetColumnName()}", p.GetValue(t) ?? DBNull.Value)).ToArray();
            //必须参数化  否则引号?  或者值里面还有引号
            string strSQL = $"UPDATE [{type.Name}] SET {columnString} WHERE Id={t.Id}";
            Func<SqlCommand, int> func = (SqlCommand command) =>
            {
                command.Parameters.AddRange(parameters);
                return command.ExecuteNonQuery();
            };
            result = ExcuteSql<int>(strSQL, func);
            return result;
        }

        //多个方法里面重复对数据库的访问  想通过委托解耦,去掉重复代码
        private T ExcuteSql<T>(string sql, Func<SqlCommand, T> func)
        {
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                using (SqlCommand command = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    SqlTransaction sqlTransaction = conn.BeginTransaction();
                    try
                    {
                        command.Transaction = sqlTransaction;
                        T tResult = func.Invoke(command);
                        sqlTransaction.Commit();
                        return tResult;
                    }
                    catch (Exception ex)
                    {
                        sqlTransaction.Rollback();
                        throw;
                    }
                }
            }
        }

        private List<T> ReaderToList<T>(SqlDataReader reader) where T : BaseModel
        {
            Type type = typeof(T);
            List<T> list = new List<T>();
            while (reader.Read())//表示有数据  开始读
            {
                T t = (T)Activator.CreateInstance(type);
                foreach (var prop in type.GetProperties())
                {
                    object oValue = reader[prop.GetColumnName()];
                    if (oValue is DBNull)
                        oValue = null;
                    prop.SetValue(t, oValue);//除了guid和枚举
                }
                list.Add(t);
            }
            reader.Close();
            return list;
        }
    }
}

10、在Main()方法中调用

using FunApplication.DAL;
using FunApplication.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 传统实现
            //BaseDAL dal = new BaseDAL();
            //// 查询
            //Student student = dal.Query<Student>(2);
            //Console.WriteLine($"姓名:{student.Name},年龄:{student.Age},Email地址:{student.Email}");
            //Console.WriteLine("----------------------------");
            //// 查询所有
            //List<Student> list = dal.QueryAll<Student>();
            //Console.WriteLine($"集合个数:{list.Count}");
            //Console.WriteLine("----------------------------");
            //// 插入
            //Student studentIns = new Student()
            //{
            //    Name = "小明",
            //    Age = 20,
            //    Sex = 2,
            //    Email = "xiaoming@qq.com"
            //};
            //bool resultIns = dal.Insert<Student>(studentIns) > 0 ? true : false;
            //Console.WriteLine($"插入执行结果:{resultIns}");
            //Console.WriteLine("----------------------------");
            //// 更新
            //Student studentUpd = new Student()
            //{
            //    Id = 1,
            //    Name = "zhangsan1234",
            //    Age = 20,
            //    Sex = 2,
            //    Email = "zhangsan1234@qq.com"
            //};
            //bool resultUpd = dal.Update<Student>(studentUpd) > 1 ? true : false;
            //Console.WriteLine($"更新执行结果:{resultUpd}");
            //Console.WriteLine("----------------------------");
            //// 删除
            //bool resultDel = dal.Delete<Student>(5) > 1 ? true : false;
            //Console.WriteLine($"删除执行结果:{resultDel}");
            #endregion

            #region 利用委托
            // 查询
            FunBaseDAL dal = new FunBaseDAL();
            Student student = dal.Query<Student>(1);
            Console.WriteLine($"姓名:{student.Name},年龄:{student.Age},Email地址:{student.Email}");
            Console.WriteLine("----------------------------");
            // 查询所有
            List<Student> list = dal.QueryAll<Student>();
            Console.WriteLine($"集合个数:{list.Count}");
            Console.WriteLine("----------------------------");
            // 插入
            Student studentIns = new Student()
            {
                Name = "tom",
                Age = 19,
                Sex = 1,
                Email = "tom@163.com"
            };
            bool resultIns = dal.Insert<Student>(studentIns) > 0 ? true : false;
            Console.WriteLine($"插入执行结果:{resultIns}");
            Console.WriteLine("----------------------------");
            List<Student> list1 = dal.QueryAll<Student>();
            Console.WriteLine($"插入后集合个数:{list1.Count}");
            Console.WriteLine("----------------------------");
            // 更新
            Student studentUpd = new Student()
            {
                Id = 2,
                Name = "马六123",
                Age = 20,
                Sex = 2,
                Email = "maliu1234@qq.com"
            };
            bool resultUpd = dal.Update<Student>(studentUpd) > 0 ? true : false;
            Console.WriteLine($"更新执行结果:{resultUpd}");
            Console.WriteLine("----------------------------");
            // 删除
            bool resultDel = dal.Delete<Student>(8) > 0 ? true : false;
            Console.WriteLine($"删除执行结果:{resultDel}");
            List<Student> list2 = dal.QueryAll<Student>();
            Console.WriteLine($"删除后集合个数:{list2.Count}");
            Console.WriteLine("----------------------------");
            #endregion
            Console.ReadKey();
        }
    }
}

11、结果

注意

在使用SqlDataReader的时候有时会报错:“已有打开的与此Command相关联的DataReader,必须先将它关闭”。

同时打开两个或循环多个sqldatareader会出现以上错误。因为用的是sqldatareader做数据库的数据读取,sqlconnection开启没有关闭。

一个SqlConnection只能执行一次事务,没用一次必须关闭然后再开启。上面我只用了一次没有关闭,直接开启所以会报错。解决方案有如下两种:

1、其实不用多次打开在开启,那样实现起来很麻烦。直接在连接字符串的后面加上MultipleActiveResultSets=true即可。 配置文件定义如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <!--<add name="DbConnection" connectionString="Server=.;Initial Catalog=MyDb;User ID=sa;Password=123456;MultipleActiveResultSets=True"/>-->
    <!--配置文件里面添加MultipleActiveResultSets=True-->
    <add name="DbConnection" connectionString="Server=.;Initial Catalog=MyDb;User ID=sa;Password=123456;MultipleActiveResultSets=True"/>
  </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

2、使用DataTable

在上面是使用的SqlDataReader读取数据,然后转换成List<T>,可以用DataTable代替SqlDataReader,这样就不会报错了,代码如下:

/// <summary>
/// 将DataTable转换成List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
private List<T> ConvertToList<T>(DataTable dt) where T:BaseModel
{
      Type type = typeof(T);
      List<T> list = new List<T>();
      foreach(DataRow dr in dt.Rows)
      {
          T t = (T)Activator.CreateInstance(type);
          foreach(PropertyInfo prop in type.GetProperties())
          {
               object value = dr[prop.GetColumnName()];
               if(value is DBNull)
               {
                    value = null;
               }
               prop.SetValue(t, value);
          }
          list.Add(t);
        }
        return list;
}

到此这篇关于C#内置泛型委托之Func委托的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#中Action和Func的区别

    本文实例分析了C#中Action和Func的区别,有助于读者牢固掌握并对其准确使用.具体分析如下: 先来看下面这段代码: //测试使用的公共值 int num = 10; //测试Func委托 Func<int, int> f; f = (int tempf) => { return tempf + 1; }; Response.Write(f(num).ToString()+"<br />"); //调用f委托,并打印相应的值! //测试Action委托

  • C#关于Func和Action委托的介绍详解

    目录 1.Action委托 2.Func委托 委托:委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托(Delegate)特别用于实现事件和回调方法.所有的委托(Delegate)都派生自 System.Delegate 类. 1.Action委托 没有返回值,可以不包含参数 (1)声明委托.定位委托事件.向委托添加事件.执行委托 /// <summary> /// 不包含参数 /// </summary> private event

  • C#中的Action、Func和Predicate如何使用

    前言 委托是一个类型安全的函数指针,它可以引用与委托具有相同签名的方法.委托常用于实现回调方法或者事件机制,在C#中一般用 "delegate" 关键字声明.你可以声明一个和类平级的委托,也可以嵌套在类中. Func 和 Action 是什么,如何使用? 两者最基本的区别是,前者适合那些需要带返回值的委托,后者适合那些不带返回值的委托. Func 所引用的方法接收一个或者多个入参并带有一个返回值,Action所引用的方法接收一个或者多个参数并且没有返回值,换句话说,你的委托所引用的方法

  • Asp.Net中的Action和Func委托实现

    前言 最近在阅读某开源框架源码的时候,发现作者在其中运用了很多 Action委托 和 Func委托 ,虽然我之前在项目中也有一些对委托的实操,但还是免不了长时间的不用,当初消化的一些委托基础都遗忘了...索性,趁热打铁,借助这次分享的机会,也帮自己重新巩固下.Net中关于委托的一些基础用法. 直奔主题 从.Net Framework1.0开始就为我们提供了委托的功能使用.那个时候.Net内置委托Action和Func还没有问世,那么,我们先来看看1.0版本时候的委托.委托从字面上来理解就是"帮别

  • C#内置泛型委托之Action委托

    1.什么是Action泛型委托 Action<T>是.NET Framework内置的泛型委托,可以使用Action<T>委托以参数形式传递方法,而不用显示声明自定义的委托.封装的方法必须与此委托定义的方法签名相对应.也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能有返回值. 2.Action委托定义 查看Action的定义: using System.Runtime.CompilerServices; namespace System { // // 摘要: //

  • 使用.NET中的Action及Func泛型委托深入剖析

    委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调机制的实现基本上依赖于委托.C#的delegate关键字用于声明委托,它具有将声明委托类型映射到System.Delegate类的能力,System.Delegate类位于mscorlib.dll中,是.NET的基础核心类之一.使用delegate关键字声明一个委托,实质上创建了System.Delegate的

  • C#内置泛型委托之Func委托

    一.什么是Func委托 Func委托代表有返回类型的委托 二.Func委托定义 查看Func的定义: using System.Runtime.CompilerServices; namespace System { // // 摘要: // 封装一个方法,该方法具有两个参数,并返回由 TResult 参数指定的类型的值. // // 参数: // arg1: // 此委托封装的方法的第一个参数. // // arg2: // 此委托封装的方法的第二个参数. // // 类型参数: // T1:

  • C#委托方法Func()中GetInvocationList()方法的用法介绍

    在日常使用委托时,有以下常用方法 方法名称 说明  Clone   创建委托的浅表副本.  GetInvocationList   按照调用顺序返回此多路广播委托的调用列表.  GetMethodImpl   返回由当前的 MulticastDelegate 表示的静态方法.  GetObjectData   用序列化该实例所需的所有数据填充 SerializationInfo 对象.  MemberwiseClone   创建当前 Object 的浅表副本.  RemoveImpl   调用列

  • Winform开发框架中如何使用DevExpress的内置图标资源

    前言 在开发Winform程序界面的时候,我们往往会使用一些较好看的图表,以便能够为我们的程序界面增色,良好的图标设置可以让界面看起来更加美观舒服,而且也比较容易理解,图标我们可以通过一些网站获取各种场景的图标资源,不过本篇随笔主要介绍如何利用DevExpress的内置图标资源来实现界面图标的设置. 下面话不多说了,来一起看看详细的介绍吧 1.设计时刻的图标处理 丰富的图标处理,在菜单.工具栏.树列表等地方,以及按钮等地方,都可以使用,而这些我们可以利用DevExpress的内置图标选择来减轻我

  • SpringBoot应用启动内置Tomcat的过程源码分析

    Connector启动过程 Connector是Tomcat提供的类. // 通过此 Connector 开始处理请求 @Override protected void startInternal() throws LifecycleException { // Validate settings before starting if (getPortWithOffset() < 0) { throw new LifecycleException(sm.getString( "coyote

  • Python 内置logging 使用详细介绍

    目录 logging 的主要作用 logging 日志等级 logging 的基础函数 logging 的四大组件(类) logging 的配置 logging 和 print 的区别 主要参考资料 logging 的主要作用 提供日志记录的接口和众多处理模块,供用户存储各种格式的日志,帮助调试程序或者记录程序运行过程中的输出信息. logging 日志等级 logging 日志等级分为五个等级,优先级从高到低依次是 : **CRITICAL; ** 程序严重错误 **ERROR; **程序错误

  • python 内置函数filter

    python 内置函数filter class filter(object): """ filter(function or None, iterable) --> filter object Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true. &

  • Python内置函数之filter map reduce介绍

    Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当于过滤器.调用一个布尔函数bool_func来迭代遍历每个seq中的元素:返回一个使bool_seq返回值为true的元素的序列. >>> N=range(10) >>> print filter(lambda x:x>

  • 用内置变量调试shell脚本的方法

    一般的shell脚本的调试基本都是echo 来处理遇到比较大的脚本的时候,就比较麻烦了,出了问题,还不是很好定位哪行代码出问题了.其实shell内置的一些变量可以很好的解决这个问题: $LINENO  $FUNCNAME $BASH_LINENO 这几个变量记录了脚本当前的执行位置,以及正在执行的函数.具体可以可以man 文档页. 实例代码:a.sh 复制代码 代码如下: #!/bin/bash abc() {echo "wo shi abc()" echo  "func:

随机推荐