Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to invoke explicitly implemented method/property via reflection in .NET?

I need to be able to determine if a given method or property comes from a particular interface and is explicitly implemented.
Has anyone done this and is it actually possible to get this information by the means of .NET reflection?


Update

As can be seen in comments below the accepted answer, the actual thing I am trying to accomplish is to call the method that implements a particular interface via reflection. Since the possibility to have multiple interfaces with the same method signature, I wanted to determine the right implementation to invoke based on the interface. In my scenario, the implementation type, interface and method name are determined at runtime, so I cannot use simple casting in my case.

like image 429
Ivaylo Slavov Avatar asked Jul 04 '11 14:07

Ivaylo Slavov


People also ask

How do you call an explicit interface?

The following is an example of how to call "Explicit Interface Method" in the same class using class methods. Output: Class1 Display Method. Iinterface_1 Method Explicit interface implementation. Iinterface_1 Method Implicit interface implementation.

How can you invoke an object method in C#?

To invoke a method and omit optional parameters, call Type. InvokeMember instead. If this method overload is used to invoke an instance constructor, the object supplied for obj is reinitialized; that is, all instance initializers are executed. The return value is null .

Can an interface implement methods C#?

With C# 8.0, you can now have default implementations of methods in an interface. Interface members can be private, protected, and static as well. Protected members of an interface cannot be accessed in the class that extends the interface.

What do you use if you want to ensure that all methods and properties are implemented?

Yep, use an interface.


2 Answers

Explicitly implemented interface methods in C# are private in the target class. You can use this fact and create this extension method to return only these methods:

static IEnumerable<MethodInfo> GetExplicitlyImplementedMethods(this Type targetType, 
    Type interfaceType) 
{ 
  return targetType.GetInterfaceMap(interfaceType).TargetMethods.Where(m => m.IsPrivate);
}

Note: this is for C# only.

UPDATE: But, from your requirements, it seems that you only want to know which methods implement which interface methods, without really caring about whether the implementation is implicit or explicit. For a solution that works across languages then, this would suffice:

static IEnumerable<MethodInfo> GetImplementedMethods(this Type targetType,
    Type interfaceType) 
{ 
  return targetType.GetInterfaceMap(interfaceType).TargetMethods;
}
like image 92
Jordão Avatar answered Oct 13 '22 00:10

Jordão


If when using reflection a method is private and its name contains a ., like "System.IDisposable.Dispose", then it is an explicit implementation.

like image 28
Ricardo Peres Avatar answered Oct 13 '22 01:10

Ricardo Peres