Re: Calling base implementation from base class?
From: Microsoft (joelycat_at_hotmail.com)
Date: 08/21/04
- Next message: Troy: "RE: For vs. For Each"
- Previous message: James from NY: "Re: Why did Microsoft limited itself to Windows?"
- In reply to: Brian Tyler: "Re: Calling base implementation from base class?"
- Next in thread: Ben Schwehn: "Re: Calling base implementation from base class?"
- Reply: Ben Schwehn: "Re: Calling base implementation from base class?"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 20 Aug 2004 23:18:26 -0400
I guess I'll have to start learning IL :-)
Thanks for the info. I hope MS adds this to C# soon. It doesn't come up
often but when you need it, you end up with a messy work-around.
Thanks again.
</joel>
"Brian Tyler" <brianmtyler@yahoo.com> wrote in message
news:OT2s8$rhEHA.2340@TK2MSFTNGP11.phx.gbl...
> It actually is possible at the IL level - using the CALL instruction
versus
> the CALLVIRT. I am not sure why there doesn't seem to be a C# syntax for
it.
> In my situation, I created a utility class in IL that I then merged in
with
> my assembly (multi-module assembly). Unfortunately, VS.NET doesn't give
you
> much help in such a build - makefiles are much easier.
>
> "Joel" <joelycat@hotmail.com> wrote in message
> news:%23pkQjKihEHA.596@TK2MSFTNGP11.phx.gbl...
> > That's the first thing I tried and it doesn't work.
> >
> > Check out
> >
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_10_5_4.asp.
> > Specifically the paragraph right after the first example. It explicity
> > states:
> >
> > Had the invocation in B been written ((A)this).PrintFields(), it would
> > recursively invoke the PrintFields method declared in B, not the one
> > declared in A, since PrintFields is virtual and the run-time type of
> > ((A)this) is B.
> >
> > I worked around my immediate problem but now I'm just academically
> curious.
> > It doesn't strike me that there's anything "illegal" about doing what I
> > want, it just seems like there is no syntax for it.
> >
> > Anyone from MS want to pitch in?
> >
> > </joel>
> >
> > "Luc E. Mistiaen" <luc.mistiaen@advalvas.be.no.spam> wrote in message
> > news:eG0FmChhEHA.1568@TK2MSFTNGP09.phx.gbl...
> > > Wouldn't a cast like
> > >
> > > ((Base)this).Method1 () ;
> > >
> > > in your Method2 solve your problem?
> > >
> > > /LM
> > >
> > > "Joel" <joelycat@hotmail.com> wrote in message
> > > news:eFXyydfhEHA.1356@TK2MSFTNGP09.phx.gbl...
> > > > Sorry, I pulled that sample out of thin air without testing it.
Here's
> > > > working code to illustrate the problem:
> > > >
> > > > using System;
> > > >
> > > > namespace ConsoleApplication4
> > > > {
> > > > class Class1
> > > > {
> > > > [STAThread]
> > > > static void Main(string[] args)
> > > > {
> > > > Derived d=new Derived();
> > > > d.Method1();
> > > > }
> > > > }
> > > >
> > > > class Base
> > > > {
> > > > public virtual void Method1()
> > > > {
> > > > Console.WriteLine("Base.Method1");
> > > > }
> > > >
> > > > public virtual void Method2()
> > > > {
> > > > Console.WriteLine("Base.Method2");
> > > > Method1();
> > > > }
> > > > }
> > > >
> > > > class Derived : Base
> > > > {
> > > > public override void Method1()
> > > > {
> > > > Console.WriteLine("Derived.Method1");
> > > > base.Method2();
> > > > }
> > > > }
> > > > }
> > > >
> > > > This results in an endless loop of :
> > > >
> > > > Derived.Method1
> > > > Base.Method2
> > > > Derived.Method1
> > > > Base.Method2
> > > >
> > > > because the call to Method1() in Base calls Derived's
implementation.
> > What
> > > I
> > > > want is for the Method1 invocation in Base to call its (Base's)
> > > > implementation of Method1. The desired output would be:
> > > >
> > > > Derived.Method1
> > > > Base.Method2
> > > > Base.Method1
> > > >
> > > > Sorry I wasn't clearer before.
> > > >
> > > > "eSapient" <eSapient@Yahoo.Com> wrote in message
> > > > news:%23F13YEZhEHA.3980@TK2MSFTNGP12.phx.gbl...
> > > > > Depends much on what interface you want to expose. Did you try
> running
> > > > your
> > > > > code? It would not run because B.Method1 is protected. So the
first
> > > thing
> > > > > you need to do is make it public. But in that case, you would be
> > > changing
> > > > > the signature of A.Method1 which you are overriding and that is
not
> > > > allowed.
> > > > > The following will work, but may not exactly meet your
requirements.
> > > > >
> > > > > class MyApp
> > > > >
> > > > > {
> > > > >
> > > > > public static void Main()
> > > > >
> > > > > {
> > > > >
> > > > > D d = new D();
> > > > >
> > > > > d.Method2();
> > > > >
> > > > > }
> > > > >
> > > > > }
> > > > >
> > > > >
> > > > >
> > > > > class B
> > > > >
> > > > > {
> > > > >
> > > > > protected virtual void Method1()
> > > > >
> > > > > { Console.WriteLine("Inside B.Method1()"); }
> > > > >
> > > > > }
> > > > >
> > > > > class D : B
> > > > >
> > > > > {
> > > > >
> > > > > public void Method2()
> > > > >
> > > > > { base.Method1(); }
> > > > >
> > > > > }
> > > > >
> > > > >
> > > > > "Joel" <joelycat@hotmail.com> wrote in message
> > > > > news:e8BVaDWhEHA.612@TK2MSFTNGP10.phx.gbl...
> > > > > > Is there a way to call a base class implementation of a
protected
> > > method
> > > > > > when Here's the scenario:
> > > > > >
> > > > > > class MyApp
> > > > > > {
> > > > > > public static Main()
> > > > > > {
> > > > > > D obj=new D();
> > > > > >
> > > > > > obj.Method1();
> > > > > > }
> > > > > > }
> > > > > >
> > > > > >
> > > > > > class B
> > > > > > {
> > > > > > protected void Method1()
> > > > > > {
> > > > > > }
> > > > > >
> > > > > > protected void Method2()
> > > > > > {
> > > > > > //I want to call B's implementation of Method1 even
though
> > I'm
> > > > > > running as D's implementation of Method2;
> > > > > > //conceptually I want to do: this.base.Method1();
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > class D : B
> > > > > > {
> > > > > > override protected void Method1()
> > > > > > {
> > > > > > Method2();
> > > > > > }
> > > > > > }
> > > > > >
> > > > > >
> > > > > >
> > > > > > Is this doable?
> > > > > >
> > > > > > TIA
> > > > > >
> > > > > > </joel>
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>
- Next message: Troy: "RE: For vs. For Each"
- Previous message: James from NY: "Re: Why did Microsoft limited itself to Windows?"
- In reply to: Brian Tyler: "Re: Calling base implementation from base class?"
- Next in thread: Ben Schwehn: "Re: Calling base implementation from base class?"
- Reply: Ben Schwehn: "Re: Calling base implementation from base class?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|