New access modifier for class/struct members restricts access to a file or namespace #9341
Replies: 3 comments 2 replies
-
I don't know what this means. 'file' is explicitly for this exact purpose. Symbols that you want compiled into your project, but which cannot be accessed outside of the file. |
Beta Was this translation helpful? Give feedback.
-
Isn't there a way to create a C# project which can be referenced by both WFP and UWP etc.? I feel like proper structuring of project dependencies is a more robust solution than the whole file links, playing with file access, all that. |
Beta Was this translation helpful? Give feedback.
-
I like the way I have described. The helper code is often not too big to create a separate DLL. If it contains more than one class and they depend on each other, ‘internal’ is used. But these internal members should often only be accessible within their own file or namespace. The following example only illustrates the scheme, but there are better samples. Sample 1: HelperFile1.cs:
//----------------------------------------------
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes I write some useful helper code which I can use in different projects (e.g. WPF, UWP). Then I don't create a DLL for that bit of code, but I add the C# file as a link into my project, no matter what kind. However, the code itself can often have more than one class and contain members (methods, properties) with the access modifier 'internal'. The code in the file has its own namespace.
When I use this code in a project, Intellisense shows me all accessible members, including the members with the keyword 'internal'. However, I want to prohibit access to the internal members of the helper code, as this will most likely lead to unexpected behavior.
So I see 2 possibilities, which are my suggestions:
There is the existing access modifier 'file'. But it is only allowed for a whole class. If 'file' could be used for individual members instead of a whole class, this would solve my problem.
Instead of using the keyword 'internal', a new keyword could be introduced that is also an access modifier and makes the desired members of the helper classes accessible only within their own namespace.
In both cases, Intellisense would no longer show me the members of classes that I had previously defined as 'internal', and the compiler would have a similar behavior.
Probably the keyword 'file' would currently help to solve the problem somehow, but it would unnecessarily inflate the helper code.
Beta Was this translation helpful? Give feedback.
All reactions