2010-04-04 214 views
0

當我執行下面的代碼saveXML時,它會生成上面的錯誤,爲什麼?Webdev.Webserver停止工作

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Xml.Serialization; 
using System.IO; 

/// <summary> 
/// Summary description for Post 
/// </summary> 
public class Post 
{ 
    private int postIDCounter = 0; 
    private String DateCreated; 

    public Post() 
    { 
     Author = "unknown"; 
     Title = "unkown"; 
     Content = ""; 
     DateCreated = DateTime.Now.ToString(); 
     ID = postIDCounter++; 
    } 

    public int ID 
    { 
     get { return ID; } 
     set 
     { 
      if (ID != value) 
       ID = value; 
     } 
    } 

    public string Author 
    { 
     get { return Author; } 
     set 
     { 
      if (Author != value) 
       Author = value; 
     } 
    } 

    public string Title 
    { 
     get { return Title; } 
     set 
     { 
      if (Title != value) 
       Title = value; 
     } 
    } 

    public string Content 
    { 
     get { return Content; } 
     set 
     { 
      if (Content != value) 
       Content = value; 
     } 
    } 

    public void saveXML() 
    { 
     XmlSerializer serializer = new XmlSerializer(typeof(Post)); 
     Stream writer = new FileStream("..'\'App_Data'\'posts'\'" + new Guid(ID.ToString()).ToString() + ".xml", FileMode.Create); 
     serializer.Serialize(writer, this); 
     writer.Close(); 
    } 
} 

回答

3

所有的變量都是循環引用,它永遠循環並最終導致系統停止/崩潰。

public string Content 
    { 
     get { return Content; } 

比如,你在這裏說,該得到的,回報率的內容,但回報又是獲取內容,並獲取內容,而你明白嗎?永遠在這條線上循環......並且在所有線路上你都有這樣的東西。

試着這樣做。

string inside_Content; 

    public string Content 
    { 
     get { return inside_Content; } 
     set { inside_Content = value;} 
    }