1 using System; 2 using System.Text; 3 using Microsoft.SqlServer.Server; 4 using System.Data.SqlTypes; 5 using System.Runtime.InteropServices; 6 using System.IO; 7 8 [SqlUserDefinedType(Format.UserDefined, MaxByteSize = 512, IsByteOrdered = true)] 9 public class DBMovie : INullable, IBinarySerialize 10 { 11 private bool _isNull; 12 private string _title; 13 private string _director; 14 private decimal _boxOfficeTotals; 15 16 public bool IsNull 17 { 18 get { return _isNull; } 19 } 20 21 public static DBMovie Null 22 { 23 get 24 { 25 DBMovie movie = new DBMovie(); 26 movie._isNull = true; 27 return movie; 28 } 29 } 30 31 public string Title 32 { 33 get { return _title; } 34 set { _title = value; } 35 } 36 37 38 public string Director 39 { 40 get { return _director; } 41 set { _director = value; } 42 } 43 44 [SqlFacet(Precision = 38, Scale = 2)] 45 public decimal BoxOfficeTotals 46 { 47 get { return _boxOfficeTotals; } 48 set { _boxOfficeTotals = value; } 49 } 50 51 52 [SqlMethod(OnNullCall = false)] 53 public static DBMovie Parse(SqlString s) 54 { 55 if (s.IsNull) 56 return Null; 57 58 DBMovie movie = new DBMovie(); 59 string[] parts = s.Value.Split(new char[] { ',' }); 60 movie.Title = parts[0]; 61 movie.Director = parts[1]; 62 movie.BoxOfficeTotals = decimal.Parse(parts[2]); 63 return movie; 64 } 65 66 public override string ToString() 67 { 68 if (this.IsNull) 69 return "NULL"; 70 71 StringBuilder builder = new StringBuilder(); 72 builder.Append(_title); 73 builder.Append(","); 74 builder.Append(_director); 75 builder.Append(","); 76 builder.Append(_boxOfficeTotals.ToString()); 77 return builder.ToString(); 78 } 79 80 public void Write(BinaryWriter w) 81 { 82 w.Write(_title); 83 w.Write(_director); 84 w.Write(_boxOfficeTotals); 85 } 86 87 public void Read(BinaryReader r) 88 { 89 _title = r.ReadString(); 90 _director = r.ReadString(); 91 _boxOfficeTotals = r.ReadDecimal(); 92 } 93 94 public DBMovie() 95 { 96 } 97 }
我从书中看到的代码如上,其中为什么Null属性和Parse方法要定义为静态的?