首页 > 编程语言 >C# 对象序列化 单元测试 .netframework

C# 对象序列化 单元测试 .netframework

时间:2024-01-15 18:34:45浏览次数:18  
标签:netframework C# serialize 单元测试 System new var using 序列化

对象序列化以及单元测试

F:\song\netframework_serialize\netframework_serialize\Program.cs

using netframework_serialize.Animal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace netframework_serialize
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var myObject = new Person(1,"Alice");

            var formatter = new BinaryFormatter();
            using (var stream = new FileStream("myobject.bin", FileMode.Create, FileAccess.Write))
            {
                formatter.Serialize(stream, myObject);
            }

            using (var stream = new FileStream("myobject.bin", FileMode.Open, FileAccess.Read))
            {
                var deserializedObject = (Person)formatter.Deserialize(stream);
                Console.WriteLine($"Id: {deserializedObject.Id}, Name: {deserializedObject.getName()}");
            }

        }
    }
}



F:\song\netframework_serialize\netframework_serialize\Animal\Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;



namespace netframework_serialize.Animal
{

    [Serializable]
    public class Person
    {
        public int Id { get; set; }
        private string Name { get; set; }

        public Person(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
        public string getName()
        {
            return this.Name;
        }
    }

}

F:\song\netframework_serialize\PersonTests\UnitTest1.cs

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using netframework_serialize.Animal;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace PersonTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var formatter = new BinaryFormatter();
            using (var stream = new FileStream("myobject.bin", FileMode.Open, FileAccess.Read))
            {
                var deserializedObject = (Person)formatter.Deserialize(stream);
                var per2 = new Person(deserializedObject.Id,deserializedObject.getName());
                Assert.AreEqual(per2.getName(), "Alice");
                Assert.AreEqual(per2.Id, 1);
            }

        }
    }
}

标签:netframework,C#,serialize,单元测试,System,new,var,using,序列化
From: https://www.cnblogs.com/zhuoss/p/17966004

相关文章

  • 软件测试/测试开发/全日制|Page Object模式:为什么它是Web自动化测试的必备工具
    为UI页面写测试用例时(比如web页面,移动端页面),测试用例会存在大量元素和操作细节。当UI变化时,测试用例也要跟着变化,PageObject很好的解决了这个问题。使用UI自动化测试工具时(包括selenium,appium等),如果无统一模式进行规范,随着用例的增多会变得难以维护,而PageObject让......
  • C# 对两个需要顺序执行的函数进行异步交叉,提高执行速度
    有的时候我们会有2个函数需要顺序执行,比如将数据库的数据写到硬盘上,读数据库和写硬盘都是IO比较慢的操作,于是我们很容易就想到让他们异步执行,避免阻塞,提高性能,但是为了保证数据的顺序一致,我们又需要整个队列来存放数据,感觉比较麻烦,今天研究了下,通过异步和信号量控制实现了两个函......
  • C#中var关键字详解:强类型、匿名类型和LINQ查询的妙用!
     在C#中,var关键字是强类型的,因为它在编译时会根据变量的初始化表达式推断出变量的实际类型,并且一旦确定了类型,就不能再更改。这种类型推断是在编译时进行的,因此代码中的变量在运行时是具有明确定义类型的。下面是一个简单的示例,说明var的强类型特性以及使用时的一些注意事项:......
  • Porsche Piwis 3 is The Ultimate Porsche Diagnostic Tool
     Piwis3isthethirdgenerationofadiagnostictooldevelopedbyPorscheforuseintheirworkshops.ItisaspecializedpieceofequipmentthatallowsPorschetechniciansandmechanicstodiagnoseandtroubleshootissuesinPorschevehicles.PorscheP......
  • podman和vscode dev container的一些问题
    1.MacArm芯片上podmanmachine启动时卡在CurrentStarting起不来。参考这个链接的回答,更新Qemu的文件https://github.com/containers/podman/issues/21096#issuecomment-1872551224Ihavethesameissue,andalsotriedsomemethodsdiscussedin#21088(comment),none......
  • LSP 网络劫持(Layered Service Provider Hijacking)
    LSP简介:分层服务提供商(LayeredServiceProvider,LSP)是一种可以扩展Winsock作为应用程序的Windows的网络套接字工具的机制。WinsockLSP可用于非常广泛的实用用途,包括Internet家长控制(parentalcontrol)和Web内容筛选。在以前版本的WindowsXP中,删除不正确的(也称......
  • ubuntu18.04+ ROS melodic 安装trac-ik
    IKFast太麻烦了,出现错误太多,安装失败!转而安装Trac-IK,方法很简单,ROS的软件源中已经集成了TRAC-IK的安装包,可以直接使用以下命令安装:sudoapt-getinstallros-melodic-trac-ik然后修改机械臂MoveIt!配置功能包下的kinematics.yaml文件就可以使用啦: arm:kinematics_sol......
  • 前端跨域三种解决方式(CORS、JSONP、代理跨域)
    什么是跨域?跨域是浏览器为了安全而作出的限制策略(所以服务端不涉及到跨域);浏览器请求必须遵循同源策略,即同域名、同端口、同协议;例如:http://www.abc.com到http://www.def.com的请求会出现跨域(域名不同)http://www.abc.com:3000到http://www.abc.com:3001的请求会出现跨域(端口不同......
  • IIC:DDM_SFP光模块参数读取
    光模块数字诊断监控数据读取逻辑报告I2C从设备地址0xA2访问的256字节的数据包括一些常量,也包含一些只读的变量,甚至还有一些可写的变量。数字诊断内存映射专用数据字段描述如下: 图1期间地址分布说明 图2检测信号地址 Finisar公司的DDM数据位于器件地址A2H,具体信号数据......
  • C-strtok-strstr
    strtok字符串分段截取https://www.runoob.com/cprogramming/c-function-strtok.htmlchar*strtok(char*str,constchar*delim)#include<string.h>#include<stdio.h>intmain(){charstr[80]="Thisis-www.runoob.com-website"......