Skip to content

.NET: Support reflection for discovery of resources and scripts in class-based skills#5183

Merged
SergeyMenshykh merged 5 commits intomicrosoft:mainfrom
SergeyMenshykh:use-reflection-for-resources-and-methods-discovery
Apr 10, 2026
Merged

.NET: Support reflection for discovery of resources and scripts in class-based skills#5183
SergeyMenshykh merged 5 commits intomicrosoft:mainfrom
SergeyMenshykh:use-reflection-for-resources-and-methods-discovery

Conversation

@SergeyMenshykh
Copy link
Copy Markdown
Member

@SergeyMenshykh SergeyMenshykh commented Apr 9, 2026

This PR adds support for using reflection to discover resources and scripts in class-based skills, enabling a more declarative approach via attributes.

Changes

  • Add AgentSkillResourceAttribute and AgentSkillScriptAttribute for declarative resource/script discovery
  • Enhance AgentClassSkill to discover resources and scripts via reflection
  • Update samples to demonstrate the new attribute-based approach

@moonbox3 moonbox3 added documentation Improvements or additions to documentation .NET labels Apr 9, 2026
@SergeyMenshykh SergeyMenshykh self-assigned this Apr 9, 2026
@SergeyMenshykh SergeyMenshykh moved this to In Review in Agent Framework Apr 9, 2026
@SergeyMenshykh SergeyMenshykh marked this pull request as ready for review April 9, 2026 11:28
Copilot AI review requested due to automatic review settings April 9, 2026 11:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds attribute-based reflection discovery for resources and scripts in class-based .NET skills, enabling a more declarative model while keeping existing override-based patterns available.

Changes:

  • Introduces [AgentSkillResource] and [AgentSkillScript] attributes for declarative discovery.
  • Updates AgentClassSkill to a CRTP generic (AgentClassSkill<TSelf>) and implements reflection-based discovery + caching.
  • Updates unit tests and samples to use the new generic base class and attribute-based approach (including DI scenarios).

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI/Skills/Programmatic/AgentClassSkill.cs Converts to AgentClassSkill<TSelf>, adds reflection discovery + caching, and adds SerializerOptions support.
dotnet/src/Microsoft.Agents.AI/Skills/Programmatic/AgentSkillResourceAttribute.cs Adds attribute for marking properties/methods as discoverable resources.
dotnet/src/Microsoft.Agents.AI/Skills/Programmatic/AgentSkillScriptAttribute.cs Adds attribute for marking methods as discoverable scripts.
dotnet/src/Microsoft.Agents.AI/Skills/Programmatic/AgentInlineSkillResource.cs Adds MethodInfo-based constructor for reflected resource members.
dotnet/src/Microsoft.Agents.AI/Skills/Programmatic/AgentInlineSkillScript.cs Adds MethodInfo-based constructor for reflected script members.
dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillsProviderBuilder.cs Updates docs to reference AgentClassSkill{TSelf}.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentClassSkillTests.cs Expands coverage for reflection discovery, caching, DI, and serializer options behavior.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentSkillsProviderTests.cs Adjusts tests to accommodate AgentClassSkill<TSelf> and mixed AgentSkill collections.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentSkillResourceAttributeTests.cs Adds unit tests for AgentSkillResourceAttribute constructors.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentSkillScriptAttributeTests.cs Adds unit tests for AgentSkillScriptAttribute constructors.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentInlineSkillResourceTests.cs Adds tests for MethodInfo-based resource behavior.
dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentInlineSkillScriptTests.cs Adds tests for MethodInfo-based script behavior and schema.
dotnet/samples/02-agents/AgentSkills/Agent_Step03_ClassBasedSkills/Program.cs Updates class-based sample to attribute discovery + CRTP base class.
dotnet/samples/02-agents/AgentSkills/Agent_Step03_ClassBasedSkills/README.md Updates documentation to describe attribute discovery usage.
dotnet/samples/02-agents/AgentSkills/Agent_Step03_ClassBasedSkills/Agent_Step03_ClassBasedSkills.csproj Adds IDE warning suppression for attribute-only private members.
dotnet/samples/02-agents/AgentSkills/Agent_Step04_MixedSkills/Program.cs Updates mixed-skills sample to attribute discovery + CRTP base class.
dotnet/samples/02-agents/AgentSkills/Agent_Step04_MixedSkills/Agent_Step04_MixedSkills.csproj Adds IDE warning suppression for attribute-only private members.
dotnet/samples/02-agents/AgentSkills/Agent_Step05_SkillsWithDI/Program.cs Updates DI sample to attribute discovery for resource/script methods.
dotnet/samples/02-agents/AgentSkills/Agent_Step05_SkillsWithDI/Agent_Step05_SkillsWithDI.csproj Adds IDE warning suppression for attribute-only private members.

SergeyMenshykh and others added 2 commits April 9, 2026 12:04
Add discovery-time validation in AgentClassSkill.DiscoverResources() to
fail fast when [AgentSkillResource] is applied to members with incompatible
signatures:

- Reject indexer properties (getter has parameters)
- Reject methods with parameters other than IServiceProvider or
  CancellationToken

Throws InvalidOperationException with actionable error messages instead of
allowing silent runtime failures when ReadAsync invokes the AIFunction with
no named arguments.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Apr 10, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 10, 2026
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Apr 10, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 10, 2026
@SergeyMenshykh SergeyMenshykh merged commit e5f7b9c into microsoft:main Apr 10, 2026
22 checks passed
@github-project-automation github-project-automation bot moved this from In Review to Done in Agent Framework Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants