[Proposal]: Allow / Allowed — Per-method caller whitelisting #9728
-
Proposal: allow/allowed — Per-method caller whitelistinghttps://github.com/philipkalimo/csharp-allow-allowed.git SummaryIntroduce two new access-control constructs for C#:
This allows precise, compile-time enforced encapsulation, reducing accidental misuse of APIs and simplifying code that currently requires internal facades or Motivation
Syntax / ExamplesClass-level trust list: class Weapon allow<Handler, WeaponSystem> {
// Weapon trusts Handler and WeaponSystem for access
} Method-level whitelist: class Weapon allow<Handler> {
allowed<Handler> void Reload() { /* callable only by Handler */ }
allowed<WeaponSystem> void Fire() { /* callable only by WeaponSystem */ }
void InternalCheck() { /* default visibility */ }
} Interface-based allowance: interface IWeaponController { }
class Weapon {
allowed<IWeaponController> void Aim() { }
}
// Any class implementing IWeaponController may call Aim() Group wildcard (optional): group CombatTeam { Handler, AIController }
class Weapon allow<@CombatTeam> {
allowed<@CombatTeam> void Configure() { }
} Semantics
Implementation Notes
Benefits
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
This is already doable in the language. Use attributes to demarcate the types of interest (attributes can be generic, or can use typeof(T) in them). THen, as you say:
You just write an analyzer that checks with whatever rules you care about. |
Beta Was this translation helpful? Give feedback.
-
See all of the discussions on friend accessibility, including #2073, #9261 (this is a near-exact duplicate, just with a different keyword), #9523, #8830, and others. |
Beta Was this translation helpful? Give feedback.
This is already doable in the language. Use attributes to demarcate the types of interest (attributes can be generic, or can use typeof(T) in them). THen, as you say:
You just write an analyzer that checks with whatever rules you care about.