为匿名类型添加方法

1:这个做法不太常用

2:这个方法要传递两个参数,

打算用扩展方法解决这个问题,

但是扩展方法又不能用在匿名类型上,

有点郁闷

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

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string, string> getAllName = delegate(string first,string last)
            {
                return string.Format("{0} {1}", first, last);
            };

            var myObj = new { firstName = "allen", secondName = "Liu", getName = getAllName };
            //这里需要传递两个参数,有点郁闷
            string myName = myObj.getName(myObj.firstName,myObj.secondName);
            Console.WriteLine(myName);
            Console.ReadKey();
        }
    }
}

原文地址:https://www.cnblogs.com/liulun/p/1689501.html