当我执行下面的代码saveXML时会产生上面的错误,为什么??
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();
}
}
答案 0 :(得分:3)
所有变量都是循环引用,它永远循环,最终系统停止/崩溃。
public string Content
{
get { return Content; }
例如,你在这里说,获取,返回内容,但返回的是获取内容,获取内容,你明白了吗?在这一行中永远是循环...并且在所有行中你都有类似的东西。
尝试这样做。
string inside_Content;
public string Content
{
get { return inside_Content; }
set { inside_Content = value;}
}