Home All Groups Group Topic Archive Search About

Reflecting for Explicitly Implemented Interface methods

Author
16 Nov 2005 10:47 PM
Hexar Anderson
We are using a tool to managed our help documentation for a .NET library we
support, and we have a policy not to document private or internal methods or
explicitly implemented interface methods.  For .NET 1.1, it was easy for our
tool to filter out explicitly implemented interface methods because they were
private, and also because the MethodInfo.Name property for the method always
has a period "." in the name (ie. "ICollection.Count").  Now, with the 2.0
framework, explicit interface implementation methods are public, and the
MethodInfo.Name now only returns the method name itself, and not the
interface.  We still want to filter out these explicitly implemented
interface methods but can't figure out a way to do so.  There doesn't appear
to be any property or method on the MethodInfo class that can tell us this
information.  It's easy to tell in ILDASM because the method has an .override
keyword; is there a programmatic way to get this same information via
reflection?

Author
19 Nov 2005 6:26 PM
Mattias Sjögren
Hexar,

>Now, with the 2.0
>framework, explicit interface implementation methods are public, and the
>MethodInfo.Name now only returns the method name itself, and not the
>interface.

That's not what I'm seeing. Can you post some code that will let us
reproduce the problem?


Mattias

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
22 Nov 2005 12:09 AM
Hexar Anderson
This is C++/CLI code for a solution with two projects, the first is a class
library assembly, the second is a Windows Forms application that reflects
over that assembly:

// ExplicitInterfaceLibrary.h
#pragma once
using namespace System;

namespace ExplicitInterfaceLibrary {
    public ref class MyType : IComparable
    {
    public:
        virtual int CompareTo(Object^ o) = IComparable::CompareTo { return 0; }
    };
}

// Form1.h
    private: System::Void button1_Click(System::Object^  sender,
System::EventArgs^  e) {
                openFileDialog1->DefaultExt = "DLL";
                openFileDialog1->Filter = ".NET Assemblies|*.dll";
                openFileDialog1->InitialDirectory = Application::ExecutablePath;

                System::Windows::Forms::DialogResult result =
openFileDialog1->ShowDialog();

                if (result == System::Windows::Forms::DialogResult::OK)
                {
                    Assembly^ assembly = Assembly::LoadFile(openFileDialog1->FileName);

                    for each (Type^ t in assembly->GetTypes())
                    {
                        String^ tname = t->ToString();

                        if (tname == "ExplicitInterfaceLibrary.MyType")
                        {
                            for each (MethodInfo^ mi in t->GetMethods())
                            {
                                // Figure out if a method is an explicit interface implementation???


                                String^ name = mi->Name;
                                System::Reflection::MethodAttributes ma = mi->Attributes;

                                MessageBox::Show("Type = " + tname +  ", Name = " + name + ",
Attributes = " + ma.ToString());
                            }
                        }
                    }
                }
            }


Show quote
"Mattias Sjögren" wrote:

> Hexar,
>
> >Now, with the 2.0
> >framework, explicit interface implementation methods are public, and the
> >MethodInfo.Name now only returns the method name itself, and not the
> >interface.
>
> That's not what I'm seeing. Can you post some code that will let us
> reproduce the problem?
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>
Author
29 Nov 2005 9:30 PM
Mattias Sjögren
>This is C++/CLI code

Ahh, sorry I assumed C#. Still, there's nothing forcing you to make
the implementing method public. It seems like this easiest solution
would be to make it internal or private, that way it will not be
returned by GetMethods().

Unfortunately I don't think Reflection lets you differentiate implicit
and explicit implementations.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

AddThis Social Bookmark Button