覆盖方法而不扩展可见性 [英] override method without extending visibility
问题描述
有没有办法让一个重写的方法不能从
程序集中覆盖它?一个人为的例子(虽然我想要这个功能,
我的程序与食品服务无关):
具体来说,如何实现DemonstrateConditions方法by
GoodEats,所以它可以被HealthBureau调用而不是
GoodEatsWaiter?
汇编A
{
公共班HealthInspector
{
}
公共抽象类LicensedEstablishment
{
内部抽象无效DemonstrateConditions(HealthInspector);
}
公共静态类HealthBureau
{
public static void Complain(LicensedEstablishment restaurant)
{
HealthInspector h = GetResponsibleInspector(); < br $>
restaurant.DemonstrateConditions(h);
}
}
}
大会B参考A
{
公共课GoodEats:LicensedEstablishment
{
// yikes!
}
内部类GoodEatsWaiter {}
}
Is there any way to have an overridden method which is not callable from the
assembly overridding it? A contrived example (though I want this feature,
my program has nothing to do with food service):
Specifically, how can the DemonstrateConditions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
Assembly A
{
public class HealthInspector
{
}
public abstract class LicensedEstablishment
{
internal abstract void DemonstrateConditions(HealthInspector);
}
public static class HealthBureau
{
public static void Complain(LicensedEstablishment restaurant)
{
HealthInspector h = GetResponsibleInspector();
restaurant.DemonstrateConditions(h);
}
}
}
Assembly B references A
{
public class GoodEats : LicensedEstablishment
{
// yikes!
}
internal class GoodEatsWaiter {}
}
推荐答案
" Ben Voigt" < rb*@nospam.nospamwrote in message
news:e4 ************** @ TK2MSFTNGP04.phx.gbl ...
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...
有没有办法让一个重写的方法无法从
调用大会覆盖它?一个人为的例子(虽然我想要这个
功能,我的程序与食品服务无关):
具体来说,如何实现DemonstrateConditions方法通过
GoodEats,它可以被HealthBureau调用而不是通过
GoodEatsWaiter?
Is there any way to have an overridden method which is not callable from
the assembly overridding it? A contrived example (though I want this
feature, my program has nothing to do with food service):
Specifically, how can the DemonstrateConditions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
CS0507的帮助表明我可以通过声明基础
方法protected internal来完成此操作。和覆盖为受保护...但是
CS0507仍然生成。
The help for CS0507 suggests I can accomplish this by declaring the base
method "protected internal" and the override as "protected"... however
CS0507 is still generated.
>
组装A
{
公共级HealthInspector
{
}
公共抽象类LicensedEstablishment
{
内部抽象无效DemonstrateConditions(HealthInspector);
}
公共静态课程HealthBureau
{
public static void Complain(LicensedEstablishment restaurant)
{
HealthInspector h = GetResponsibleInspector();
restaurant.DemonstrateConditions(h);
}
}
}
大会B参考A
{
公共课GoodEats:LicensedEstablishment
{
// yikes!
}
内部班级GoodEatsWaiter {}
}
>
Assembly A
{
public class HealthInspector
{
}
public abstract class LicensedEstablishment
{
internal abstract void DemonstrateConditions(HealthInspector);
}
public static class HealthBureau
{
public static void Complain(LicensedEstablishment restaurant)
{
HealthInspector h = GetResponsibleInspector();
restaurant.DemonstrateConditions(h);
}
}
}
Assembly B references A
{
public class GoodEats : LicensedEstablishment
{
// yikes!
}
internal class GoodEatsWaiter {}
}
" Ben Voigt" < rb*@nospam.nospamwrote in message
news:%2 **************** @ TK2MSFTNGP04.phx.gbl ...
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
" Ben Voigt" < rb*@nospam.nospamwrote in message
news:e4 ************** @ TK2MSFTNGP04.phx.gbl ...
>
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...
>有没有办法让一个重写的方法无法从组件覆盖它来调用它?一个人为的例子(虽然我想要这个
功能,我的程序与食品服务无关):
具体来说,如何通过GoodEats实现DemonstrateConditions方法,所以它可以由HealthBureau调用而不是由GoodEatsWaiter调用?
>Is there any way to have an overridden method which is not callable from
the assembly overridding it? A contrived example (though I want this
feature, my program has nothing to do with food service):
Specifically, how can the DemonstrateConditions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
CS0507的帮助建议我可以通过声明基础
方法protected internal来完成此操作。和覆盖为受保护...但是
CS0507仍然生成。
The help for CS0507 suggests I can accomplish this by declaring the base
method "protected internal" and the override as "protected"... however
CS0507 is still generated.
提交错误,因为C#编译器显然没有实现CS0507帮助页面指示的行为
。
https://connect.microsoft.com/视觉... dbackID = 236091
>
>
>>
汇编A
公共类HealthInspector
/>
公共抽象类LicensedEstablishment
{
内部抽象无效DemonstrateConditions(HealthInspector);
}
公共静态类HealthBureau
{
public static void投诉(LicensedEstablishment restaurant)
{HealthInspector h = GetResponsibleInspector();
restaurant.DemonstrateConditions(h);
}
}
{
公共课GoodEats:LicensedEstablishment
{
// yikes!
} >内部类GoodEatsWaiter {}
>>
Assembly A
{
public class HealthInspector
{
}
public abstract class LicensedEstablishment
{
internal abstract void DemonstrateConditions(HealthInspector);
}
public static class HealthBureau
{
public static void Complain(LicensedEstablishment restaurant)
{
HealthInspector h = GetResponsibleInspector();
restaurant.DemonstrateConditions(h);
}
}
}
Assembly B references A
{
public class GoodEats : LicensedEstablishment
{
// yikes!
}
internal class GoodEatsWaiter {}
}
Ben Voigt写道:
Ben Voigt wrote:
提交错误,因为C#编译器显然没有实现行为
由CS0507帮助pag指示即
Filing a bug because the C# compiler clearly does not implement the behavior
indicated by the CS0507 help page.
在提交错误报告之前,为什么不发布一些可以让人们有机会发表评论的实际代码?
Arne
Before filing a bug report, then why not post some
real code that would give people a fair chance to comment ??
Arne
这篇关于覆盖方法而不扩展可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!