• 阅读目录

        一: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. android --- Afianl框架里面的FinalBitmap加载网络图片

    Afinal里面FinalBitmap:用于显示bitmap图片,而无需考虑线程并发和oom等问题. 1.测试请求 使用网页打开http://avatar.csdn.net/C/6/8/1_bz419 ...

  2. Activity之间的通信

    通常Activity之间的通信有三种方式:单向不传参数通信.单项传参数通信和双向通信. 这几种传递方式都需要通信使者Intent.以下将用代码来辅助理解. 1.单向不传递参数通信 public cla ...

  3. Winpcap构建用户级网桥

    Winpcap构建网桥 根据winpcap sdk中的user-level-bridge用户级网桥 |机器1                 |                  |机器2   | | ...

  4. Mat 转 IplImage

    Mat 转 IplImage 分类: OpenCV2013-06-02 17:00 1487人阅读 评论(0) 收藏 举报 Mat 转 IplImage Mat image1; IplImage *i ...

  5. hdu 5098 双队列拓扑排序

    http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...

  6. Android:res之layer-list的用法

    layer-list可以将多个图片按照顺序层叠起来,让其看起来像一个图一样.  和    叠加为: 用法: 在在drawable下建立一个xml文件,faceleft.xml <?xml ver ...

  7. android:shape

    android:shape=["rectangle" | "oval" | "line" | "ring"] shape ...

  8. 初识ASP.NET 5中的Sake与KoreBuild

    从github上签出基于ASP.NET 5的MVC 6的源代码进行编译,发现有2个编译命令: build.cmd是针对Windows的编译命令,build.sh是针对Mac/Linux的编译命令,这本 ...

  9. Memcached使用小记

    该文章简单记录一下在Windows平台下安装与配置Memcached的方法,Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. 1.下载Memcached ...

  10. 用Photoshop处理图片使背景透明

    用Photoshop处理图片使背景透明 打开一张图片 双击背景或者右键背景图层,新建一个图层, 选择魔棒工具,单击图片, 会自动选择颜色相近的范围 按下键盘的delete键,就可以删除魔棒所选择的区域 ...