我如何重新编写空检查行,因为它不支持统一?

How can i re write the null checking line since it's not supporting in unity?

XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg");
            XNamespace ns = "http://www.w3.org/2000/svg";

            var list = document.Root.Descendants(ns + "rect").Select(e => new {
                Style = e.Attribute("style").Value.Substring(15, 7),
                Transform = e.Attribute("transform")?.Value,
                Width = e.Attribute("width").Value,
                Height = e.Attribute("height").Value,
                X = e.Attribute("x").Value
            });

在 csharp 中它工作正常。 但是统一 visual studio 我在线上遇到错误:

e.Attribute("transform")?.Value.Substring(18, 43)

功能 'null propagating operator' 在 C# 4 中不可用。请使用语言版本 6 或更高版本。

在 csharp 中我不需要改变任何东西。

我在 unity 中使用的 visual studio(与 csharp 相同)是:14.0.24531.01 Update 3 和 visual c# 2015

也许我需要更改检查 null 的行以获得其他内容?

您可以使用三元运算符 ?: 以老式方式完成此操作,并且我会在集合初始值设定项之外获取属性,因此您不必对其进行两次索引:

var list = document.Root.Descendants(ns + "rect").Select(e => 
    var tr = e.Attribute("transform");

    new {Style = e.Attribute("style").Value.Substring(15, 7),
    Transform = (tr != null) ? tr.Value : null,
    Width = e.Attribute("width").Value,
    Height = e.Attribute("height").Value,
    X = e.Attribute("x").Value
});

如果您将 Visual Studio 的版本升级到 2015 或 2017,您可以使用 null 条件运算符。

您已经知道为什么不能使用 ?.,那是因为 Unity 不支持支持 ?..

的 C# 版本

UnholySheep 评论建议使用 if 语句,但我认为您不能在这里使用它。

您可以使用三元运算符检查 null

使用:

Transform = e.Attribute("transform") != null ? e.Attribute("transform").Value : "",

如果你还一头雾水。这是完整的代码:

XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg");
XNamespace ns = "http://www.w3.org/2000/svg";

var list = document.Root.Descendants(ns + "rect").Select(e => new
{
    Style = e.Attribute("style").Value.Substring(15, 7),
    Transform = e.Attribute("transform") != null ? e.Attribute("transform").Value : "",
    Width = e.Attribute("width").Value,
    Height = e.Attribute("height").Value,
    X = e.Attribute("x").Value
});