.NET Framework基础知识(四)(转载)

1、反射:是编程的读取与类型相关联的元数据的行为。通过读取元数据,可以了解它是什么类型以及类型的成员。

比如类中的属性,方法,事件等。所属命名空间System.Reflection。
例:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Day1401
{
    class Program
    {
        static void Main(string[] args)
        {
        }
        public Program()
        {
            Console.WriteLine("无参构造函数!");
        }
        public Program(int i)
        {
            Console.WriteLine("有参构造函数!"+i);
        }
        public void method()
        {
            Console.WriteLine("方法!");
        }
        string str="bbb";
        public string Str
        {
            get
            {
                return str;
            }
            set
            {
                str = value;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Day1402
{
    class Program
    {
        static void Main(string[] args)
        {
            //反射方法、无参构造函数
            Assembly con1 = Assembly.LoadFrom(@"E:\我的项目\Day1401\Day1401\bin\Debug\Day1401.exe");
            object obj = con1.CreateInstance("Day1401.Program");
            MethodInfo mi = obj.GetType().GetMethod("method");
            mi.Invoke(obj, null);

            //反射字段
            FieldInfo fi = obj.GetType().GetField("str", BindingFlags.NonPublic | BindingFlags.Instance);
            Console.WriteLine(fi.GetValue(obj));

            //反射属性
            PropertyInfo pi = obj.GetType().GetProperty("Str");
            pi.SetValue(obj, "aaa", null);
            Console.WriteLine(pi.GetValue(obj, null));

           
            //反射有参构造函数
            Type[] t1 = new Type[] { typeof(int)};
            ConstructorInfo ci= obj.GetType().GetConstructor(t1);
            object[] o = new object[] { 66};
            ci.Invoke(o);

            //反射成员类型
            Type type = typeof(Program);
            MemberInfo[] MI = type.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            foreach (MemberInfo memb in MI)
            {
                Console.WriteLine("名称:{0},类型{1}", memb.Name, memb.MemberType.ToString());
            }
        }
    }
}
2、属性Attribute:这里的属性并非类的成员,它提供功能强大的方法以将声明信息与C#代码(类型、方法、属性)相关联。

属性与程序实体关联后,即可在运行时使用名为“反射”的技术查询属性。
属性出现的形式有两种:一种是在公共语言运行库中定义的属性,另一种是可以创建的用于向代码中添加附加信息的自定义属性。
属性的特点:1)属性可向程序中添加元数据  2)程序可以使用反射检查自己的元数据  3)通常使用属性与com交互。
3、自定义属性
语法:[attributeClass(定位参数|,...命名参数|,...)]
定位参数和相应特征类的实例构造器紧密相关——构造器提供了什么样的参数构造方式,位置参数就对应什么样的形式。

位置参数不可省略,但如果特征类提供了无参数的构造器,那就另当别论。
命名参数对应着特征类的实例公有域或实例属性,但它在实例化的时候并非必须,可以省略。
例:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Day1404
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (object o in typeof(Zhu).GetCustomAttributes(true))
            {
                ShiziAttribute shizi = o as ShiziAttribute;
                Console.WriteLine(shizi.Name);
            }
        }
    }
    [AttributeUsage(AttributeTargets.Class,AllowMultiple=true,Inherited=true)]
    class ShiziAttribute : Attribute
    {
        string name;
        double size;
        public string Name
        {
            get
            {
                return name;
            }
        }
        public double Size
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
            }
        }
        public ShiziAttribute(string name)
        {
            this.name = name;
        }
    }
    [Shizi("老二",Size=0.9)]
    [Shizi("老大",Size=1.0)]
    class Zhu
    { }
}
4、序列化:是将对象状态转换为可保存或传输的形式的过程。序列化的补集是反序列化,后者将流转换为对象。

这两个过程一起保证数据易于存储和传输。
5、.NET Framework 提供了两个序列化技术:
(1)二进制序列化保持类型保真,这对于多次调用应用程序时保持对象状态非常有用。例如,通过将对象序列化到剪贴板,

可在不同的应用程序之间共享对象。您可以将对象序列化到流、磁盘、内存和网络等。远程处理使用序列化,

“按值”在计算机或应用程序域之间传递对象。
(2)XML 序列化只序列化公共属性和字段,并且不保持类型保真。当您希望提供或使用数据而不限制使用该数据的应用程序时,

这一点非常有用。由于 XML 是开放式的标准,因此它对于通过 Web 共享数据来说是一个理想选择。

SOAP 同样是开放式的标准,这使它也成为一个理想选择。
6、二进制序列化:可以将序列化定义为一个将对象状态存储到存储介质的过程。在这个过程中对象的公共字段

和私有字段以及类的名称,将转换成字节流,而字节流接着将写入数据流。
7、二进制序列化需要的命名空间
  using System.Runtime.Serialization;
  using System.Runtime.Serialization.Formatters.Binary;
例:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Day1407
{
    [Serializable]
    public class Person
    {
        List<Person> list = new List<Person>();
        public List<Person> List
        {
            get{return list;}
            set { list = value; }
        }
        public int age;
        public string name;
        public bool Sex
        {
            get;
            set;
        }
    }
  
    class Program
    {
        static void Main(string[] args)
        {
            Person perp = new Person();
            Person per1 = new Person();
            per1.age = 18;
            per1.name = "XiaoHui";
            per1.Sex = false;
            perp.List.Add(per1);
            Person per2 = new Person();
            per2.age = 20;
            per2.name = "ChenHui";
            per2.Sex = true;
            perp.List.Add(per2);
            IFormatter formatter = new BinaryFormatter();

            //序列化
            Stream stream = new FileStream("F:/Myfile.bin", FileMode.Create, FileAccess.Write);
            formatter.Serialize(stream,perp);
            stream.Close();

            //反序列化
            Stream stream1 = new FileStream("F:/MyFile.bin", FileMode.Open, FileAccess.Read);
            Person per = formatter.Deserialize(stream1) as Person;
            foreach (Person p in per.List)
            {
                Console.WriteLine(p.name+"  "+p.age+"  "+p.Sex);
            }
           
            stream1.Close();
        }
    }
}
8、选择序列化:可以在要序列化的类前面加[Serializable]
例:[Serializable]
    public class Person
    {       
       [NonSerialized]
        public  int age;
        public bool Sex
        {  get;  set;  }
    }
 

本文出自 “大懒丫头” 博客,请务必保留此出处http://lanyatou.blog.51cto.com/3306130/633162

原文地址:https://www.cnblogs.com/babycool/p/2137334.html