You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support structs with a parameterless constructor in Activator.CreateInstance - #312
Activator.CreateInstance<T>() and new T() now run the parameterless constructor of a struct that declares one, instead of throwing NotSupportedException.
Tests for constructor invocation of structs, generic structs, DateTime, TimeSpan, Guid and string, and for ConstructorInfo.Invoke on an existing object.
Motivation and Context
The exception was there because ConstructorInfo.Invoke did not return a usable boxed struct.
Auto reviews are disabled on base/target branches other than the default branch.
Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Central YAML; Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Advanced
Run ID: 0b51ad0b-7ba3-4fd3-bb93-1142e5dcf141
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
New Features
Reflection can now locate and invoke constructors for structs, classes, built-in value types, and strings, with support for applying constructors to existing objects.
Creating a struct with a parameterless constructor now runs that constructor and returns the initialized value.
Activator now invokes declared parameterless constructors for value types. A new test suite checks constructor lookup and invocation for structs, classes, built-in value types, and strings, including invocation on existing targets.
Activator returns the default value when a value type has no parameterless constructor. When one exists, Activator proceeds to visibility checking and invocation. Tests assert that construction initializes the struct’s X field to 5.
Constructor lookup and returned instances Tests/NFUnitTestSystemLibReflection3/NFUnitTestSystemLibReflection3.nfproj, Tests/NFUnitTestSystemLibReflection3/UnitTestConstructorInvokeTests.cs
The project includes a new test suite. The tests cover constructor lookup and returned instances for structs, generic structs, classes, built-in value types, and strings, along with argument and lookup errors.
Invocation on existing targets Tests/NFUnitTestSystemLibReflection3/UnitTestConstructorInvokeTests.cs
Tests check updates to class targets and reinitialization of boxed structs. They also check exceptions for mismatched target types, null targets, and incorrect argument counts.
Activator.CreateInstance() and new T() now run a struct's declared parameterless constructor instead of throwing NotSupportedException. Tests cover the new behavior. The one thing to confirm before release is that the matching native change lands in nf-interpreter, as the bot comment already requests.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title states the main change: Activator.CreateInstance and new T() now support structs with a parameterless constructor. It matches the changeset in Activator.cs and the new tests. It is 76 charac…
Description check
The description states the change: parameterless constructors of structs now run instead of throwing NotSupportedException. It also lists the added tests for structs, generic structs, DateTime, TimeSp…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title states the main change: Activator.CreateInstance and new T() now support structs with a parameterless constructor. It matches the changeset in Activator.cs and the new tests. It is 76 characters, which exceeds the preferred 50-character length, but the title remains descriptive and has no issue references or trailing full stop.
Full details: Description check
Explanation
The description states the change: parameterless constructors of structs now run instead of throwing NotSupportedException. It also lists the added tests for structs, generic structs, DateTime, TimeSpan, Guid, string, and ConstructorInfo.Invoke. This matches the changeset.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
The native contract hash of nanoFramework.CoreLibrary has changed. The native declaration in nf-interpretermust be updated before this package can be released.
…nstance
- Remove NotSupportedException for structs with a parameterless constructor.
- Add constructor invocation tests.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Activator.CreateInstance<T>()andnew T()now run the parameterless constructor of a struct that declares one, instead of throwingNotSupportedException.DateTime,TimeSpan,Guidandstring, and forConstructorInfo.Invokeon an existing object.Motivation and Context
ConstructorInfo.Invokedid not return a usable boxed struct.How Has This Been Tested?
Screenshots
Types of changes
Checklist