1,在CLR中引入了可空值类型结构 System.Nullable<T>-----注意这个是值类型(struct).
注解:[System.Runtime.InteropServices.ComVisible(true)]---控制member对于Com的可访问性.意思是是否可以由COm来访问.
注解:system.SerializableAttribute----表明对象是否可以序列化.
[System.Runtime.InteropServices.ComVisible(true)] [System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Struct, Inherited=false)] public sealed class SerializableAttribute : Attribute
Serializable---举例
1.1创建类及相关的方法
[Serializable] public class SerializeTest { public int member1; public string member2; public string member3; public double member4; // A field that is not serialized. [NonSerialized()] public string member5; public SerializeTest() { member1 = 11; member2 = "hello"; member3 = "hello"; member4 = 3.14159265; member5 = "hello world!"; } public void Print() { Console.WriteLine("member1 = '{0}'", member1); Console.WriteLine("member2 = '{0}'", member2); Console.WriteLine("member3 = '{0}'", member3); Console.WriteLine("member4 = '{0}'", member4); Console.WriteLine("member5 = '{0}'", member5); }
1.2 进行测试代码
使用Soap.SoapFormatter,进行序列化和反序列化一个类的实列.
public static void Go() { // Creates a new SerializeTest object. SerializeTest obj = new SerializeTest(); Console.WriteLine("Before serialization the object contains: "); obj.Print(); // Opens a file and serializes the object into it in binary format. Stream stream = File.Open("data.xml", FileMode.Create); SoapFormatter formatter = new SoapFormatter(); //BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); stream.Close(); // Empties obj. obj = null; // Opens file "data.xml" and deserializes the object from it. stream = File.Open("data.xml", FileMode.Open); formatter = new SoapFormatter(); //formatter = new BinaryFormatter(); obj = (SerializeTest)formatter.Deserialize(stream); stream.Close(); Console.WriteLine(""); Console.WriteLine("After deserialization the object contains: "); obj.Print(); }
查看 data.xml文件
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <a1:SerializeTest id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ClrFromCSharp_2_2.LearnAttribute/ClrFromCSharp_2_2%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D11fccde6e91ad1e9"> <member1>11</member1> <member2 id="ref-3">hello</member2> <member3 href="#ref-3"/> <member4>3.14159265</member4> </a1:SerializeTest> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
查看结果--member5没有值.
Before serialization the object contains: member1 = '11' member2 = 'hello' member3 = 'hello' member4 = '3.14159265' member5 = 'hello world!' After deserialization the object contains: member1 = '11' member2 = 'hello' member3 = 'hello' member4 = '3.14159265' member5 = ''
2,Nullrable<T>结构的本质
基本属性
Nullable<T> 结构的两个基本成员是 HasValue 和 Value 属性。 如果 Nullable<T> 对象的 HasValue 属性为 true
,则可以使用 Value 属性访问对象的值。 如果 false
HasValue 属性,则对象的值是不确定的,尝试访问 Value 属性将引发 InvalidOperationException。
装箱和拆箱
- 装箱:如果HasValue=True ,则装箱Null,否则,装箱其基础值<T>,而非Nullrable<T>.
- 拆箱:如果HasValue=True,则将其基础值返回,否则返回null;
3,空接合操作符号??
string filename=GetFileName()??"Untitled";//如果前面的方法是空,则赋值后面的值.
4,可空值调用GetType(),返回的是<T>的类型,而不是Null able<T>.