Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Last active July 27, 2022 14:30
Show Gist options
  • Save mattwarren/be21d80a016043ea5c462415b81d9b69 to your computer and use it in GitHub Desktop.
Save mattwarren/be21d80a016043ea5c462415b81d9b69 to your computer and use it in GitHub Desktop.
private static void GetRuntimeCacheInfo(Type typeToTest)
{
var expectedType = "System.RuntimeType";
if (typeToTest.GetType().FullName != expectedType)
{
Console.WriteLine(" Unexpected type, Expected: {0}, Got: {1}", expectedType, typeToTest.GetType().FullName);
return;
}
Console.WriteLine(" Type: {0}", typeToTest.FullName);
Console.WriteLine(" Reflection Type: {0} (BaseType: {1})", typeToTest.GetType(), typeToTest.GetType().BaseType);
var runtimeTypeCache = typeToTest.GetType()
.GetProperty("Cache", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.GetGetMethod(nonPublic: true)
.Invoke(typeToTest, null);
if (runtimeTypeCache.GetType().FullName != "System.RuntimeType+RuntimeTypeCache")
{
Console.WriteLine(" Unexpected type for 'Cache', Expected: {0}, Got: {1}",
"System.RuntimeType+RuntimeTypeCache", runtimeTypeCache.GetType().FullName);
return;
}
var fieldInfoCache = runtimeTypeCache.GetType()
.GetField("m_fieldInfoCache", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.GetValue(runtimeTypeCache);
if (fieldInfoCache == null)
{
Console.WriteLine(" m_fieldInfoCache is null, cache has not been initialised yet");
return;
}
var isCacheComplete = fieldInfoCache.GetType()
.GetField("m_cacheComplete", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.GetValue(fieldInfoCache);
var members = fieldInfoCache.GetType()
.GetField("m_allMembers", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.GetValue(fieldInfoCache) as Array;
Console.WriteLine(" RuntimeTypeCache: {0}, m_cacheComplete = {1}, {2} items in cache", runtimeTypeCache, isCacheComplete, members.Length);
for (int i = 0; i < members.Length; i++)
{
var item = members.GetValue(i);
var fieldInfo = item as FieldInfo;
Console.WriteLine(" [{0}] - {1} - {2}", i, item, String.Join(", ", fieldInfo?.Attributes));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment