Last active
January 12, 2021 07:23
-
-
Save Kir-Antipov/c8079a9676939c1b789c0ef010e467ba to your computer and use it in GitHub Desktop.
Get the interfaces implemented by the object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A little bit faster then standard Type.GetInterfaces() | |
public static Type[] GetInterfaces(Type ObjectType) | |
{ | |
unsafe | |
{ | |
void* methodTable = ObjectType.TypeHandle.Value.ToPointer(); | |
int count = ((ushort*)methodTable)[7]; | |
IntPtr* interfaces = (IntPtr*)((IntPtr*)methodTable)[9].ToPointer(); | |
Type[] result = new Type[count]; | |
for (int i = 0; i < count; ++interfaces, ++i) | |
result[i] = GetTypeFromHandleUnsafe(*interfaces); | |
return result; | |
} | |
} | |
private static readonly Func<IntPtr, Type> GetTypeFromHandleUnsafe = (Func<IntPtr, Type>)typeof(Type).GetMethod("GetTypeFromHandleUnsafe", BindingFlags.Static | BindingFlags.NonPublic).CreateDelegate(typeof(Func<IntPtr, Type>), null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment