C # set accessor is available for all types in the assembly and gets evaluated only for derivative types. How?

This property is in a type with a modifier without access (thus internal access):

 class SomeType { private int length; internal int Length { get { return length; } set length = value; } } } 

allows you to use all types inside the SomeType assembly to use get and set accessors. Problem : how to restrict access to set to only types obtained from SomeType (and SomeType)?

 internal int Length { get { return length; } protected set length = value; } } 

rejected by the compiler because protected is considered less restrictive than internal (presumably: protected has an intersection with internal but is not fully included in internal Derived types may exist outside the scope of internal ).

What will be the code for accessing any type inside the assembly and defined only by derivative types in the assembly?

Edit : after reviewing the answers, it seems to me that I need to add one more property characteristic, as this may matter in the solution: the property type is actually SomeType . Modified Code:

 class SomeType { private SomeType length; internal SomeType Length { get { return length; } set length = value; } } } 

If the property is declared public , the compiler throws an error (the type of the SomeType property is less accessible for the Length property).

+4
source share
3 answers

(EDIT: I just checked, and it works even when the property type matches the declaration type. However, it does not work when you try to declare a property in a public type where the property type is internal.)

You cannot do this in C # (strictly speaking), but you can do something very similar:

 protected internal int Length { get; protected set; } 

(for simplicity, an automatically implemented property is used, just as for a regular property).

This will make a getter available for any type within the same assembly and derived types; "setter" will be available only for derived types. Since your class is internal, one way or another, this is pretty much equivalent anyway - the getter will theoretically be available for types outside the assembly, but since the class is internal, nothing from the other assembly should come from your type anyway.

The problem is that properties require one access level to be a "subset" of another; internal and protected do not work like this: one type can be in the same assembly, but not obtained from the corresponding type; another type can be obtained from it, but in a different assembly. They are orthogonal, basically.

The above solution works because protected internal means that it is available to any type that is either in the same assembly or derived from that type. It is clear that each of protected and internal individually is a subset of this.

You could create an internal property that was further restricted to the installer if C # had some equivalent level of access to the CLR family and assembly. ( protected internal equivalent to "family or assembly".) Unfortunately for you this is not so: (

If you really want the goals originally stated (for example, if you have a public class to which you want to apply the same restrictions), you will have to make at least one of them a separate method, for example

 private int length; internal int Length { get { return length; } } protected void SetLength(int value) { this.length = value; } 
+4
source

Since the class itself is only available in the declaring assembly (due to the implicit access modifier internal ), just create a getter in the public property and the protected installer:

 class SomeType { private int length; public int Length { get { return length; } protected set { length = value; } } } 

A getter will not be available outside of your assembly, since the class itself is not displayed.


Disable topic: if you have a recent C # compiler, you can use automatic properties instead:

 class SomeType { public int Length { get; protected set; } } 

This is only a language / compiler trick, so you do not need to compile it using the 3.X environment to use it.

+3
source

Unable to rotate it (not tested):

 protected int Length { internal get { return length; } set { length = value; } } 
-one
source

All Articles