You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Do nothing. Derived classes can't copy property bags in their Init methods.
Make PropertyBagHolder.Propertiesprotected instead of internal.
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.
I was looking at a code base that defines a class
ResultExtendedthat derives from the SARIF SDK'sResultclass, and adds a few more properties. Since theInitmethods in our generated classes areprivate,ResultExtendedhad to supply its own implementation – which repeated all the code inResult.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:… you get the error "ResultExtended.Properties is inaccessible due to its protection level." This is because
Propertiescomes fromPropertyBagHolder, andPropertyBagHolder.Propertiesis declaredinternal. This is intentional, to ensure that clients access property bags through theGet/SetPropertyAPIs.There are a few options:
PropertyBagHolder.Propertiesprotectedinstead ofinternal.Initmethodsprotectedinstead ofprivate. Then a derived class'sInitmethod 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.