如何在同一方法中访问调用其类的方法的对象?

    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”来访问对象?我不想再添加一个参数。请帮助


守着星空守着你
浏览 114回答 2
2回答

波斯汪

找到了解决方案-我只是没有检查输入到属性的空值。做了检查,它起作用了。
打开App,查看更多内容
随时随地看视频慕课网APP