views:

27

answers:

1

Consider the following case:

Public Interface IHasDateUpdated
     Property DateUpdated As DateTime
End Interface

Public Class MyClass
     Implements IHasDateUpdated

     Public Property MyDateUpdated As DateTime Implements IHasDateUpdated.DateUpdated
End Class

Now, assuming I was referencing an instance of MyClass as an IHasDateUpdated; how would I determine the actual name of the property that implements the interface property via reflection?

For example:

Dim x As IHasDateUpdated = New MyClass()
' How do I derive "MyDateUpdated" from "x" using x.DateUpdated?
+1  A: 

Sorry flor the c# answer but you should be able to translate this I'm suer :)

InterfaceMapping im = y.GetInterfaceMap(typeof(IHasDateUpdated ));
        foreach (MethodInfo info in im.TargetMethods)
        {
            if (info.IsPrivate)
                MessageBox.Show(info.Name + " is private");
        }
        MessageBox.Show(y.FullName);
Kell
@Kell: Assuming that there was more than a single property on the interface, is there a way I can target that specifically without resorting to iteration?
DanP
I suppose you could use linq to select the one you are interested in, but you would need to get the index in order to get the mapped index. Maybe this site will shed some light (its in vb) http://msdn.microsoft.com/en-us/library/system.type.getinterfacemap(VS.95).aspx
Kell
@Kell: Methinks this is an ideal place for an extension method..that should clean up my client code quite a bit..thanks for putting me on the right track.
DanP
Sounds like a fantastic ides :) good luck
Kell