583行是C#8.0版本支持语法,这一句怎么修改可以兼容Unity,当前不兼容Unity.
[项目源码]https://github.com/twcclegg/libphonenumber-csharp/blob/master/csharp/PhoneNumbers/BuildMetadataFromXml.cs
只能這樣吧
if (element.Attribute(LOCAL_ONLY) is { })
{
var localLengths = element.Attribute(LOCAL_ONLY);
//...
}
有其他方法吗 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^:
if (element.Attribute(LOCAL_ONLY) is object)
這樣?
@RosonJ: 这样系统奔了
@Rosiness^:
element.Attribute(LOCAL_ONLY)
的型別是?
is 後直接寫型別吧
@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^:
if (element.Attribute(LOCAL_ONLY) is XAttribute)
{
var localLengths = element.Attribute(LOCAL_ONLY);
//...
}
確定型別正確再進行後面的檢查,你的需求是這個吧
@RosonJ:
@Rosiness^:
錯誤是在366至419行
要看看那裏的代碼才知道問題喔
@RosonJ: 整个项目的类都报错了
@Rosiness^:
截完整的錯誤訊息貼上來看看
@Rosiness^:
我猜你可能缺少什麼引用
@RosonJ: https://q.cnblogs.com/q/133288/ 一个新的博问
var attr=element.Attribute(LOCAL_ONLY)
if (attr){
var v=localLengths.Value;
}
var attr=element.Attribute(LOCAL_ONLY)
if (attr){
var v=attr.Value;
}