委托、多播委托、泛型委托Func,Action,Predicate,ExpressionTree

当试图通过一个事件触发多个方法,抽象出泛型行为的时候,或许可以考虑使用委托。

 

  通过委托构造函数或委托变量把方法赋值给委托

        private delegate double DiscountDelegate(double discount);
        private static DiscountDelegate dd = getDiscountRate;
        static void Main(string[] args)
        {
            double discountPercent;
            discountPercent = dd.Invoke(80);
            Console.WriteLine("打折比例是{0}",discountPercent);
 
            DiscountDelegate dd1 = new DiscountDelegate(getDiscountRate);
            discountPercent = dd1(90);
            Console.WriteLine("打折比例是{0}", discountPercent);
            Console.ReadKey();
        }
 
        static double getDiscountRate(double discount)
        {
            return (discount/100);
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

  多播委托

        private delegate double CalculateDelegate(double x, double y);
        private static CalculateDelegate cd = getPlus;
 
        static void Main(string[] args)
        {
            double result1 = cd.Invoke(1, 2);
            Console.WriteLine(result1);
            cd += new CalculateDelegate(getMinus);
            double result2 = cd.Invoke(1, 2);
            Console.WriteLine(result2);
            Console.ReadKey();
        }
 
        static double getPlus(double x, double y)
        {
            return (x + y);
        }
 
        static double getMinus(double x, double y)
        {
            return (x - y);
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

  泛型委托

□ Func<>规定输入输出的泛型委托

    Func<Calculate, double> func = c => c.X*3 + c.Y;
    Calculate calculate = new Calculate(){X = 2,Y=3};
    double result = func(calculate);
    Console.WriteLine(result);
 
    class Calculate
    {
        public double X { get; set; }
        public double Y { get; set; }
    }       

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

□ Action<>规定输入的泛型委托

    ction<string> MyAction = x => Console.WriteLine(x);
     MyAction("Hello World");    

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

□ Predicate<>规定输出为bool类型的泛型委托

   string date = "2012/05/10";
    Predicate<string> isDate = d => IsDate(d);
    if (isDate(date))
    {
        Console.WriteLine("有效");
    }
    else
    {
        Console.WriteLine("无效");
    }
 
    private static bool IsDate(string date)
    {
        DateTime dt;
        return DateTime.TryParse(date, out dt);
    }   
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

□ ExpressionTree泛型委托

    //(5+4)-(2-1)
    //5+4
    BinaryExpression exp1 = Expression.MakeBinary(ExpressionType.Add, Expression.Constant(5),Expression.Constant(4));
    //2-1
    BinaryExpression exp2 = Expression.MakeBinary(ExpressionType.Subtract, Expression.Constant(2),Expression.Constant(1));
 
    //(5+4)-(2-1)
    BinaryExpression resultExp = Expression.MakeBinary(ExpressionType.Subtract, exp1, exp2);
    int result = Expression.Lambda<Func<int>>(resultExp).Compile()();
    Console.WriteLine(result);
    Console.ReadKey();     

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

上一篇:Difference between plt.draw() and plt.show() in matplotlib


下一篇:scapy官方文档