zoukankan      html  css  js  c++  java
  • 2.C#中泛型在方法Method上的实现

    • 阅读目录

          一:C#中泛型在方法Method上的实现

          把Persion类型序列化为XML格式的字符串,把Book类型序列化为XML格式的字符串,但是只写一份代码,而不是public static string GetSerializeredString(Book book)这个方法写一份,再public static string GetSerializeredString(Persion persion)再写一份方法,而是在方法的调用时候再给他传数据类型的类型

       1 using System;
       2 using System.Collections.Generic;
       3 using System.IO;
       4 using System.Linq;
       5 using System.Text;
       6 using System.Threading.Tasks;
       7 using System.Xml.Serialization;
       8 
       9 namespace GenericMethod2
      10 {
      11     class Program
      12     {
      13         static void Main(string[] args)
      14         {
      15             Book book = new Book();
      16             book.BookNumber = "001";
      17             book.Name = "《西游记》";
      18 
      19             Person person = new Person();
      20             person.PersonName = "张三";
      21 
      22             string bookXMLString = GetSerializeredString<Book>(book);
      23 
      24             string personXMLString = GetSerializeredString<Person>(person);
      25 
      26             Console.WriteLine(bookXMLString);
      27 
      28             Console.WriteLine("--------------");
      29 
      30             Console.WriteLine(personXMLString);
      31             Console.ReadLine();
      32         }
      33 
      34         /// <summary>
      35         /// 根据对象和泛型得到序列化后的字符串
      36         /// </summary>
      37         /// <typeparam name="T"></typeparam>
      38         /// <param name="t"></param>
      39         /// <returns></returns>
      40         public static string GetSerializeredString<T>(T t)
      41         {
      42             //1.step 序列化为字符串
      43             XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
      44             MemoryStream ms = new MemoryStream();
      45             xmlserializer.Serialize(ms, t);
      46             string xmlString = Encoding.UTF8.GetString(ms.ToArray());
      47 
      48             return xmlString;
      49         }
      50 
      51     }
      52 
      53     [Serializable()]
      54     [XmlRoot]
      55     public class Book
      56     {
      57         string _booknumber = "";
      58         [XmlElement]
      59         public string BookNumber
      60         {
      61             get { return _booknumber; }
      62             set { _booknumber = value; }
      63         }
      64 
      65         string _name = "";
      66         [XmlElement]
      67         public string Name
      68         {
      69             get { return _name; }
      70             set { _name = value; }
      71         }
      72     }
      73 
      74     [Serializable()]
      75     [XmlRoot]
      76     public class Person
      77     {
      78         string _personName = "";
      79         [XmlElement]
      80         public string PersonName
      81         {
      82             get { return _personName; }
      83             set { _personName = value; }
      84         }
      85     }
      86 
      87 }

       

  • 相关阅读:
    asp.net HttpModule和HttpHandler
    Asp.Net生命周期和Http管道技术
    降低web服务器压力
    html里嵌入CSS的三种方式
    php实现简单视图模板(视图引擎)
    ASP.NET MVC路由配置
    igel udc2 config
    单IP、网络、别名管道限速的设置
    Apple SIP简介及在Clover中如何控制
    Hackintosh
  • 原文地址:https://www.cnblogs.com/menglin2010/p/5223657.html
Copyright © 2011-2022 走看看