XamlX
XamlX copied to clipboard
PR to fix issue #14370 in Avalonia project (2 of 2)
The AvaloniaNameGenerator
uses the TypeReferenceResolver
when parsing markup files.
During parsing, when an element is found having an x:TypeArguments
attribute and a property element, when the property element is visited the TypeReferenceResolver
throws XamlTransformException: Unable to resolve type from namespace.
At the time of the exception, the declaring type for the property is what the TypeReferenceResolver is trying to resolve.
A property element is not allowed to have an x:TypeArguments
attribute, so the TypeReferenceResolver
naively tries to resolve the declaring class of the property with no type arguments, even when the declaring type on the property element and the type on the node above which has the x:TypeArguments
are exactly the same name. This leads to the type not being found and the exception thrown.
How was the solution implemented (if it's not obvious)?
If the properties inside a tag are owned by exactly the same class (by name), use them for the child node.
Fixed issues
https://github.com/AvaloniaUI/Avalonia/issues/14370
Thank you! Looks good to me in general. So, error was on deeper level than just Name Generator.
I think alternative solution would be to check if element property type is the same as parent node type. If so, then just reuse type
instance instead of creating new XamlAstXmlTypeReference
.
Something like:
var declaringType = pair[0] == type.Name && elementNode.Name.NamespaceName == type.XmlNameSpace
? type : new XamlAstXmlTypeReference(el.AsLi(), elementNode.Name.NamespaceName, pair[0], parentGenericArguments);
var propertyReference = new XamlAstNamePropertyReference(el.AsLi(), declaringType, pair[1], type));
// And use propertyReference in the XamlAstXamlPropertyValueNode
Just in case if declaring type is different and might not have generic types. Could be the case with attached properties, I guess?
Either way, some tests would really help. Something like here https://github.com/kekekeks/XamlX/blob/master/tests/XamlParserTests/BasicCompilerTests.cs.
This fix has been optimized down to one line of code, a unit test has been added, and all tests pass.
@busitech thank you! Looks good. Pinging @kekekeks to get it merged.