Fix InvalidCastException in GetBrowseName methods for Attributes, DataTypes, ReferenceTypes, and StatusCodes#3242
Conversation
|
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3242 +/- ##
==========================================
+ Coverage 57.68% 57.75% +0.07%
==========================================
Files 361 361
Lines 79163 79199 +36
Branches 13821 13826 +5
==========================================
+ Hits 45665 45745 +80
+ Misses 29275 29220 -55
- Partials 4223 4234 +11 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: marcschier <[email protected]>
…d DataGenerator Co-authored-by: marcschier <[email protected]>
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Thanks for fixing this! When do you plan to publish an updated NuGet package? |
|
@iconics-janb It will take some more weeks at least |
|
I’ve noticed that in the latest NuGet release (1.5.377.22), the fix still hasn’t been included. |
|
@heidva there will be a new Release to nuget in the coming days (still labeled Preview) that includes the fix. In the meantime you could use the Preview nuget Feed linked in the Main readme. |
|
@heidva 1.5.378-preview is released |
Description
Fixes #2434 - Resolves
InvalidCastExceptionwhen callingGetBrowseName()methods on various constant classes.Problem
The
GetBrowseName()methods inAttributes,DataTypes,ReferenceTypes, andStatusCodesclasses were throwingInvalidCastExceptionwhen retrieving field values via reflection:Root Cause: When using reflection to retrieve
const uintfield values,field.GetValue()returns a boxed object. For values within theintrange, the value is boxed asintrather thanuint. Direct casting(uint)field.GetValue()throws an exception because you cannot directly cast a boxedinttouint.Solution
Use
Convert.ToUInt32(): Replace direct casts withConvert.ToUInt32(field.GetValue(...), CultureInfo.InvariantCulture)to properly handle the boxed value conversion.Add type filtering: Include
if (field.FieldType == typeof(uint))checks to only process uint fields, avoiding potential issues with other static fields.Code style compliance: Split long lines to meet the 160-character limit.
Changes
Fixed Classes
Attributes.Helpers.cs- Fixeds_attributesIdToNamedictionary initializationDataTypes.Helpers.cs- Fixeds_dataTypeIdToNamedictionary initializationReferenceTypes.Helpers.cs- Fixeds_referenceTypeIdToNamedictionary initializationStatusCodes.Helpers.cs- Fixed boths_statusCodeToSymbolands_utf8BrowseNamesdictionary initializationsUtils.cs- Fixed genericGetIdentifier()helper methodDataGenerator.cs- Fixed StatusCode field reflectionTests Added
Added comprehensive unit tests covering all fixed methods:
AttributesTests.cs- 7 tests for Attributes classDataTypesTests.cs- 7 tests for DataTypes classReferenceTypesTests.cs- 7 tests for ReferenceTypes classStatusCodesTests.cs- 10 tests for StatusCodes class (including UTF8 encoding)Total: 31 new tests, all passing
Verification
✅ The exact scenario from the issue now works correctly
✅ All 31 new unit tests pass
✅ All 24,571 existing core tests pass (no regressions)
✅ Tested on .NET 8.0 as specified in the issue
Example Usage
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.