Reputation: 113956
I have a class library with some public classes, and many internal utility classes. These utilities should not be visible/usable to people who link against the DLL.
namespace MyProject {
public class PublicClass { }
}
namespace MyProject {
internal class UtilClass { }
}
When I do this, VS triggers errors such as "UtilClass is less accessible than PublicClass.Member"
Am I doing something wrong? How should I be doing this? I've seen libraries with private classes that cannot be used by me. How do I get this behavior?
Thanks.
Upvotes: 2
Views: 6539
Reputation: 13177
I suspect you are not showing us the full code as what you have provided should work.
You must have either a public method or property that returns an instance of your internal class E.G.
namespace MyProject
{
public class PublicClass
{
public UtilClass DoSomething() {}
public UtilClass { get; }
// External assemblies cannot see the UtilClass type which is
// why this does not work.
}
}
This obviously can't be done as the external assembly would need to know about it.
If you really need to have a public property that exposes behaviour without exposing your internal type then consider having an interface that your utility class can implement.
This will allow you to keep the helper class internal but still expose it (via the interface) to an external assembly.
internal class UtilClass : IMyPublicInterface
{
}
namespace MyProject
{
public class PublicClass
{
public IMyPublicInterface DoSomething() {}
public IMyPublicInterface { get; }
// External assemblies now only need to know about the public interface.
}
}
Upvotes: 1
Reputation: 5836
This means you have a public member of type UtilClass
in your PublicClass
. The compiler does not allow this since an external assembly is able to access the public member, but the type of the member itself is internal and hence is not inaccessible.
Make that member internal instead of public.
Upvotes: 7
Reputation: 5994
your error is quite common. As example if you have two classes:
public class PublicClass { ... }
and
private class PrivateClass { ... }
this will return no error. But if you add the PublicClass a property like
public PrivateClass MyProperty { ... }
you ll get an error. Because that property is visibly for the user of your libary but the type of the property is not visible.
Upvotes: 1