委托转换,委托重载


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

namespace ConsoleApplication1
{
    delegate void MyAction();
    delegate void MyAction<T>(T obj);
    delegate void MyAction<T1, T2>(T1 who, T2 say);

    delegate string MyFunc();
    delegate T MyFunc<T>();
    delegate TResult MyFunc<T, TResult>(T give);
    delegate TResult MyFunc<T1, T2, TResult>(T1 give, T2 n);
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Action------------------");
            Action a = () => { Console.WriteLine("hello"); };
            a.Invoke();

            Action<string> b = x => { Console.WriteLine(x); };
            b("hi");

            Action<string, string> c = (x, y) => Console.WriteLine(x + " say " + y);
            c("Mr.Du", "Hello");

            Console.WriteLine("MyAction------------------");

            MyAction a1 = a.Method.CreateDelegate(typeof(MyAction)) as MyAction;
            a1();

            MyAction<string> b1 = b.Method.CreateDelegate(typeof(MyAction<string>)) as MyAction<string>;
            b1("hi1");

            MyAction<string, string> c1 = c.Method.CreateDelegate(typeof(MyAction<string, string>)) as MyAction<string, string>;
            c1("Mr.Du", "Hello1");


            Console.WriteLine("Func------------------");

            Func<string> f1 = () => { return "f1"; };
            Console.WriteLine(f1());

            Func<string, string> f2 = x => { return x + x; };
            string give = "a";
            Console.WriteLine(f2(give) + " get of " + give);

            Func<string, int, string> f3 = (x, n) =>
            {
                for (int i = 0; i < n; i++)
                {
                    x += x;
                }
                return x;
            };
            Console.WriteLine(f3(give, 3) + " get of " + give);


            Console.WriteLine("MyFunc------------------");

            MyFunc mf1 = f1.Method.CreateDelegate(typeof(MyFunc)) as MyFunc;
            Console.WriteLine(mf1());

            MyFunc<string> mf2 = f1.Method.CreateDelegate(typeof(MyFunc<string>)) as MyFunc<string>;
            Console.WriteLine(mf2());

            MyFunc<string, string> mf3 = f2.Method.CreateDelegate(typeof(MyFunc<string, string>)) as MyFunc<string, string>;
            Console.WriteLine(mf3(give) + " get of " + give);

            MyFunc<string, int, string> mf4 = f3.Method.CreateDelegate(typeof(MyFunc<string, int, string>)) as MyFunc<string, int, string>;
            Console.WriteLine(mf4(give,3) + " get of " + give);
        }
    }
}

转载于:https://www.cnblogs.com/cppss/p/3950930.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值