Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing the accessibility of a Class in C Sharp

I am developing software and I need to change the accessibility of a class swiftly. The main reason for this is that during the testing process, the class must be public to be seen from the test module, in the production, the class should be internal. Here is a sample:

public interface ILdapService : IDisposable
{
// It will be internal in production. (TODO)
// YAP1 It will be public in the test. ()

/// <summary>
///  
/// </summary>
/// <param name="email">Ldap user name without domain name</param>
/// <param name="password">Ldap passsword.</param>
bool Verify(string email, string password);
}

I am handling the issue through the Visual Studio editor tags, however, it increases the time needed to build the software. I am using the preprocessor tags for conditional build and change can be done swiftly, but changing accessibility is a problem. How can I do this? Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The main reason for this is that during the testing process, the class must be public to be seen from the test module, in the production, the class should be internal.

Sounds like what you really want is to make it internal, but use InternalsVisibleToAttribute to still have access to it from the test project.

Just apply [assembly:InternalsVisibleTo("YourTestProject")] in your production project and you should be fine. (In my experience this is the most common reason for using InternalsVisibleTo. There are plenty of people who say you shouldn’t test anything that isn’t actually public, but personally I find testing internal classes to be very pragmatic.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading