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
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 to share