How can I prevent someone from holding a reference to an object in C #

I have a class like this

internal class  Report
{
    public Company TargetCompany { get; private set; }

    .... // other stuff
}

      

I don't want anyone to do this

Report r = GetReport();

Company c = r.TargetCompany;

      

but instead always use

r.TargetCompany 

      

when they want to access the Company variable.

Is it possible? Does it even make sense?

+2


a source to share


3 answers


You can expose a proxy interface in the report that invokes the equivalent interface in the company, and make the company private so that you have complete control (even encapsulate) access to the company object.



+3


a source


No, It is Immpossible.



And no, that doesn't make sense either. Maybe if you explain your reasoning, we will understand a little better.

+4


a source


No, It is Immpossible. The closest you can do is give the wrappers only access to the members in the TargetCompany (not the company itself), or return a copy of your internal TargetCompany member without allowing the internal member to be referenced (which doesn't really matter).

+4


a source







All Articles