Skip to content

Support structs with a parameterless constructor in Activator.CreateInstance - #312

Merged
josesimoes merged 2 commits into
nanoframework:developfrom
josesimoes:constructor-invoke-tests
Nov 24, 2026
Merged

josesimoes merged 2 commits into
nanoframework:developfrom
josesimoes:constructor-invoke-tests

Conversation

@josesimoes

@josesimoes josesimoes commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Description

  • 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

How Has This Been Tested?

  • [tested against nanoclr BuildId 68577]

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Unit Tests (add new Unit Test(s) or improved existing one(s), has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).
  • I have added new tests to cover my changes.

@nfbot nfbot left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ The PR template looks good now. Thanks!

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

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.

Changes: Constructor Invocation

Layer / File(s) Summary
Activator value-type constructors
nanoFramework.CoreLibrary/System/Activator.cs, Tests/NFUnitTestSystemLibReflection3/UnitTestActivatorTests.cs
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.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: Minimal · previous review 1381b

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

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: d7256b33-7f3d-42d0-8c88-a3099cc82f7c
Commits

Reviewing files that changed from the base of the PR and between 6473369 and 1381bda.

Files selected for processing: 4

  • Tests/NFUnitTestSystemLibReflection3/NFUnitTestSystemLibReflection3.nfproj
  • Tests/NFUnitTestSystemLibReflection3/UnitTestActivatorTests.cs
  • Tests/NFUnitTestSystemLibReflection3/UnitTestConstructorInvokeTests.cs
  • nanoFramework.CoreLibrary/System/Activator.cs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@nfbot

nfbot commented Nov 24, 2026

Copy link
Copy Markdown
Member

⚠️ Native declaration update required!

The native contract hash of nanoFramework.CoreLibrary has changed. The native declaration in nf-interpreter must be updated before this package can be released.

Previously published This build
Native contract hash 0x940E6E23 0x272B0DC7

👉 Tracking issue: nanoframework/Home#1957

The issue contains a link to the stubs artifact from this build and instructions for GitHub Copilot to perform the update.

…nstance

- Remove NotSupportedException for structs with a parameterless constructor.
- Add constructor invocation tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@josesimoes
josesimoes force-pushed the constructor-invoke-tests branch from cc1cb05 to 1381bda Compare November 24, 2026 14:22
@josesimoes

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@sonarqubecloud

Copy link
Copy Markdown

@josesimoes

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

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.

@josesimoes
josesimoes merged commit 9785212 into nanoframework:develop Nov 24, 2026
2 of 4 checks passed
@josesimoes
josesimoes deleted the constructor-invoke-tests branch November 24, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants