Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CategoryOrderAttribute not working as intended #842

Open
xceedsoftware opened this issue Jul 13, 2017 · 3 comments
Open

CategoryOrderAttribute not working as intended #842

xceedsoftware opened this issue Jul 13, 2017 · 3 comments
Labels

Comments

@xceedsoftware
Copy link
Owner

arobincaron[CodePlex]
The property grid ObjectContainerHelper class uses Type.GetCustomAttributes() to get the CategoryOrderAttribute instead of using TypeDescriptor.GetAttributes(). This is inconsistent with the way these types of attributes are retreived for the rest of property
grid and prevents adding this metadata attribute for classes that lack it.

In Xceed.Wpf.Toolkit\PropertyGrid\Implementation\ObjectContainerHelper.cs around line 109 I changed the following code:
CategoryOrderAttribute[] orderAttributes = ( selectedObject != null )
? ( CategoryOrderAttribute[] )selectedObject.GetType().GetCustomAttributes( typeof( CategoryOrderAttribute ), true )
: new CategoryOrderAttribute[ 0 ];

to
CategoryOrderAttribute[] orderAttributes = (selectedObject != null)
? TypeDescriptor.GetAttributes(selectedObject.GetType()).OfTypeltCategoryOrderAttributegt().ToArray()
: new CategoryOrderAttribute[0];

@xceedsoftware
Copy link
Owner Author

BoucherS[CodePlex]
Hi,
According to MSDN, when using TypeDescriptor.GetAttributes :

For attributes with AttributeUsageAttribute.AllowMultiple set to true, the attribute collection removes duplicate instances.

This means that using TypeDescriptor.GetAttributes instead of Type.GetCustomAttributes() will remove dupplicates of attributes. In the following example, only the first attribute will be considered, while they should all be considered.
[CategoryOrder( Information, 1 )]
[CategoryOrder( Hobbies, 2 )]
[CategoryOrder( Connections, 3 )]
public class PersonOrdered : PersonBase { }
So the current code is kept.

@MarqueIV
Copy link

Boucher,

As per your comment, the reason duplicates are removed is because CategoryOrderAttribute is missing the TypeId override, thus all instances of CategoryOrderAttribute share the same TypeId and as such appear as duplicates to TypeDescriptor.GetAttributes.

I added a new PR that both adds in the missing TypeId override to CategoryOrderAttribute (using CategoryValue for uniqueness since you shouldn't have more than one per CategoryValue) as well as it updates the code to properly use TypeDescriptor.GetAttributes.

With this change, using your above example data, all three attributes are now properly returned even when using TypeDescriptor.GetAttributes, and unlike Type.GetCustomAttributes, it now also returns those dynamically added at runtime.

For more information, see the 'Remarks' section on MSDN here... https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.typedescriptor.getattributes

Here's the PR with the correct fixes... #1523

@XceedBoucherS
Copy link
Collaborator

Thank you for bringing this to our attention.
The fix will be included in v3.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants