Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

Allow generated Init methods to be protected #120

Description

I was looking at a code base that defines a class ResultExtended that derives from the SARIF SDK's Result class, and adds a few more properties. Since the Init methods in our generated classes are private, ResultExtended had to supply its own implementation – which repeated all the code in Result.Init, and then add the code to initialize their extra properties.

I noticed that their copied initialization code did not initialize the property bag. The reason is that they can’t: if you add this code to ResultExtended.Init:

if (properties != null) 
{ 
    Properties = new Dictionary<string, SerializedPropertyInfo>(properties); 
} 

… you get the error "ResultExtended.Properties is inaccessible due to its protection level." This is because Properties comes from PropertyBagHolder, and PropertyBagHolder.Properties is declared internal. This is intentional, to ensure that clients access property bags through the Get/SetProperty APIs.

There are a few options:

  1. Do nothing. Derived classes can't copy property bags in their Init methods.
  2. Make PropertyBagHolder.Properties protected instead of internal.
  3. Make the Init methods protected instead of private. Then a derived class's Init method can call the base class method (in addition to initializing the properties defined in the derived class), which copies the property bag without breaking encapsulation.

Michael C. Fanning (@michaelcfanning) agrees that #3 is the right course.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions