简单异步和反射小例子

class Program
    {
        public delegate string MyDelegate(string name);
        
        static void Main(string[] args)
        {
            #region 异步部分
            MyDelegate mygate = GetName;//new MyDelegate(GetName);

            //声明回调
            AsyncCallback callback = new AsyncCallback(CallBack);
            //委托调用方法
            mygate.BeginInvoke("李菲菲", callback, mygate);

            Console.WriteLine("下面我还要执行方法...");
            Console.ReadLine();
            #endregion

            #region 反射部分
            Models model = new Models();
            model.id = 1;
            model.name = "李菲菲";
            model.pwd = "123456";

            List<Models> list = new List<Models>();
            list.Add(model);
            Type t = model.GetType();
                
            Console.WriteLine(t.FullName + ":" + t.Name);

            PropertyInfo[] pinfo = t.GetProperties();
            foreach (var item1 in list)
            {
                foreach (PropertyInfo item in pinfo)
                {
                    Console.WriteLine(item.Name + " : " + item.PropertyType);
                    Console.WriteLine(item.GetValue(item1, null));
                }

            }
            Console.ReadLine();
            #endregion
        }

        public static string GetName(string name)
        {
            Thread.Sleep(TimeSpan.FromSeconds(3));
            Console.WriteLine("我的名字是:{0}", name);
            return "委托方法执行完毕!";
        }
        //回调方法
        public static void CallBack(IAsyncResult iar)
        {
            MyDelegate mygate = (MyDelegate)iar.AsyncState;
            string s = mygate.EndInvoke(iar);
            Console.WriteLine(s);
        }
    }

    public class Models
    {
        public int id { get; set; }
        public string name { get; set; }
        public string pwd { get; set; }
    }

 

posted on 2012-06-15 16:09  李菲菲  阅读(216)  评论(0编辑  收藏  举报