Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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