有时候你可以丢掉整个世界,但你不可以丢掉自己--王建国
随笔 - 7,  文章 - 2,  评论 - 5,  阅读 - 3605
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
//using System.Reflection;

namespace abstractClass
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
           
          //  C c = new C();          
          //  c.G();
          //  c.F();
          //E ds = new E();
           
            //MessageBox.Show(ds.GenerateImage().ImageFormat.ToString());
           // ActivatorCreateInstance.main aac = new ActivatorCreateInstance.main();
            ActivatorCreateInstance.main.asa();
             Test test=new Test();
             test.Deserialize();
        }
       
      
    }
    [Serializable]
    public class Person
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Sex;
        public int Age = 31;
        public Course[] Courses;

        public Person()
        {
        }
        public Person(string Name)
        {
            name = Name;
            Sex = "男";
        }
    }
    [Serializable]
    public class Course
    {
        public string Name;
        [XmlIgnore]
        public string Description;
        public Course()
        {
        }
        public Course(string name, string description)
        {
            Name = name;
            Description = description;
        }
    }
    class Test:Form1
    {
        //序列化
        public void Serialiaze()
        {
            Person c = new Person("cyj");
            c.Courses = new Course[2];
            c.Courses[0] = new Course("英语", "交流工具");
            c.Courses[1] = new Course("数学", "自然科学");

            XmlSerializer xs = new XmlSerializer(typeof(Person));
            Stream stream = new FileStream("c:\\cyj.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            xs.Serialize(stream, c);
            stream.Close();
        }
        //反序列化
            public void Deserialize()
            {
            XmlSerializer xs=new XmlSerializer(typeof(Person));
            Stream stream = new FileStream("c:\\cyj.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            Person p=(Person)xs.Deserialize(stream);
           MessageBox.Show(p.Name);
           MessageBox.Show(p.Age.ToString());
           MessageBox.Show(p.Courses.Length.ToString());
           
            }
       
    }

 

    /// <summary>
    /// 类E实现了IVerifyImage接口
    /// </summary>
    public class E : IVerifyImage
    {
        public D GenerateImage()
        {
            D dd = new D();
            dd.ImageFormat = ImageFormat.Icon;
            return dd;
        }
    }
    /// <summary>
    /// IVerifyImage接口
    /// </summary>
    public interface IVerifyImage
    {
        D GenerateImage();
    }
    /// <summary>
    /// 定义一个类型为D的类
    /// </summary>
    public class D
    {
        private ImageFormat imageFormat = ImageFormat.Jpeg;
        public ImageFormat ImageFormat
        {
            get { return imageFormat; }
            set { imageFormat = value; }
        }
     
    }
    /// <summary>
    /// 定义抽象类A、B,B继承A,C继承B
    /// </summary>
    abstract class A{ public abstract void F(); }
    abstract class B:A{public abstract void G();}
    class C : B
    {
        public override void F(){MessageBox.Show("override void F()");}
        public override void G(){MessageBox.Show("override void G()");}
       
     
    }
       
}
namespace ActivatorCreateInstance
{
    /// <summary>
    /// ClassExam 的摘要说明。
    /// </summary>
    public class ClassExam : IObjcet
    {
        private string name = "default name";
        public ClassExam()
        {

        }

        public ClassExam(string name)
        {
            this.name = name;
        }

        public void printName()
        {
            MessageBox.Show(this.name+"'s name is:"+"c://aa//".Trim().Trim('\\')+"db.dll");
           // Console.WriteLine(this.name);
          
        }
    }
}

namespace ActivatorCreateInstance
{
 /// <summary>
 /// IObjcet 的摘要说明。
 /// </summary>
 public interface IObjcet
 {
  void printName();
 }
}
namespace ActivatorCreateInstance
{
    /// <summary>
    /// main 的摘要说明。
    /// </summary>
    public class main
    {
       
        public  static void asa()
        {

            //用传递参数来得到一个类的实例
          
            string classname="ClassExam";
            //用Activator .CreateInstance创建函数实例,默认的不带参数的构造函数
            IObjcet obj = (IObjcet)Activator.CreateInstance(System.Type.GetType(string.Format("ActivatorCreateInstance.{0}", classname)), null);
            //System.Type .GetType  命名空间.类名,程序集
            obj.printName();
            //调用ClassExam类的另一个带参数构造函数
            IObjcet obj2 = (IObjcet)System.Activator.CreateInstance(System.Type.GetType("ActivatorCreateInstance.ClassExam",true), new string[] { "dd" });
            obj2.printName();

        }
    }
}

posted on   王*建国  阅读(822)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示