C#的规范是否阻止从对象的(或结构)初始值设定项构造中调用方法?对象初始化程序中的函数和属性用法
我问的原因是因为我试图使用LINQ-to-XML语句在初始化程序中使用gater数据。这不起作用。但是,如果我在将数据保存到本地变量之前获取数据,那么它的工作没有问题。我只是想知道为什么会发生这种情况,因为我已经找到了我的代码中的错误。
不起作用:
SavedData sData = new SavedData()
{
exportLocation = data.Root.Descendants("ExportLocation").FirstOrDefault().Value,
exportType = (ExportType)data.Root.Descendants("ExportType").FirstOrDefault().Value
};
作品:
var exLoc = data.Root.Descendants("ExportLocation").FirstOrDefault().Value;
ExportType type = (ExportType)data.Root.Descendants("ExportType").FirstOrDefault().Value;
Saved Data sData = new SavedData()
{
exportLocation = exLoc,
exportType = type
};
*你的意思是什么*不起作用*?它编译对我来说很好。 – MarcinJuraszek
它应该工作。如果你告诉我们它不完全正常工作,我们也许可以帮忙。 – fejesjoco
@MarcinJuraszek _在这种情况下不工作_并不意味着它不能编译。该对象不会初始化并等于null。 – JNYRanger