/// <summary>
/// 颜色接口
/// </summary>
public interface IColor
{
IColor Clone();
int Red { get; set; }
int Green { get; set; }
int Blue { get; set; }
}
public class RedColor:IColor
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
public IColor Clone()
{
return (IColor)this.MemberwiseClone();
}
}
static void Main(string[] args)
{
IColor color = new RedColor();
color.Red = 255;
Console.WriteLine("color -red " + color.Red); //225
IColor color1 = color.Clone();
color1.Red = 224;
Console.WriteLine("color1-red " + color1.Red);//224
Console.WriteLine("color -red " + color.Red); //225
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
/// <summary>
/// 颜色接口
/// </summary>
public interface IColor
{
IColorDemo Clone();
int Red { get; set; }
int Green { get; set; }
int Blue { get; set; }
Factroy f{get;set;}
}
/// <summary>
/// 生产颜色的工厂信息
/// </summary>
[Serializable]
public class Factroy
{
public string name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
/// <summary>
/// 颜色
/// </summary>
[Serializable]
public class RedColor:IColor
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
public Factroy f { get; set; }
public IColor Clone()
{
SerializableHelper s = new SerializableHelper();
string target = s.Serializable(this);
return s.Derializable<IColor>(target);
}
}
}
/// <summary>
/// 序列化和反序列化辅助类
/// </summary>
public class SerializableHelper
{
public string Serializable(object target)
{
using (MemoryStream stream = new MemoryStream())
{
new BinaryFormatter().Serialize(stream, target);
return Convert.ToBase64String(stream.ToArray());
}
}
public object Derializable(string target)
{
byte[] targetArray = Convert.FromBase64String(target);
using (MemoryStream stream = new MemoryStream(targetArray))
{
return new BinaryFormatter().Deserialize(stream);
}
}
public T Derializable<T>(string target)
{
return (T)Derializable(target);
}
}
static void Main(string[] args)
{
IColor color = new RedColor();
color.Red = 255;
color.f = new Factroy() { name="湖北工厂" };
Console.WriteLine("color - Factroy:" + color.f.name); //湖北工厂
IColor color1 = color.Clone();
color1.Red = 234;
color1.f.name = "北京工厂";
Console.WriteLine("color1- Factroy:" + color1.f.name); //北京工厂
Console.WriteLine("color - Factroy:" + color.f.name); //湖北工厂
Console.Read();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有