Error with "internal interface"

N

~~~ .NET Ed ~~~

I am facing a problem. My project is composed of several assemblies. In one
of them -the backend- I have several internal classes that must implement an
interface. These internal classes are only used by one master class that is
public.

I am able to declare the interface as "internal interface" without problem
but when I attempt to use it by declaring a class that implements it, if I
declare the implemented interface members as "internal" (they are supposed
to be seen only within the assemly) I get an error that it is not
implemented because of "wrong return type or wrong visibility" so basically
it is forcing me to use "public" which I don't want because I don't want
those methods to be seen outside.

Or... perhaps the public gets "downgraded" if you use an "internal"
attribute in the class declaration, at least that is what I am hoping for
but makes the notation a bit confusing.

Emilio
 
J

Jon Skeet [C# MVP]

I am facing a problem. My project is composed of several assemblies. In one
of them -the backend- I have several internal classes that must implement an
interface. These internal classes are only used by one master class that is
public.

I am able to declare the interface as "internal interface" without problem
but when I attempt to use it by declaring a class that implements it, if I
declare the implemented interface members as "internal" (they are supposed
to be seen only within the assemly) I get an error that it is not
implemented because of "wrong return type or wrong visibility" so basically
it is forcing me to use "public" which I don't want because I don't want
those methods to be seen outside.

That's because all interface members are public by default.
Or... perhaps the public gets "downgraded" if you use an "internal"
attribute in the class declaration, at least that is what I am hoping for
but makes the notation a bit confusing.

Nope, the members are still public.

However, if the class itself is internal, that's pretty much
irrelevant as no-one outside the assembly will be able to see it
anyway.

Jon
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

If those classes are only used by one class, why don't you declare them as
private to the master class?

/In the same vein you can declare your interface inside the Master class.
But even so it might be visible from the outside using Master.Interface

Now, if you change the interface for an abstract class inside the Master you
can get what you want:


public class Master
{
abstract class A
{
abstract public void Me();

}
class B : A
{
public override void Me()
{
throw new Exception("The method or operation is not
implemented.");
}
}
}
 
C

Christof Nordiek

I am able to declare the interface as "internal interface" without problem
but when I attempt to use it by declaring a class that implements it, if I
declare the implemented interface members as "internal" (they are supposed
to be seen only within the assemly) I get an error that it is not
implemented because of "wrong return type or wrong visibility" so
basically it is forcing me to use "public" which I don't want because I
don't want those methods to be seen outside.
If you want the methods to remain internal you have to implement them
explicitly, that is by naming the interface in the member:

public class Class : Interface
{
//This member is only accessible by cast to the interface
void Interface.Method()
{
//This call is made if the following declaration is present
Method();
//Or simpöy put here the implementation
}

//Make it accessible without cast to interface
internal void Method()
{
...
}
}

Now you have a public class wich implements a interface, wich is only
accessible internal.

But if the class itself is internal, all members even public will only be
accessible internal.

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top