如何在C#中隐藏界面 [英] How to hide an interface in C#

查看:72
本文介绍了如何在C#中隐藏界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道如何制作界面的方法

无法从外面访问


示例:

----------------------------------------------

内部接口ImyInterface

{

void MyPrivateMethod(); //我不能在这里定义任何辅助功能,好吧

ok!

}


公共类A,ImyInterface //我不能实现我的界面

私人或内部辅助功能,好吧!

{

public void MafonctionTresPrivee()//为什么我不能使用私人或

这里有内部可访问性???

{

ASSERT(false);

}

}


公共等级B

{

myObjectA = new A();

myObjectA.MyPrivateMethod(); //嘿,我们可以在这里找到我的方法

!!!

}

------------ -----------------------------------


谢谢你提前

解决方案

Laruent,


您可以明确地实现界面,如下所示:


公共类A,IMyInterface

{

void IMyInterface.MafonctionTresPrivee()

{
< br $>
}

}


这只是意味着你没有公开的MafonctionTresPrivee方法

暴露,并且只能通过界面访问它(投射对象

对界面的引用)。拥有私人或受保护的实施

没有多大意义。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


" Laurent Vigne" < LA *********** @ thomson.net>在消息中写道

news:cl ********* @s5.feed.news.oleane.net ...

你好,我想知道如何使界面的方法无法从外部进入

例如:
-------------------- --------------------------
内部接口ImyInterface
{
void MyPrivateMethod(); //我不能在这里定义任何可访问性,
好吧
好的!

公共类A,ImyInterface //我无法实现我的界面
私人或内部无障碍,好吧!
{// @ public void MafonctionTresPrivee()//为什么我不能在这里使用私人或
内部辅助功能???
{
ASSERT (假);
}

公共课B
{myObjectA = new A();
myObjectA.MyPrivateMethod( ); //嘿,我们可以在这里找到我的方法
!!!
}
------------------------ -----------------------

提前谢谢



因为否则,界面将毫无意义。实现

接口,这意味着您正在接受实现其每个

方法的合同。通常,这样就可以将对象强制转换为界面,然后

然后调用方法。


如果你实现的方法之一是私有,那么这将变成

无效 - 你将无法通过界面演员在对象上调用该特定方法

- 使得实现界面毫无意义。 />

另外,在你的例子中,如果方法是私有的,你将无法从

B类调用该方法。

" Laurent Vigne" < LA *********** @ thomson.net>在消息中写道

news:cl ********* @s5.feed.news.oleane.net ...

你好,我想知道如何使界面的方法无法从外部进入

例如:
-------------------- --------------------------
内部接口ImyInterface
{
void MyPrivateMethod(); //我不能在这里定义任何可访问性,
好​​吧!

公共类A,ImyInterface //我无法实现我的界面
私有或内部可访问性好吧!
{// @ public void MafonctionTresPrivee()//为什么我不能在这里使用私人或
内部辅助功能???
{
ASSERT(false);
}

公共课B
{myObjectA = new A();
myObjectA.MyPrivateMethod(); //嘿,我们可以在这里达到我的方法
!!!
}
------------------------ -----------------------

提前谢谢



嗨Laurent,


接口定义了一个合同 - 它定义了实现接口的所有成员类型

肯定会有的。


默认情况下,界面中的所有成员都是公开的 - 事实上,你不能在界面中拥有任何私人/受保护等成员的b / b
因为他们只会简单地形成一个界面所谓的公共合同。对于那个

的原因,在定义

界面时,你将无法使用任何访问权限。


因为接口成员是公共的,它们在子类型中的实现也应该是公开的。


如果我能得到更多的帮助,请告诉我。 />

HTH,

Rakesh Rajan


" Laurent Vigne"写道:

您好,我想知道如何使界面的方法无法从外部进入



----------------------------------------------
内部接口ImyInterface
{
void MyPrivateMethod(); //我不能在这里定义任何可访问性,好吧
好吧!

公共类A,ImyInterface //我无法实现我的界面
私有或内部可访问性好吧!
{// @ public void MafonctionTresPrivee()//为什么我不能在这里使用私人或
内部辅助功能???
{
ASSERT(false);
}

公共课B
{myObjectA = new A();
myObjectA.MyPrivateMethod(); //嘿,我们可以在这里找到我的方法
!!!
}
------------------------ -----------------------

提前谢谢



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
!!!
}
-----------------------------------------------

Thank you in advance

解决方案

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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Laurent Vigne" <la***********@thomson.net> wrote in message
news:cl*********@s5.feed.news.oleane.net...

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
!!!
}
-----------------------------------------------

Thank you in advance



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.

"Laurent Vigne" <la***********@thomson.net> wrote in message
news:cl*********@s5.feed.news.oleane.net...

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 !!!
}
-----------------------------------------------

Thank you in advance



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

"Laurent Vigne" wrote:

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
!!!
}
-----------------------------------------------

Thank you in advance



这篇关于如何在C#中隐藏界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆