Skip to content

Compiler allows setting a value of a private property on attribute #18571

Closed
@ForNeVeR

Description

@ForNeVeR

Compiler allows to "set" a value of a property with private setter on an attribute. This doesn't work in runtime and isn't allowed by the C# compiler, even though it has some representation in the assembly metadata.

Repro steps

  1. Create a new F# project on .NET 9.0.300 SDK
  2. Install package CommandLineParser v2.9.1
  3. Write this:
    [<CommandLine.Verb("start", HelpText = "Start the game", IsDefault = true)>]
    type StartGame() = class end

Here's a ZIP archive with the project: ConsoleApp19.zip

Expected behavior

The code should not compile. Inspect the VerbAttribute and note that it has a constructor parameter named isDefault (starting from lowercase) that's accessible, and a property (starting from a capital letter) IsDefault that has a private setter.

See this code: https://github.com/commandlineparser/commandline/blob/1e3607b97af6141743edb3c434c06d5b492f6fb3/src/CommandLine/VerbAttribute.cs#L67-L70

public bool IsDefault { get; private set; }

A corresponding C# program doesn't compile:

using CommandLine;

Console.WriteLine("Hello, World!");

[Verb("foo", IsDefault = true)]
class Foo ;

Error CS0617 : 'IsDefault' is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static.

Actual behavior

F# compiler happily compiles the code above, and as far as I see this assignment has no effect in runtime, even though it (surprisingly) gets compiled to some metadata.

Known workarounds

Do not use the erroneous construction in F# code. In the particular case, use the (valid) isDefault = true instead of IsDefault = true.

Related information

Provide any related information (optional):

  • Operating system: Windows
  • .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET SDK 9.0.300
  • Editing Tools (e.g. Visual Studio Version, Visual Studio): doesn't matter, happens in console compiler as well

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.good first issuehelp wanted

Type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions