1. 程式人生 > >C#委託和dynamic型別配合使用的小例子

C#委託和dynamic型別配合使用的小例子

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace ConsoleApplication1
{
    static class Program
    {
        public class MyClass
        {
            public MyClass()
            {
                Id = "1";
                Name = "王123123";
            }
            public string Id { get; set; }
            public string Name { get; set; }
            public override string ToString()
            {
                return this.Id + "-" + this.Name;
            }
        }

        static void Main(string[] args)
        {
            int timeS = System.Environment.TickCount;
            int timeE = System.Environment.TickCount;
            var timeSpan = new Stopwatch();
            timeSpan.Start();
            Excute(() =>
            {
                Console.WriteLine(new MyClass());
                return null;
            });
            timeSpan.Stop();
            Console.WriteLine(timeSpan.ElapsedMilliseconds);
            Console.ReadKey();
        }

        public static dynamic Excute(Func<dynamic> code)
        {
            if (code != null)
            {
                dynamic result = code();
                return result;
            }

            return null;
        }
    }
}