Skip to content

Instantly share code, notes, and snippets.

@jaredpar
Last active May 26, 2021 00:09
Show Gist options
  • Select an option

  • Save jaredpar/3cb935be4eadfbddbc16c12ac4b2ffeb to your computer and use it in GitHub Desktop.

Select an option

Save jaredpar/3cb935be4eadfbddbc16c12ac4b2ffeb to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
internal static class Extensions
{
/// <summary>
/// Reset the internal RuntimeBinder instance to free the symbol table, etc.
/// Uses reflection to execute:
/// <code>
/// var binder = RuntimeBinder.GetInstance();
/// lock (binder.m_bindLock)
/// {
/// binder.Reset();
/// }
/// </code>
/// </summary>
internal static bool ResetRuntimeBinder()
{
var type = Type.GetType("Microsoft.CSharp.RuntimeBinder.RuntimeBinder, Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false);
if (type == null)
{
return false;
}
var method = type.GetMethod("GetInstance", BindingFlags.Public | BindingFlags.Static);
if (method == null)
{
return false;
}
var runtimeBinder = method.Invoke(null, new object[0]);
var field = type.GetField("m_bindLock", BindingFlags.NonPublic | BindingFlags.Instance);
method = type.GetMethod("Reset", BindingFlags.NonPublic | BindingFlags.Instance);
if (field == null || method == null)
{
return false;
}
var lockObj = field.GetValue(runtimeBinder);
lock (lockObj)
{
method.Invoke(runtimeBinder, new object[0]);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment