Reflecting for Explicitly Implemented Interface methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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
 
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());
}
}
}
}
}
 
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
 
Back
Top