How to hide an interface in C#

  • Thread starter Thread starter Laurent Vigne
  • Start date Start date
L

Laurent Vigne

Hello, I would like to know how to make the methods of an interface
inaccessible from outside

Example:
----------------------------------------------
internal Interface ImyInterface
{
void MyPrivateMethod(); // I cant define any accessibility here, well
ok !
}

public class A, ImyInterface // I cant implement my interface with
Private or internal accessibility, well ok !
{
public void MafonctionTresPrivee() // Why I cant use private or
internal accessibility here ???
{
ASSERT(false);
}
}

public class B
{
A myObjectA = new A();
myObjectA.MyPrivateMethod (); // Heyyy, we can reach my method here
!!!
}
 
Laruent,

You can explicitly implement the interface, like so:

public class A, IMyInterface
{
void IMyInterface.MafonctionTresPrivee()
{

}
}

This just means that you won't have a public MafonctionTresPrivee method
exposed, and can only access it through the interface (cast the object
reference to the interface). Having a private or protected implementation
doesn't make much sense.

Hope this helps.
 
Because otherwise, an interface would be meaningless. Implementing an
interface, meaning you are accepting a contract to implement each of its
methods. Usually, this is so the object can be cast to the interface, and
then have methods called on it.

If one of them methods you implemented is private, then this would become
invalid - you would not be able to call that particular method on the object
through the interface cast - making implementing the interface meaningless.

Also, in your example here, you would not be able to call the method from
class B if the method was private.
 
Hi Laurent,

Interfaces define a contract - it defines what all members a type
implementing an interface will definitely have.

All members in an interfaces are public by default - in fact, you can't have
any private/protected etc. members in an interface, because they will simply
not form a 'public' contract which an interface is meant to be. For that
reason, you won't be able to use any access specifies while defining an
interface.

Because interface members are public, their implementations in the sub types
should be public as well.

Let me know if I could be of more help.

HTH,
Rakesh Rajan
 
Marina said:
Because otherwise, an interface would be meaningless. Implementing an
interface, meaning you are accepting a contract to implement each of its
methods. Usually, this is so the object can be cast to the interface, and
then have methods called on it.

If one of them methods you implemented is private, then this would become
invalid - you would not be able to call that particular method on the object
through the interface cast - making implementing the interface meaningless.

Also, in your example here, you would not be able to call the method from
class B if the method was private.

On the other hand, it *would* make sense to be able to specify an
internal assembly implementation. It's a bit of a shame that's not
allowed, as it could be useful sometimes.
 
I see that you exactly understood my problem.
So I resume, if a public class implement an interface, there no way to hide
interface methods from outside.
Actually I use a private class that implement my interface and call back the
internal methods of my public class.
I just hope there a better solution.

Sorry for my poor english.
And thank for your help all !


Laurent.
 
Jon Skeet said:
On the other hand, it *would* make sense to be able to specify an
internal assembly implementation. It's a bit of a shame that's not
allowed, as it could be useful sometimes.


Right, in fact I want to hide interface implementation to outside of my
assembly
 
Would it really? If you can have an instance of this object outside the
assembly, woudn't you expect to be able to access all of its interface
methods?
 
No ! In my sample, the public class implement an interface declared in
internal !
Outside of my assembly you cant see the interface, you just see the method
that implement this interface cause there no way to hide them. But there no
sense in my case to give an extern acces to this methods.


Marina said:
Would it really? If you can have an instance of this object outside the
assembly, woudn't you expect to be able to access all of its interface
methods?
 
Sorry, I did not understand that response.

My only point is, if you have an object defined in assembly A, and using it
in assembly B, and this object implements an interface, you expect to be
able to cast the object to the interface, and then call any of the interface
methods on it.

If some of those methods were declared internal, this couldn't work.

And if it did work, then who cares if the method's implementation was marked
as internal or not, if the above procedure violated the intention of hiding
the implementation - since now you can get it anyway.

Laurent Vigne said:
No ! In my sample, the public class implement an interface declared in
internal !
Outside of my assembly you cant see the interface, you just see the method
that implement this interface cause there no way to hide them. But there no
sense in my case to give an extern acces to this methods.
 
Marina said:
Would it really? If you can have an instance of this object outside the
assembly, woudn't you expect to be able to access all of its interface
methods?

Not if I couldn't even tell it was implementing the interface - if the
fact that it implemented the interface was internal as well.

Bear in mind that the way things are now, I can't even design my own
interface for use strictly within my own assembly and not make the rest
of the world able to see and use that interface. That's not a good
thing, IMO.
 
Hi Jon,
On the other hand, it *would* make sense to be able to specify an
internal assembly implementation. It's a bit of a shame that's not
allowed, as it could be useful sometimes.

Think about the semantics of those interfaces:

// assembly A
public interface IContract {
void Method()
internal void InternalMethod();
}

// assembly B
class C : IContract {
public void Method() {}
// InternalMethod() cannot be implemented, so C doesn't really
// implement IContract.
}

So IContract must be made non-implementable from outside.
A quite unilateral contract, isn't it? :-)

bye
Rob
 
Jon Skeet said:
Bear in mind that the way things are now, I can't even design my own
interface for use strictly within my own assembly and not make the rest
of the world able to see and use that interface. That's not a good
thing, IMO.

Maybe I'm misunderstanding, but I thought you could do that. If you create
an assembly like this:

internal interface MyInterface {
void DoSomething();
}

public class MyClass : MyInterface {
public MyClass() {
}

void MyInterface.DoSomething() {
}
}

then MyClass can be used from another assembly, but MyInterface (and
MyClass's DoSomething method) are not visible to that assembly.

Chris Jobson
 
Chris Jobson said:
Maybe I'm misunderstanding, but I thought you could do that. If you create
an assembly like this:

internal interface MyInterface {
void DoSomething();
}

public class MyClass : MyInterface {
public MyClass() {
}

void MyInterface.DoSomething() {
}
}

then MyClass can be used from another assembly, but MyInterface (and
MyClass's DoSomething method) are not visible to that assembly.

Ah yes, you're right. For some reason I'd misremembered things,
thinking that you couldn't declare the interface itself to be internal.

Still, you can't declare a public class to implement a public
interface, but only implement it for classes in the same assembly,
which is something that could sometimes be useful.
 
Robert Jordan said:
Think about the semantics of those interfaces:

// assembly A
public interface IContract {
void Method()
internal void InternalMethod();
}

// assembly B
class C : IContract {
public void Method() {}
// InternalMethod() cannot be implemented, so C doesn't really
// implement IContract.
}

So IContract must be made non-implementable from outside.
A quite unilateral contract, isn't it? :-)

That's not quite what I was going after. I was suggesting something
like a class which from inside the assembly could be regarded as
implementing IList, but shouldn't be publicly implementing IList. That
can't be done, as far as I can see.
 
Back
Top