Last active
May 26, 2021 00:09
-
-
Save jaredpar/3cb935be4eadfbddbc16c12ac4b2ffeb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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