C# 的规范是否阻止从对象(或结构)的初始化程序构造中调用方法?
我问的原因是因为我试图使用 LINQ-to-XML 语句在初始化程序中使用门数据。这不起作用。但是,如果我事先将数据保存到局部变量中,则它可以正常工作。我只是想知道为什么会发生这种情况,因为我已经发现了我的代码中的错误。
不工作:
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
};