Method.Overrides

10 views
Skip to first unread message

Knight Commander

unread,
Mar 10, 2009, 11:17:21 AM3/10/09
to mono-...@googlegroups.com
Dear all,
I have a code which try to reads all overrides of a method, but it seems that Cecil does not fill overrides collection.
Here is the code:

        private void RenameMethod(MethodDefinition method)
        {
            if (renamedObjects.ContainsKey(method.Name))
                return;
            List<MethodReference> overrides = new List<MethodReference>();

            foreach (MethodReference methodref in method.Overrides)
            {
                overrides.Add(methodref);
            }
            method.Name = GetNewName();
            renamedObjects.Add(method.Name, method.Name);
            foreach (MethodReference methodRef in overrides)
            {
                methodRef.Name = method.Name;
            }
    
Is there anything wrong with what I did?
thank you.

Keith

unread,
Mar 12, 2009, 12:54:19 AM3/12/09
to mono-cecil
Cecil only populates the Overrides collection with those that appear
explicitly in the metadata, corresponding to the .override IL
directive. This directive is rarely used, only when you need to
override (from a base class) or implement (from an interface) a method
with the same arguments/return but with a different name. Few high-
level languages provide a means for you to do this, and compilers
typically don't create explicit override entries for implicit
overrides.

A method can be overridden/implemented simply by matching name and
signature. These don't require an entry in the metadata, and won't
appear in the Overrides collection in Cecil. To find them, you have to
search the base class and parent interfaces for methods with the same
name and signature.

Lotfi Gheribi

unread,
Mar 13, 2009, 12:05:34 PM3/13/09
to mono-...@googlegroups.com
I'm working on developing an open source .Net obfuscator, and I had to
find all the methods overriding/overriden by a given method.

As Keith said, you should load all parent types to see which methods
override which ones. However, it's not as simple as it seams. Do not
forget that a signature can change when instantiating generic types :

Class C1<T>
{
virtual void MyMethod(T arg) {}
}

Class C2<T1, T2> : C1<T2>
{
virtual void MyMethod(T2 arg) {}
}


Class C3 : C2 <int, bool>
{
virtual void MyMethod(bool arg) {}
}

C1.MyMethod, C2.MyMethod and C3.MyMethod have distinct signatures, but
you should deals with them as equivalent !


==> Consider also the following case (it can be important for some
applications, as it was the case of my obfuscator)

Class C1
{
virtual void MyMethod() {}
}

Interface I1
{
void MyMethod();
}

Class C2 : C1, I1
{ }

Although C2 implements I1, it doesn't provide directly an
implementation for I1.MyMethod. This is because C2 inherits from C1,
and C1 contains a method MyMethod.

As you can see, C1.MyMethod overrides indirectly I1.MyMethod, but I1
isn't a parent of C1 !

Sidar Ok

unread,
Mar 13, 2009, 4:32:07 PM3/13/09
to mono-...@googlegroups.com
would you mind sharing any links to this OSS ?
--
Sidar Ok
http://www.sidarok.com

Lotfi Gheribi

unread,
Mar 13, 2009, 4:46:05 PM3/13/09
to mono-...@googlegroups.com
I'm sorry, right now it's just a peace of mess without UI :(
I hope the first public version would be available during June, but I
cant promise anything.

Sidar Ok

unread,
Mar 13, 2009, 4:56:14 PM3/13/09
to mono-...@googlegroups.com
so it is just "source" rather than "open source" yet :)

thanks anyway.
Reply all
Reply to author
Forward
0 new messages