Overrides

219 views
Skip to first unread message

Greg Young

unread,
Oct 26, 2010, 3:25:16 PM10/26/10
to mono-...@googlegroups.com
I wasn't really expecting this ...


    public class SimpleBase
    {
        public virtual void Test()
        {
            
        }
    }
    class SimpleDerived : SimpleBase
    {
        public override void Test()
        {
            int x = 5;
        }
    }

The The MethodDefinition::HasOverrides for Base::Test says it has no overrides?

--
Les erreurs de grammaire et de syntaxe ont été incluses pour m'assurer de votre attention

Jb Evain

unread,
Oct 26, 2010, 3:38:24 PM10/26/10
to mono-...@googlegroups.com
On Tue, Oct 26, 2010 at 9:25 PM, Greg Young <gregor...@gmail.com> wrote:
> The The MethodDefinition::HasOverrides for Base::Test says it has no
> overrides?

Overrides is used exclusively for cases where the base method can't be
implicitely discovered. (C#'s explicit interface implementation for
instance).

You can use the GetBaseMethod extension method to navigate implicitly
overriden methods.

Greg Young

unread,
Oct 26, 2010, 3:42:25 PM10/26/10
to mono-...@googlegroups.com
hmm.

I am already searching for base methods (I am trying to walk down). I was hoping hasoverrides works so I could tell the difference between a shadowed method and a base method

public class A {
   public void Foo() { }
}

public class B {
   public void Foo() { }
}

vs

public class A {
   public virtual void Foo() { }
}

public class B {
   public override void Foo() { }
}

but I guess I could just look if A::Foo is virtual. Not sure if that is good enough, will have to check ECMA docs.

--
--
mono-cecil

Jb Evain

unread,
Oct 26, 2010, 3:46:37 PM10/26/10
to mono-...@googlegroups.com
On Tue, Oct 26, 2010 at 9:42 PM, Greg Young <gregor...@gmail.com> wrote:
> but I guess I could just look if A::Foo is virtual. Not sure if that is good
> enough, will have to check ECMA docs.

You indeed have to check whether they're virtual. You also have to
differentiate NewSlot/ReuseSlot methods for shadowing or overriding.

Greg Young

unread,
Oct 26, 2010, 3:58:46 PM10/26/10
to mono-...@googlegroups.com
Ah yes ... That should do it thanks JB.

--
--
mono-cecil

Greg Young

unread,
Oct 27, 2010, 9:21:07 AM10/27/10
to mono-...@googlegroups.com
hey JB.

I was looking through the mcs code and it just uses virtual. Am I missing something? I would think using virtual would be enough without using NewSlot/ReuseSlot. If the method is virtual on the base then it must be an override otherwise its a shadow. In the major languages I can think of this is good enough, don't remember if IL even allows me to break this.

Cheers,

Greg

Fabian Schmied

unread,
Oct 27, 2010, 9:27:20 AM10/27/10
to mono-...@googlegroups.com
> If the method is virtual on the base then it must be an
> override otherwise its a shadow.

class Base
{
public virtual void M() { }
}

class Derived
{
public new virtual void M() { }
}

In this case, Base.M() is virtual (and not final), but Derived.M() is
not an override. You can see this in the metadata because Derived.M()
has the NewSlot flag set. Methods with NewSlot are never implicit
overrides of base methods.

Cheers,
Fabian

> On Tue, Oct 26, 2010 at 3:58 PM, Greg Young <gregor...@gmail.com> wrote:
>>
>> Ah yes ... That should do it thanks JB.
>>
>> On Tue, Oct 26, 2010 at 3:46 PM, Jb Evain <jbe...@gmail.com> wrote:
>>>
>>> On Tue, Oct 26, 2010 at 9:42 PM, Greg Young <gregor...@gmail.com>
>>> wrote:
>>> > but I guess I could just look if A::Foo is virtual. Not sure if that is
>>> > good
>>> > enough, will have to check ECMA docs.
>>>
>>> You indeed have to check whether they're virtual. You also have to
>>> differentiate NewSlot/ReuseSlot methods for shadowing or overriding.
>>>
>>> --
>>> --
>>> mono-cecil
>>
>>
>> --
>> Les erreurs de grammaire et de syntaxe ont été incluses pour m'assurer de
>> votre attention
>
>
>
> --
> Les erreurs de grammaire et de syntaxe ont été incluses pour m'assurer de
> votre attention
>

> --
> --
> mono-cecil

Jb Evain

unread,
Oct 27, 2010, 9:29:07 AM10/27/10
to mono-...@googlegroups.com
On Wed, Oct 27, 2010 at 3:21 PM, Greg Young <gregor...@gmail.com> wrote:
> Am I missing
> something?

using System;

class Foo {
public virtual void Do ()
{
Console.WriteLine ("Foo::Do");
}
}

class Bar : Foo {
public override void Do ()
{
Console.WriteLine ("Bar::Do");
}
}

class Baz : Bar {
public new virtual void Do ()
{
Console.WriteLine ("Baz::Do");
}
}

class Gazonk : Baz {

public override void Do ()
{
Console.WriteLine ("Gazonk::Do");
}
}

class Program {

static void Main ()
{
Foo f = new Gazonk ();

f.Do ();

Baz b = f as Baz;

b.Do ();
}
}

Greg Young

unread,
Oct 27, 2010, 9:42:27 AM10/27/10
to mono-...@googlegroups.com
Then MCS must be broken. I cannot find it handling this (but it does do the virtual check).

Greg

Jb Evain

unread,
Oct 27, 2010, 9:48:38 AM10/27/10
to mono-...@googlegroups.com
On Wed, Oct 27, 2010 at 3:42 PM, Greg Young <gregor...@gmail.com> wrote:
> Then MCS must be broken. I cannot find it handling this (but it does do the
> virtual check).

mcs, Mono's C# compiler? Please, it's basic C#, mcs deals with that
without any issue :)

In that case, Baz::Do is marked as NewSlot, as it should.

Greg Young

unread,
Oct 27, 2010, 10:00:36 AM10/27/10
to mono-...@googlegroups.com
I was refering to the linker code you sent


It seems to only use IsVirtual (not NewSlot).

Cheers,

Greg

--
--
mono-cecil

Jb Evain

unread,
Oct 27, 2010, 10:08:50 AM10/27/10
to mono-...@googlegroups.com
On Wed, Oct 27, 2010 at 4:00 PM, Greg Young <gregor...@gmail.com> wrote:
> I was refering to the linker code you sent
> http://github.com/mono/mono/blob/master/mcs/tools/linker/Mono.Linker.Steps/TypeMapStep.cs
> It seems to only use IsVirtual (not NewSlot).

The linker is a particular case. Removing virtual methods is not
trivial, so it takes the whole method chain, whether they have a
NewSlot or not.

Greg Young

unread,
Oct 27, 2010, 10:16:47 AM10/27/10
to mono-...@googlegroups.com
While we are on edge conditions:

.method public newslot virtual final instance void 
        Woo() cil managed
{
  .override Base::Boo
  .maxstack  1
   //...
 IL_000a:  ret
}

ouch.

Greg
--
--
mono-cecil

Greg Young

unread,
Oct 27, 2010, 10:43:57 AM10/27/10
to mono-...@googlegroups.com
To be clear, is there anyway at all of finding a renamed override?

Jb Evain

unread,
Oct 27, 2010, 10:48:17 AM10/27/10
to mono-...@googlegroups.com
On Wed, Oct 27, 2010 at 4:43 PM, Greg Young <gregor...@gmail.com> wrote:
> To be clear, is there anyway at all of finding a renamed override?

I'm not exactly sure what you want here. Please be more specific or
include a piece of code showing what you have and what you would like.

Fabian Schmied

unread,
Oct 27, 2010, 10:48:58 AM10/27/10
to mono-...@googlegroups.com
> To be clear, is there anyway at all of finding a renamed override?

Check the Overrides collection, it should be in there, if I'm not mistaken.
And with that, this thread has gone to where it originally started :)

Fabian

> --
> --
> mono-cecil

Greg Young

unread,
Oct 27, 2010, 10:54:12 AM10/27/10
to mono-...@googlegroups.com
I included the IL in the previous post. Renamed override.

Will try the Overrides collection as suggested by Fabian.

--
--
mono-cecil
Reply all
Reply to author
Forward
0 new messages