473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

override method without extending visibility

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 {}
}
Nov 6 '06 #1
13 3466

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...
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?
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.
>
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 {}
}

Nov 6 '06 #2

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...
>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?

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.
Filing a bug because the C# compiler clearly does not implement the behavior
indicated by the CS0507 help page.

https://connect.microsoft.com/Visual...dbackID=236091
>
>>
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 {}
}


Nov 6 '06 #3
Ben Voigt wrote:
Filing a bug because the C# compiler clearly does not implement the behavior
indicated by the CS0507 help page.
Before filing a bug report, then why not post some
real code that would give people a fair chance to comment ??

Arne
Nov 7 '06 #4
Hi Ben,

I'm not sure I fully understand your question, so please feel free to let
me know.

Per your question:

==========
Specifically, how can the DemonstrateConditions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
==========

I think that using "protected internal" on the method should be ok for your
requirement. "protected" means it can only be called by inheriting classes.
"internal" means can only be called by the declaring assembly (in this
case, is assembly A). Combining them means it can be called by inheriting
classes or classes in the same assembly.

When you inherit this class in assemly B, you will notice the overridden
method only has signature "protected", we're not allowed to use "internal"
here because "internal" to assembly B means it's callable in assembly B
which will change the visibility and violates compiler error CS0507.

So I think the documentation os CS0507 is correct here.

Please let me know if my understanding of your question is correct or not.
Thanks.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 7 '06 #5
If I understand your non-code, then your interpretation is incorrect;
"protected internal" allows inherited OR same-assembly access, not AND.

Marking HealthBureau.Complain as sealed will prevent the downstream classes
from further overriding, but that doesn't sound like quite what you want...

Marc
Nov 7 '06 #6

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:l3**************@TK2MSFTNGXA01.phx.gbl...
Hi Ben,

I'm not sure I fully understand your question, so please feel free to let
me know.

Per your question:

==========
Specifically, how can the DemonstrateConditions method be implemented by
GoodEats, so that it can be called by HealthBureau and not by
GoodEatsWaiter?
==========

I think that using "protected internal" on the method should be ok for
your
requirement. "protected" means it can only be called by inheriting
classes.
"internal" means can only be called by the declaring assembly (in this
case, is assembly A). Combining them means it can be called by inheriting
classes or classes in the same assembly.

When you inherit this class in assemly B, you will notice the overridden
method only has signature "protected", we're not allowed to use "internal"
here because "internal" to assembly B means it's callable in assembly B
which will change the visibility and violates compiler error CS0507.

So I think the documentation os CS0507 is correct here.
I agree that this interpretation of "protected internal" with a "protected"
override is designed for this situation. However, the compiler (C# 2.0 with
SP1 beta) doesn't actually behave as advertised. CS0507 is generated when
the override is "protected", and it only compiles when the override is
"protected internal" (accessible from assembly B, NOT what I want).

I've worked around this by making the virtual function protected, and
defining an internal shim method.
>
Please let me know if my understanding of your question is correct or not.
Thanks.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 7 '06 #7
However, the compiler (C# 2.0 with
SP1 beta) doesn't actually behave as advertised.
In the definition
(http://msdn2.microsoft.com/en-us/lib...8b(VS.80).aspx) the key
phrase is "defined in referenced metadata" - i.e. separate assemblies.
The "internal" aspect doesn't make it past the assembly edge.

Marc

Nov 8 '06 #8
To clarify - by this I mean that within a single assembly you are bound
by the first condition (changing access), not the second (specifically
relating to protected internal in referenced metadata).

Marc

Nov 8 '06 #9
Hi Ben,

I'm not sure if I understood you correctly, do you mean that having:

virtual protected internal void M() {}

in assembly A, and in assembly B:

protected override internal void M() {}

works while:

protected override void M() {}

doesn't work?
I just verified that on VS2005 and VS2005+SP1 Beta, both indicates only:

protected override void M() {}

works, which is the behavior as documented in compiler error CS0507.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 8 '06 #10

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:yM**************@TK2MSFTNGXA01.phx.gbl...
Hi Ben,

I'm not sure if I understood you correctly, do you mean that having:

virtual protected internal void M() {}

in assembly A, and in assembly B:

protected override internal void M() {}

works while:

protected override void M() {}

doesn't work?
Yes, I mean exactly that. Only, my base method is pure as well as virtual
(using abstract keyword).

public abstract class IHandlespace

{

....

protected internal abstract void OnEndEpisode();

//internal void EndEpisode() { OnEndEpisode(); }

}

internal sealed class Handlespace : IHandlespace

{

....

override public event HandlespaceHandler EpisodeEnded;

protected override void OnEndEpisode()

{

if (EpisodeEnded != null)

EpisodeEnded(this);

}

}
error CS0507: '....Handlespace.OnEndEpisode()': cannot change access
modifiers when overriding 'protected internal' inherited member
'.....IHandlespace.OnEndEpisode()'

>
I just verified that on VS2005 and VS2005+SP1 Beta, both indicates only:

protected override void M() {}

works, which is the behavior as documented in compiler error CS0507.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 8 '06 #11
Hi Ben,

Is Handlespace in the same assembly as IHandlespace? If this is the case,
the "internal" is required because without it it will change the
accessibility of the method.

If it's in another assembly, then the "internal" should be removed as I
described in my previous reply.

Would you please help me confirm this behavior? Thanks.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 9 '06 #12

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:6$**************@TK2MSFTNGXA01.phx.gbl...
Hi Ben,

Is Handlespace in the same assembly as IHandlespace? If this is the case,
the "internal" is required because without it it will change the
accessibility of the method.

If it's in another assembly, then the "internal" should be removed as I
described in my previous reply.

Would you please help me confirm this behavior? Thanks.
Two different assemblies, however there is also an
InternalsVisibleToAttribute in play.
>
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 10 '06 #13
Hi Ben,

Thanks for letting us know that you're also using
InternalsVisibleToAttribute. I'm sorry that I haven't taken this attribute
into account in my previous replies. However, I think the compiler behavior
is still correct here, although the documentation of CS0507 should mention
this attribute. I mean, when you make the first assembly internal
classes/methods visible to second assembly, overriding the method will
certainly need to keep the internal modifier just as if you're overriding
it in the same assembly.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 13 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: wooks | last post by:
I have a very simple stylesheet attached to an html file as below. body {color: darkgray; visibility: hidden} span {color: black; visibility: visible} The objective is to only display text...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
4
by: Jazper Manto | last post by:
hi what is the difference between virtual / override and new? until now i only knew the virtual / override thing. thanx for hint. Jazper //--- new ----------------------------------------...
2
by: Flip | last post by:
In java, the default for methods is override. In c# that is not the case. This talks about two classes, the base class and the overriding class. What happens when you have a third class in the...
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
14
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
4
by: Sean Dockery | last post by:
I am a Java veteran who has just started learning C# with Tom Archer's Inside C# 2nd Ed. In the first chapter, there is a typical Hello, World! application that looks like this... namespace...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.