public class Container : XmlElementGenerator
{
public string projectcode { get; set; }
public string projectname { get; set; }
public string projectleader { get; set; }
public string barcode { get; set; }
public string sampleartid { get; set; }
public string sampledescription { get; set; }
public string remark { get; set; }
public DateTime samplingdate { get; set; }
public int samplenumber { get; set; }
public XmlElement GenerateXMLElement(XmlDocument xmlDocument)
{
Type containerGenerator = typeof(Container);
XmlElement container = ElementChildrenGenerator(xmlDocument, "container");
foreach (PropertyInfo prop in containerGenerator.GetProperties())
{
container.AppendChild(ElementChildrenGenerator(xmlDocument, prop.Name, prop.GetValue(this).ToString()));
}
return container;
}
}
这里prop.GetValue(this)
上的错误 - “System.NullReferenceException:对象引用未设置为对象的实例”。为什么我不能使用“this”来访问该对象?我不想再添加一个参数。请帮忙
答案 0 :(得分:1)
试试这个:
public class Container : XmlElementGenerator
{
public string projectcode { get; set; }
public string projectname { get; set; }
public string projectleader { get; set; }
public string barcode { get; set; }
public string sampleartid { get; set; }
public string sampledescription { get; set; }
public string remark { get; set; }
public DateTime samplingdate { get; set; }
public int samplenumber { get; set; }
public XmlElement GenerateXMLElement(XmlDocument xmlDocument)
{
XmlElement container = ElementChildrenGenerator(xmlDocument,"container");
foreach (PropertyInfo prop in this.GetType().GetProperties())
{
container.AppendChild(ElementChildrenGenerator(xmlDocument, prop.Name, prop.GetValue(this).ToString()));
}
return container;
}
}
答案 1 :(得分:0)
找到一个解决方案 - 我只是没有检查属性的null值输入。检查是否有效。