首页 新闻 会员 周边

C#语句修改

0
悬赏园豆:200 [已解决问题] 解决于 2021-01-19 17:46

583行是C#8.0版本支持语法,这一句怎么修改可以兼容Unity,当前不兼容Unity.
[项目源码]https://github.com/twcclegg/libphonenumber-csharp/blob/master/csharp/PhoneNumbers/BuildMetadataFromXml.cs

Rosiness^的主页 Rosiness^ | 初学一级 | 园豆:47
提问于:2021-01-18 14:45
< >
分享
最佳答案
0

只能這樣吧

if (element.Attribute(LOCAL_ONLY) is { })
{
    var localLengths = element.Attribute(LOCAL_ONLY);
    //...
}
收获园豆:160
RosonJ | 老鸟四级 |园豆:4910 | 2021-01-18 15:14

有其他方法吗 Unity 还是报错

    private static void PopulatePossibleLengthSets(IEnumerable<XElement> possibleLengths, SortedSet<int> lengths,
            SortedSet<int> localOnlyLengths)
        {
            foreach (var element in possibleLengths)
            {
                var nationalLengths = element.GetAttribute(NATIONAL);
                // We don't add to the phone metadata yet, since we want to sort length elements found under
                // different nodes first, make sure there are no duplicates between them and that the
                // localOnly lengths don't overlap with the others.
                var thisElementLengths = ParsePossibleLengthStringToSet(nationalLengths);
                //  if (element.Attribute(LOCAL_ONLY) is { } localLengths)
                if (element.Attribute(LOCAL_ONLY) is { })

                {
                    var localLengths = element.Attribute(LOCAL_ONLY); 
                    var thisElementLocalOnlyLengths = ParsePossibleLengthStringToSet(localLengths.Value);
                    if (thisElementLengths.Overlaps(thisElementLocalOnlyLengths))
                        throw new Exception(
                            $"Possible length(s) found specified as a normal and local-only length: {thisElementLengths.Intersect(thisElementLocalOnlyLengths)}");
                    // We check again when we set these lengths on the metadata itself in setPossibleLengths
                    // that the elements in localOnly are not also in lengths. For e.g. the generalDesc, it
                    // might have a local-only length for one type that is a normal length for another type. We
                    // don't consider this an error, but we do want to remove the local-only lengths.
                    foreach (var length in thisElementLocalOnlyLengths)
                        localOnlyLengths.Add(length);
                }
                // It is okay if at this time we have duplicates, because the same length might be possible
                // for e.g. fixed-line and for mobile numbers, and this method operates potentially on
                // multiple phoneNumberDesc XML elements.
                foreach (var length in thisElementLengths)
                    lengths.Add(length);
            }
        }

Rosiness^ | 园豆:47 (初学一级) | 2021-01-18 15:22

@Rosiness^:

if (element.Attribute(LOCAL_ONLY) is object)

這樣?

RosonJ | 园豆:4910 (老鸟四级) | 2021-01-18 15:28

@RosonJ: 这样系统奔了

Rosiness^ | 园豆:47 (初学一级) | 2021-01-18 15:31

@Rosiness^:

element.Attribute(LOCAL_ONLY)

的型別是?
is 後直接寫型別吧

RosonJ | 园豆:4910 (老鸟四级) | 2021-01-18 15:33

@RosonJ: 官方源码这样写的 没有见过这么用的

官方源码

private static void PopulatePossibleLengthSets(IEnumerable<XElement> possibleLengths, SortedSet<int> lengths,
            SortedSet<int> localOnlyLengths)
        {
            foreach (var element in possibleLengths)
            {
                var nationalLengths = element.GetAttribute(NATIONAL);
                // We don't add to the phone metadata yet, since we want to sort length elements found under
                // different nodes first, make sure there are no duplicates between them and that the
                // localOnly lengths don't overlap with the others.
                var thisElementLengths = ParsePossibleLengthStringToSet(nationalLengths);
                if (element.Attribute(LOCAL_ONLY) is { } localLengths) 
                {
                    var thisElementLocalOnlyLengths = ParsePossibleLengthStringToSet(localLengths.Value);
                    if (thisElementLengths.Overlaps(thisElementLocalOnlyLengths))
                        throw new Exception(
                            $"Possible length(s) found specified as a normal and local-only length: {thisElementLengths.Intersect(thisElementLocalOnlyLengths)}");
                    // We check again when we set these lengths on the metadata itself in setPossibleLengths
                    // that the elements in localOnly are not also in lengths. For e.g. the generalDesc, it
                    // might have a local-only length for one type that is a normal length for another type. We
                    // don't consider this an error, but we do want to remove the local-only lengths.
                    foreach (var length in thisElementLocalOnlyLengths)
                        localOnlyLengths.Add(length);
                }
                // It is okay if at this time we have duplicates, because the same length might be possible
                // for e.g. fixed-line and for mobile numbers, and this method operates potentially on
                // multiple phoneNumberDesc XML elements.
                foreach (var length in thisElementLengths)
                    lengths.Add(length);
            }
        }
Rosiness^ | 园豆:47 (初学一级) | 2021-01-18 15:38

@Rosiness^:

if (element.Attribute(LOCAL_ONLY) is XAttribute)
{
    var localLengths = element.Attribute(LOCAL_ONLY);
    //...
}

確定型別正確再進行後面的檢查,你的需求是這個吧

RosonJ | 园豆:4910 (老鸟四级) | 2021-01-18 15:42

@RosonJ:

Rosiness^ | 园豆:47 (初学一级) | 2021-01-18 15:50

@Rosiness^:
錯誤是在366至419行
要看看那裏的代碼才知道問題喔

RosonJ | 园豆:4910 (老鸟四级) | 2021-01-18 15:52

@RosonJ: 整个项目的类都报错了

Rosiness^ | 园豆:47 (初学一级) | 2021-01-18 15:55

@Rosiness^:
截完整的錯誤訊息貼上來看看

RosonJ | 园豆:4910 (老鸟四级) | 2021-01-18 15:56

@Rosiness^:
我猜你可能缺少什麼引用

RosonJ | 园豆:4910 (老鸟四级) | 2021-01-18 15:59

@RosonJ: https://q.cnblogs.com/q/133288/ 一个新的博问

Rosiness^ | 园豆:47 (初学一级) | 2021-01-26 16:54
其他回答(1)
0

var attr=element.Attribute(LOCAL_ONLY)
if (attr){
var v=localLengths.Value;
}

收获园豆:40
为乐而来 | 园豆:1432 (小虾三级) | 2021-01-18 15:38

支持(0) 反对(0) Rosiness^ | 园豆:47 (初学一级) | 2021-01-18 15:49

var attr=element.Attribute(LOCAL_ONLY)
if (attr){
var v=attr.Value;
}

支持(0) 反对(0) 为乐而来 | 园豆:1432 (小虾三级) | 2021-01-19 14:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册