Skip to content

Instantly share code, notes, and snippets.

@ForNeVeR
Last active April 27, 2022 16:17
Show Gist options
  • Save ForNeVeR/a0da2ffc4fd569c5d5e24ed19493321c to your computer and use it in GitHub Desktop.
Save ForNeVeR/a0da2ffc4fd569c5d5e24ed19493321c to your computer and use it in GitHub Desktop.
Shows how interface maps work for implicit and explicit interface implementations in C#.
using System;
using System.Reflection;
public interface ITest
{
void TestFun(int arg);
}
public class TestClass : ITest
{
void ITest.TestFun(int a)
{
Console.WriteLine("xxx");
}
}
class Program
{
public static void Main(string[] args)
{
InterfaceMapping interfaceMap = typeof(TestClass).GetInterfaceMap(typeof(ITest));
MethodInfo methodInfo = interfaceMap.TargetMethods[0];
MethodInfo methodInfo2 = interfaceMap.InterfaceMethods[0];
Console.WriteLine(methodInfo.Name); // ITest.TestFun
Console.WriteLine(methodInfo2.Name); // TestFun
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment