Rvne.ReflectiveOptions 2.1.2

dotnet add package Rvne.ReflectiveOptions --version 2.1.2
                    
NuGet\Install-Package Rvne.ReflectiveOptions -Version 2.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Rvne.ReflectiveOptions" Version="2.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rvne.ReflectiveOptions" Version="2.1.2" />
                    
Directory.Packages.props
<PackageReference Include="Rvne.ReflectiveOptions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Rvne.ReflectiveOptions --version 2.1.2
                    
#r "nuget: Rvne.ReflectiveOptions, 2.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Rvne.ReflectiveOptions@2.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Rvne.ReflectiveOptions&version=2.1.2
                    
Install as a Cake Addin
#tool nuget:?package=Rvne.ReflectiveOptions&version=2.1.2
                    
Install as a Cake Tool

Rvne.ReflectiveOptions

ReflectiveOptions builds option objects from attributes on a source type. It is most useful for configuring complex trees of nested nodes (for example, a CLI command tree). It supports:

  • Compile-time attributes that directly apply values to an options instance.
  • Derived attributes that compute values by invoking parameterless methods on a context object.
  • Inheritance-aware attribute discovery.

Targets

  • net8.0
  • net10.0

Demo Examples

There is a small runnable CLI demo that exercises the core scenarios in this README:

  • Demo overview and run instructions: Rvne.ReflectiveOptions.Demo/README.md
  • Annotated CLI + commands: Rvne.ReflectiveOptions.Demo/Program.cs
  • Option types, attributes, and a compact runner: Rvne.ReflectiveOptions.Demo/DemoInfrastructure.cs

Usage

The demo defines two option domains:

  • Runner options on CLI members (CommandRunnerOptions)
  • Descriptions on command types (CommandInfo)

The preferred path is the source generator. It creates both static and derived attributes for every public settable property on an options type.

using System;
using Rvne.ReflectiveOptions.Attributes;

[GenerateReflectiveOptions]
[ApplicableOn(GenerationTarget.Member)]
[Inherited(false)]
public sealed class CommandRunnerOptions
{
    [Alias("Timeout")]
    public int TimeoutMs { get; set; } = 30_000;

    public int Retries { get; set; }

    [Alias("Runnability")]
    public bool Runnable { get; set; } = true;

    public string? Name { get; set; }
}

[GenerateReflectiveOptions]
[ApplicableOn(GenerationTarget.Class)]
[Inherited(false)]
public sealed class CommandInfo
{
    [Alias("Description")]
    public string? Description { get; set; }
}

Use [Ignore] on a property to skip generation for that member. Use [ApplicableOn], [Inherited], and [AllowMultiple] to control the generated AttributeUsage targets, Inherited, and AllowMultiple.

From the CommandRunnerOptions example above, the generator produces:

  • TimeoutAttribute and DerivedTimeoutAttribute
  • RetriesAttribute and DerivedRetriesAttribute
  • RunnabilityAttribute and DerivedRunnabilityAttribute
  • NameAttribute and DerivedNameAttribute

From CommandInfo, it produces DescriptionAttribute and DerivedDescriptionAttribute.

2) Annotate your source type

This is shown end-to-end in the demo CLI: see Rvne.ReflectiveOptions.Demo/Program.cs.

public enum Environment
{
    Local,
    CI
}

public abstract class EnvironmentAwareBase(Environment env)
{
    protected bool IsLocalEnv() => env == Environment.Local;
}

[Description("Root CLI")]
public sealed class Cli(Environment env) : EnvironmentAwareBase(env)
{
    [Retries(2)]
    [Timeout(45_000)]
    [DerivedRunnability(nameof(IsLocalEnv))]
    [Name("push-remote")]
    public PushCommand Push { get; } = new();

    [DerivedTimeout(nameof(GetPullCommandTimeout))]
    [Name("pull-remote")]
    public PullCommand Pull { get; } = new();

    private int GetPullCommandTimeout() => IsLocalEnv() ? 15_000 : 90_000;
}

3) Calculate options

var root = new Cli(Environment.CI);

var rootOptions = root.GetOptions<CommandInfo>();
// rootOptions.Description == "Root CLI"

var pullOptions = root.GetMemberOptions<CommandRunnerOptions>(nameof(Cli.Pull));
// pullOptions.TimeoutMs == 90_000
// pullOptions.Name == "pull-remote"

var pushOptions = root.GetMemberOptions<CommandRunnerOptions>(nameof(Cli.Push));
// pushOptions.Runnable == false (derived from IsLocalEnv in CI)

Derivation rules

  • The derivation method name is provided by the attribute and looked up via reflection.
  • Methods can be instance or static, must be parameterless, and must return a value (non-void).
  • If the method returns null for a non-nullable value type, an exception is thrown.
  • If the method returns null for a non-nullable reference type, an exception is thrown.
  • The returned value must be assignable to the option member referenced by the attribute.

Mixing option domains

You can map different option types to different parts of the same tree. The demo uses:

  • CommandRunnerOptions on CLI members (properties)
  • CommandInfo on command classes
var root = new Cli(Environment.Local);

var pullRunner = root.GetMemberOptions<CommandRunnerOptions>(nameof(Cli.Pull));
var pullInfo = root.Pull.GetOptions<CommandInfo>();

// pullRunner.TimeoutMs comes from member attributes (and derivation on the parent).
// pullInfo.Description comes from attributes on the command type itself.

Member-level attributes

GetMemberOptions applies only the attributes defined on the named member (property/field). It does not read attributes from the member's value type. Use GetOptions on the value itself if you need the value's own attributes applied.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.2 149 1/30/2026
2.1.1 123 1/29/2026
2.0.0 131 1/27/2026
1.1.0 118 1/26/2026
1.0.0 132 1/25/2026