0

I want an specific behaviour to my classes access, and although there are workarounds and I can handle the situation without achieving that specific behaviour, I was finally wondering if its even possible to achieve that in c#.

So I have 3 classes related:

class Foo {
    public FooData fooData;

  }

  class FooData { //holding Foo class serializable part logic
    public Settings settings;


  }
  // needes to be public along with all its members for serialization
  [Serializable]
  public class Settings { 
    public int { get; set; };
    public int { get; set; };
    public int { get; set; };
  }
}

These 3 classes don't necessarily need to be nested, but hold and instance of each recursively (hope that makes sense). By this I mean that Foo holds a field of FooData, and FooData holds a field of Settings.

What I want is an specific access behaviour. This means that I want to have access to the settings properties from the, lets call it "parent class" Foo instance (although there is not inheritance involved for the moment). BUT, that If I handle Settings class instances for serialization topics, I want the the access to the Settings properties to be denied (private behaviour).

Some hopefully understandable pseudo code:

  //With interfaces, I can expose the settings properties, 
  //so that I can get and set them direclty from the Foo instance. Thants fine
  Foo fooInstance = new Foo();
  fooInstance.int1 = 3; 
  int myInt = FooInstance.int1;

  Foo.Settings mySettings = new Settings();
  mySettings.int1; // ERROR. NO ACCESS FROM SETTINGS INSTANCE. I want this to be private, that you 
  //cannot access it from the settings 
  //instance, only from the Foo instance directly.
  fooInstance.Settings = mySettings; //the whole settings instance needs to be get and set as a 
  //whole, BUT settings variables should ONLY be possible to be accessed from Foo instance.

The limitating factor is that Settings class needs to be public, because for serialization topics i need to create instances of that anywhere. Also, al of its properties getters and setters, need to be public also due to the serialization (anywhere access).

My question is if there could be a way, to encapsulate a Settings intance class accessibity to leave it and all of it members public, so that serialization can take place, BUT, to have retricted access to the settings properties (not the whole Settings instance), so that I can get and set them ONLY from the master class Foo. So that:

fooInstance.int1 = 3; //get or set from foo instance IS granted
fooInstance.settings.int1 = 3; //get or set from foo instance NOT granted

I tried many many things with class nesting and interface private properties exposure etc, but according to what I have researched, this is not possible to achieve.

Summary if the cuestion is, although a class along with all its members needs to be public (in my case the settings class), can private behaviour be achieved somewhat towards determined classes or to the outside world except determined classes (serialization clases that need to access)?? (similar to c++ friend class key word, with which you can set accesibily upon your needs would be the need I guess).

The purpose of all this, is that the settings class instance from Foo are changed in runtime, and has a similar name to other class Settings instance that holds the serializable part, only updated when the app saves these settings. Due to this probable similar name confusion on the settings instances manipulation, I was wondering if with the desired access control you can leave the code access behaviour as detailed as possible to avoid the wrong instance access.

I cannot post the real code because its quite huge and holds lots of prone to misunderstanding and offtopic parts. Hope that I made myself understood and question or comments will be appreciated.

One workaround I was thinking about is to create an equal settings class with all its members private, and set this in the foo class with reflection or something like that (Does this make any sense?)

Dispite of the extension and the convoluted of the explanation hope that someone in the community can find the topic interesting enough to get to the end of it, and dicover wether this is possible, if so, how, and if not, to be aware of the c# limitations.

Thanks in advance.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • 1
    Those are fields, not properties. In .NET there's a big difference, especially for serialization. Properties are part of an object's API, fields are just implementation details. Serializers typically serialize properties, not fields – Panagiotis Kanavos Jun 19 '20 at 11:06
  • 1
    Properties can have different accessibilities, eg `public get` and `private set;`. I suspect that if you used properties and perhaps interfaces or base classes the problem would become a *lot* simpler - and easier to understand – Panagiotis Kanavos Jun 19 '20 at 11:07
  • You are right, those are actually properties. I updated that in the question. I am aware of the different get/set accessibilities. The point is that both getters and setters need to be both public for the serialization (read & write), so I set that as a whole with a public field instead of property in the pseudocode by mistake. Thanks for your comments that fix this gap in the question. – rustyBucketBay Jun 19 '20 at 11:12

0 Answers0