In my solution I have a DLL project, say Project1, that owns a class, say class A. This class has some properties, so vital that must only be set in this DLL. Any other projects that references this DLL can only use the getters. So far I have handled the situation by accessing the setters internally, like:
public class A
{
int Num1
{
get;
internal set;
}
int Num2
{
get;
internal set;
}
}
Right now I have to add another project, Project2, to the solution that not only must use class A (both getters and setter) but also, Project1 has a reference to this new project. So I decided to separate class A in a different definition dll, on the top of the hierarcy. That is the point, my problem araises. Now all the setters are invisible to Project1 and Project2. If I remove the internal accessor then setters will be available to any assembly that references the definition dll. I do not want this because the information on the class A is so important, it must not be set mistakenly.
How can I limit the access to the setters of class A from outside of Project1 and Project2?
Only solution comes to my mind is using InternalsVisibleTo. I do not know, it doesn’t sound perfect. Also combining Project1 and Project2 would be another another solution, but these two are responsible for completely different tasks, it is not the best option designwise.
You can keep the internal protector on it, but give another assembly the trust to access the internal members.
You can do this at the class level, or on the entire assembly itself if you want.
InternalsVisibleToAttribute Class