序列化和反序列化,异步调用web/wcf/函数

 
       //xml序列化
        public static string Seria(DataSet ds)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DataSet));
            StringBuilder sb = new StringBuilder();

            XmlWriter writer = XmlWriter.Create(sb);
            serializer.Serialize(writer, ds);
            return sb.ToString();
        }
        //xml反序列化
        public static DataSet DeSeria(string sb)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DataSet));
            StringReader sr = new StringReader(sb);

            XmlReader xr = XmlReader.Create(sr);
            DataSet ds = (DataSet)serializer.Deserialize(xr);
            return ds;
        }
        public static string Seria<T>(T ds)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringBuilder sb = new StringBuilder();

            XmlWriter writer = XmlWriter.Create(sb);
            serializer.Serialize(writer, ds);
            return sb.ToString();
        }
        //xml反序列化
        public static T DeSeria<T>(string sb)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringReader sr = new StringReader(sb);

            XmlReader xr = XmlReader.Create(sr);
            T ds = (T)serializer.Deserialize(xr);
            return ds;
        }



核心测试代码:

        private void butCallWebservice1_Click(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient wc = new ServiceReference1.WebService1SoapClient();
            string ret = wc.HelloWorld();
            MessageBox.Show("web服务返回值:"+ret);
        }

        public int add(int a, int b)
        {
            System.Threading.Thread.Sleep(4000);//耗时的操作
            return a + b;
        }
        public delegate int defun(int a,int b);//定义一个委托
        public void f2()
        {
            MessageBox.Show(DateTime.Now.ToString());
        }
        private void btnTestseria_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("id",typeof(string));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("age", typeof(int));
            DataRow dr = dt.NewRow();
            dr["id"] = "1";
            dr["name"] = "testname1";
            dr["age"] = 22;
            dt.Rows.Add(dr);
            ds.Tables.Add(dt);
           string s= Common.Common.Seria(ds);//DataSet序列化和反序列化
           DataSet dsde = Common.Common.DeSeria(s);
           MessageBox.Show(s+"\n "+ds.Tables[0].Rows.Count);
            List<Student> list=new List<Student>();
            list.Add(new Student("2","testname2",25));
            string s2 = Common.Common.Seria<List<Student>>(list);
            List<Student> listde = Common.Common.DeSeria<List<Student>>(s2);
            MessageBox.Show(s2+" \n "+listde.Count);//List<对象>序列化和反序列化

        }

        private void btnCall1_Click(object sender, EventArgs e)
        {
            f2();
        }

        private void btnAsynCall2_Click(object sender, EventArgs e)
        {
            //异步调用
            defun objDe = new defun(add);
            IAsyncResult Iasyn = objDe.BeginInvoke(12, 34, null, null);
            int re = objDe.EndInvoke(Iasyn);//这里会阻塞线程
            MessageBox.Show("异步调用返回结果:" + re);
        }
        defun objDe2 = null;
        bool isCalling = false;
        /// <summary>
        /// 自动回调方式调用函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAsynCall3_Click(object sender, EventArgs e)
        {
            if (isCalling == false)
            {
                objDe2 = new defun(add);
                AsyncCallback AsynCallback = new AsyncCallback(AsynCallbackfun);
                isCalling = true;
                objDe2.BeginInvoke(23, 45, AsynCallback, null);//完成后自动触发回调函数,不会阻塞线程
            }
        }
        void AsynCallbackfun(IAsyncResult Iasy)//异步回调函数
        {
            if (objDe2 != null)
            {
                int re = objDe2.EndInvoke(Iasy);//这里会阻塞线程
                isCalling = false;
                MessageBox.Show("异步调用返回结果:" + re);
            }
        }
        void AsynCallbackfun2(IAsyncResult Iasy)//异步回调函数
        {
            if (objService != null)
            {
                string re = objService.EndGetData(Iasy);//这里会阻塞线程
                isCalling2 = false;
                MessageBox.Show("Wcf异步调用返回结果:" + re);
            }
        }
        ServiceReference2.WcfService2Client objService = null;
        bool isCalling2 = false;
        /// <summary>
        /// 自动回调方法调用WCF/Web 服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWcfAsynCall1_Click(object sender, EventArgs e)
        {
            if (isCalling2 == false)
            {
                objService = new ServiceReference2.WcfService2Client();
                AsyncCallback AsynCallback = new AsyncCallback(AsynCallbackfun2);
                isCalling2 = true;
                //添加引用时在“高级”中选择“允许生成异步操作”
                //或者在已有的服务引用上“配置服务引用”
                objService.BeginGetData
                    (AsynCallback, null);
            }
        }



示例源代码下载:http://download.csdn.net/detail/pukuimin1226/6953713

你可能感兴趣的:(序列化和反序列化,异步调用web/wcf/函数)