{"id":19252736,"url":"https://github.com/tomaszrewak/privatest","last_synced_at":"2026-05-12T12:37:07.785Z","repository":{"id":125409506,"uuid":"336616191","full_name":"TomaszRewak/Privatest","owner":"TomaszRewak","description":"Adding instance-level accessibility mode to C#","archived":false,"fork":false,"pushed_at":"2021-04-17T21:56:00.000Z","size":97,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-05T07:13:23.494Z","etag":null,"topics":["accessibility","analyzer","compilation","csharp","roslyn","roslyn-analyzer","roslyn-codefix","scala"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomaszRewak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-06T19:13:27.000Z","updated_at":"2023-02-25T22:43:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"a5172d7d-f408-435c-8764-20b6d140da13","html_url":"https://github.com/TomaszRewak/Privatest","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomaszRewak%2FPrivatest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomaszRewak%2FPrivatest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomaszRewak%2FPrivatest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomaszRewak%2FPrivatest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomaszRewak","download_url":"https://codeload.github.com/TomaszRewak/Privatest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240347947,"owners_count":19787236,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["accessibility","analyzer","compilation","csharp","roslyn","roslyn-analyzer","roslyn-codefix","scala"],"created_at":"2024-11-09T18:28:14.942Z","updated_at":"2026-05-12T12:37:07.755Z","avatar_url":"https://github.com/TomaszRewak.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Privatest\n\nPrivatest is a code analyzer that adds instance-level accessibility mode to C#.\n\n### Setup\n\nTo set it up simply install the `Privatest` Nuget package in your project:\n\n```\ndotnet add package Privatest\n```\n\n### Usage\n\nTo assign an instance-level accessibility mode to a member of a class, decorate it with the `[This]` attribute.\n\n```csharp\npublic class Class\n{\n   [This] private int _field;\n\n   public void Method(Class other)\n   {\n      _field = 10; // OK\n\n      other._field = 10; // ERROR\n    //^^^^^^^^^^^^ `_field` is inaccessible due to its protection level. It can only be accessed by the instance to which it belongs (through the `this` reference).\n   }\n}\n```\n\nYou can apply the `[This]` attribute on fields, methods and properties (including property getters and setters).\n\nThe `[This]` attribute can only be applied on private members.\n\n```csharp\npublic class Class\n{\n   [This] public int Property { get; set; }\n                   //^^^^^^^^ [This] attribute can be applied only on private members, but was applied on 'Public' member 'Property'\n}\n```\n\nThe `[This]` attribute can also be used to limit the accessibility of a member to a specific scope only. It can be useful when working with backing fields, which should be accessed only by the associated properties.\n\n```csharp\npublic class Class\n{\n   [This(nameof(FrontProperty))] private int _backField;\n   public int FrontProperty\n   {\n      get =\u003e _backField;\n      set =\u003e _backField = value;\n   }\n\n   public void Method(Class other)\n   {\n      _backField = 2;\n    //^^^^^^^^^^ `_backField` is inaccessible due to its protection level. It can only be accessed in `FrontProperty` (but is used in `Method`).\n   }\n}\n```\n\nThe scope can be limited to any method or property of a class. It can by specified either by using the `nameof` operator or as a plain string value.\n\nIn case of backing fields that have the same name as their front properties (just starting with a lower case letter and/or `_` character), one can also use the `[BackingField]` attribute which will work the same way as a longer `[This(nameof(FrontPropertyName))]`.\n\n```csharp\npublic class Class\n{\n   [BackingField] private int _name;\n   public int Name\n   {\n      get =\u003e _name;\n      set =\u003e _name = value;\n   }\n   \n   public int Surname\n   {\n      get =\u003e _name;\n           //^^^^^ `_name` is inaccessible due to its protection level. It can only be accessed in `Name` (but is used in `Surname`).\n   }\n}\n```\n\n### How does it work?\n\nPrivatest is a C# code analyzer that performs accessibility checks during the compilation stage only (using Roslyn hooks). It does not introduce any additional checks in the compiled code and therefore does not impact its performance.\n\n## Contributions\n\nI am open to PRs as well as github issues.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomaszrewak%2Fprivatest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomaszrewak%2Fprivatest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomaszrewak%2Fprivatest/lists"}