• 阅读目录

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

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

     using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization; namespace GenericMethod2
    {
    class Program
    {
    static void Main(string[] args)
    {
    Book book = new Book();
    book.BookNumber = "";
    book.Name = "《西游记》"; Person person = new Person();
    person.PersonName = "张三"; string bookXMLString = GetSerializeredString<Book>(book); string personXMLString = GetSerializeredString<Person>(person); Console.WriteLine(bookXMLString); Console.WriteLine("--------------"); Console.WriteLine(personXMLString);
    Console.ReadLine();
    } /// <summary>
    /// 根据对象和泛型得到序列化后的字符串
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="t"></param>
    /// <returns></returns>
    public static string GetSerializeredString<T>(T t)
    {
    //1.step 序列化为字符串
    XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
    MemoryStream ms = new MemoryStream();
    xmlserializer.Serialize(ms, t);
    string xmlString = Encoding.UTF8.GetString(ms.ToArray()); return xmlString;
    } } [Serializable()]
    [XmlRoot]
    public class Book
    {
    string _booknumber = "";
    [XmlElement]
    public string BookNumber
    {
    get { return _booknumber; }
    set { _booknumber = value; }
    } string _name = "";
    [XmlElement]
    public string Name
    {
    get { return _name; }
    set { _name = value; }
    }
    } [Serializable()]
    [XmlRoot]
    public class Person
    {
    string _personName = "";
    [XmlElement]
    public string PersonName
    {
    get { return _personName; }
    set { _personName = value; }
    }
    } }

     

2.C#中泛型在方法Method上的实现的更多相关文章

  1. python 中的__new__方法

    1.有关__new__方法的介绍 __new__方法调用在构造方法构造实例之前,即在__init__方法执行之前,我们可以这样理解,他的作用是决定是否适用这个__iint__方法来构造实例,但是需要注 ...

  2. Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)

    Python 在子类中调用父类方法详解(单继承.多层继承.多重继承)   by:授客 QQ:1033553122   测试环境: win7 64位 Python版本:Python 3.3.5 代码实践 ...

  3. 【转载】OLE控件在Direct3D中的渲染方法

    原文:OLE控件在Direct3D中的渲染方法 Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题 那么, 有什么 ...

  4. 【java编程】java中什么是bridge method(桥接方法)

    https://blog.csdn.net/mhmyqn/article/details/47342577 https://www.cnblogs.com/strinkbug/p/5019453.ht ...

  5. java Class中得到构造方法Constructor、方法Method、字段Field

    常用方法: Constructor类用于描述类中的构造方法: Constructor<T> getConstructor(Class<?>... parameterTypes) ...

  6. struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

    struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

  7. 1.什么是泛型和C#中泛型在Class上的实现

    阅读目录 一:什么是泛型? 二:C#中泛型在Class上的实现   一:什么是泛型? 我们在编程的时候需要一个数据类型,但是在刚开始的时候还不确定这个数据类型是怎么样的,或者说对于不同的多个数据类型有 ...

  8. [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法

    12.3 We have the following method used in a chess game: boolean canMoveTo( int x, int y). This metho ...

  9. Python魔法方法(magic method)细解几个常用魔法方法(上)

    这里只分析几个可能会常用到的魔法方法,像__new__这种不常用的,用来做元类初始化的或者是__init__这种初始化使用的 每个人都会用的就不介绍了. 其实每个魔法方法都是在对内建方法的重写,和做像 ...

随机推荐

  1. 大数据处理对象CLOG BLOG

    File 文件类的使用 decimal 类型(规范小数的形式)  longtext 类型(存储字符大数据) 存入文本文件到数据库 InputStream FileInputStream CLOG 类的 ...

  2. JS检测浏览器是否支持HTML5视频播放 (标签<video>) ,

    function checkVideo() { if (!!document.createElement('video').canPlayType) { var vidTest = document. ...

  3. day4----生成器,迭代器

    迭代器,生成器,装饰器 1.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要 ...

  4. jquery 编码解码

    中文转Unicode:HttpUtility.UrlEncodeUnicode(string str); 转换后中文格式:"%uxxxx" 举例:"柳_abc123&qu ...

  5. SQL语句 in和inner join各有什么优点

    比如A1表 100W行 A2表50W行select a.* from A1 a where a.column1 in (select b.column1 from A2 b where b.colum ...

  6. 团队项目--站立会议 DAY4

    小组名称:D&M 参会人员:张靖颜,钟灵毓秀,何玥,赵莹,王梓萱 项目进展: 1.张靖颜:筛选完元素后,把昨天优化完的再进行测试,进行进一步的检验.对于钟灵毓秀和赵莹同学编写的代码进行进一步审 ...

  7. 从源代码的角度分析--在BaseAdapter调用notifyDataSetChanged()之后发生了什么

    导师安排我做一个小项目,其中涉及到利用Adapter作为ListView的适配器,为ListView提供数据.选中某一项后,要让这一项变成选中状态,也就是背景图片要换一下.下面我就用一个小例子来模拟. ...

  8. [游戏模版19] Win32 物理引擎 匀速运动

    >_<:Learning the physical engine >_<:resource >_<:code #include <windows.h> ...

  9. Linux下jvm、tomcat、mysql、log4j优化配置笔记

    小菜一直对操作系统心存畏惧,以前也很少接触,这次创业购买了Linux云主机,由于木有人帮忙,只能自己动手优化服务器了.... 小菜的云主机配置大致为:centeos6(32位),4核心cpu,4G内存 ...

  10. 【Oracle】Oracle锁表处理

    问题分析(1)锁的分析ORACLE里锁有以下几种模式:0:none1:null 空2:Row-S 行共享(RS):共享表锁,sub share3:Row-X 行独占(RX):用于行的修改,sub ex ...