using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Dynamic; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Text; using System.Threading; using Microsoft.CodeAnalysis; using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Collections.Generic; using MonoMod.Utils; using MonoMod.Utils.Cil; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETCoreApp,Version=v5.0", FrameworkDisplayName = "")] [assembly: InternalsVisibleTo("MonoMod.Utils.Cil.ILGeneratorProxy")] [assembly: AssemblyCompany("0x0ade")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright 2022 0x0ade")] [assembly: AssemblyDescription("Utilities and smaller MonoMod \"components\" (f.e. ModInterop, DynDll, DynData). Can be used for your own mods. Required by all other MonoMod components.")] [assembly: AssemblyFileVersion("22.5.1.1")] [assembly: AssemblyInformationalVersion("22.05.01.01")] [assembly: AssemblyProduct("MonoMod.Utils")] [assembly: AssemblyTitle("MonoMod.Utils")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("22.5.1.1")] [module: UnverifiableCode] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } } internal static class MultiTargetShims { private static readonly object[] _NoArgs = new object[0]; public static string Replace(this string self, string oldValue, string newValue, StringComparison comparison) { return self.Replace(oldValue, newValue); } public static bool Contains(this string self, string value, StringComparison comparison) { return self.Contains(value); } public static int GetHashCode(this string self, StringComparison comparison) { return self.GetHashCode(); } public static int IndexOf(this string self, char value, StringComparison comparison) { return self.IndexOf(value); } public static int IndexOf(this string self, string value, StringComparison comparison) { return self.IndexOf(value); } public static Module[] GetModules(this Assembly asm) { return asm.Modules.ToArray(); } public static Module GetModule(this Assembly asm, string name) { return asm.Modules.FirstOrDefault((Module module) => module.Name == name); } public static byte[] GetBuffer(this MemoryStream ms) { long position = ms.Position; byte[] array = new byte[ms.Length]; ms.Read(array, 0, array.Length); ms.Position = position; return array; } public static TypeReference GetConstraintType(this GenericParameterConstraint constraint) { return constraint.ConstraintType; } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] public class IgnoresAccessChecksToAttribute : Attribute { public string AssemblyName { get; } public IgnoresAccessChecksToAttribute(string assemblyName) { AssemblyName = assemblyName; } } } namespace MonoMod { internal static class MMDbgLog { public static readonly string Tag; public static TextWriter Writer; public static bool Debugging; static MMDbgLog() { Tag = typeof(MMDbgLog).Assembly.GetName().Name; if (Environment.GetEnvironmentVariable("MONOMOD_DBGLOG") == "1" || (Environment.GetEnvironmentVariable("MONOMOD_DBGLOG")?.ToLower(CultureInfo.InvariantCulture)?.Contains(Tag.ToLower(CultureInfo.InvariantCulture), StringComparison.Ordinal)).GetValueOrDefault()) { Start(); } } public static void WaitForDebugger() { if (!Debugging) { Debugging = true; Debugger.Launch(); Thread.Sleep(6000); Debugger.Break(); } } public static void Start() { if (Writer != null) { return; } string text = Environment.GetEnvironmentVariable("MONOMOD_DBGLOG_PATH"); if (text == "-") { Writer = Console.Out; return; } if (string.IsNullOrEmpty(text)) { text = "mmdbglog.txt"; } text = Path.GetFullPath(Path.GetFileNameWithoutExtension(text) + "-" + Tag + Path.GetExtension(text)); try { if (File.Exists(text)) { File.Delete(text); } } catch { } try { string directoryName = Path.GetDirectoryName(text); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } Writer = new StreamWriter(new FileStream(text, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete), Encoding.UTF8); } catch { } } public static void Log(string str) { TextWriter writer = Writer; if (writer != null) { writer.WriteLine(str); writer.Flush(); } } public static T Log(string str, T value) { TextWriter writer = Writer; if (writer == null) { return value; } writer.WriteLine(string.Format(CultureInfo.InvariantCulture, str, value)); writer.Flush(); return value; } } } namespace MonoMod.ModInterop { [AttributeUsage(AttributeTargets.Class)] public sealed class ModExportNameAttribute : Attribute { public string Name; public ModExportNameAttribute(string name) { Name = name; } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field)] public sealed class ModImportNameAttribute : Attribute { public string Name; public ModImportNameAttribute(string name) { Name = name; } } public static class ModInteropManager { private static HashSet Registered = new HashSet(); private static Dictionary> Methods = new Dictionary>(); private static List Fields = new List(); public static void ModInterop(this Type type) { if (Registered.Contains(type)) { return; } Registered.Add(type); string name = type.Assembly.GetName().Name; object[] customAttributes = type.GetCustomAttributes(typeof(ModExportNameAttribute), inherit: false); for (int i = 0; i < customAttributes.Length; i++) { name = ((ModExportNameAttribute)customAttributes[i]).Name; } FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public); foreach (FieldInfo fieldInfo in fields) { if (typeof(Delegate).IsAssignableFrom(fieldInfo.FieldType)) { Fields.Add(fieldInfo); } } MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public); foreach (MethodInfo method in methods) { method.RegisterModExport(); method.RegisterModExport(name); } foreach (FieldInfo field in Fields) { if (!Methods.TryGetValue(field.GetModImportName(), out var value)) { field.SetValue(null, null); continue; } bool flag = false; foreach (MethodInfo item in value) { try { field.SetValue(null, Delegate.CreateDelegate(field.FieldType, null, item)); flag = true; } catch { continue; } break; } if (!flag) { field.SetValue(null, null); } } } public static void RegisterModExport(this MethodInfo method, string prefix = null) { if (!method.IsPublic || !method.IsStatic) { throw new MemberAccessException("Utility must be public static"); } string text = method.Name; if (!string.IsNullOrEmpty(prefix)) { text = prefix + "." + text; } if (!Methods.TryGetValue(text, out var value)) { value = (Methods[text] = new List()); } if (!value.Contains(method)) { value.Add(method); } } private static string GetModImportName(this FieldInfo field) { object[] customAttributes = field.GetCustomAttributes(typeof(ModImportNameAttribute), inherit: false); int num = 0; if (num < customAttributes.Length) { return ((ModImportNameAttribute)customAttributes[num]).Name; } customAttributes = field.DeclaringType.GetCustomAttributes(typeof(ModImportNameAttribute), inherit: false); num = 0; if (num < customAttributes.Length) { return ((ModImportNameAttribute)customAttributes[num]).Name + "." + field.Name; } return field.Name; } } } namespace MonoMod.Utils { public sealed class DynamicData : DynamicObject, IEnumerable>, IEnumerable { private class _Cache_ { public readonly Dictionary> Getters = new Dictionary>(); public readonly Dictionary> Setters = new Dictionary>(); public readonly Dictionary> Methods = new Dictionary>(); public _Cache_(Type targetType) { bool flag = true; while (targetType != null && targetType != targetType.BaseType) { FieldInfo[] fields = targetType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo field in fields) { string name = field.Name; if (!Getters.ContainsKey(name) && !Setters.ContainsKey(name)) { Getters[name] = (object obj) => field.GetValue(obj); Setters[name] = delegate(object obj, object value) { field.SetValue(obj, value); }; } } PropertyInfo[] properties = targetType.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (PropertyInfo propertyInfo in properties) { string name2 = propertyInfo.Name; MethodInfo get = propertyInfo.GetGetMethod(nonPublic: true); if (get != null && !Getters.ContainsKey(name2)) { Getters[name2] = (object obj) => get.Invoke(obj, _NoArgs); } MethodInfo set = propertyInfo.GetSetMethod(nonPublic: true); if (set != null && !Setters.ContainsKey(name2)) { Setters[name2] = delegate(object obj, object value) { set.Invoke(obj, new object[1] { value }); }; } } Dictionary dictionary = new Dictionary(); MethodInfo[] methods = targetType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (MethodInfo methodInfo in methods) { string name3 = methodInfo.Name; if (flag || !Methods.ContainsKey(name3)) { if (dictionary.ContainsKey(name3)) { dictionary[name3] = null; } else { dictionary[name3] = methodInfo; } } } foreach (KeyValuePair item in dictionary) { if (!(item.Value == null) && !item.Value.IsGenericMethod) { FastReflectionDelegate cb = item.Value.GetFastDelegate(); Methods[item.Key] = (object target, object[] args) => cb(target, args); } } flag = false; targetType = targetType.BaseType; } } } private class _Data_ { public readonly Dictionary> Getters = new Dictionary>(); public readonly Dictionary> Setters = new Dictionary>(); public readonly Dictionary> Methods = new Dictionary>(); public readonly Dictionary Data = new Dictionary(); public _Data_(Type type) { _ = type == null; } } private static readonly object[] _NoArgs = new object[0]; private static readonly Dictionary _CacheMap = new Dictionary(); private static readonly Dictionary _DataStaticMap = new Dictionary(); private static readonly ConditionalWeakTable _DataMap = new ConditionalWeakTable(); private static readonly ConditionalWeakTable _DynamicDataMap = new ConditionalWeakTable(); private readonly WeakReference Weak; private object KeepAlive; private readonly _Cache_ _Cache; private readonly _Data_ _Data; public Dictionary> Getters => _Data.Getters; public Dictionary> Setters => _Data.Setters; public Dictionary> Methods => _Data.Methods; public Dictionary Data => _Data.Data; public bool IsAlive { get { if (Weak != null) { return Weak.SafeGetIsAlive(); } return true; } } public object Target => Weak?.SafeGetTarget(); public Type TargetType { get; private set; } public static event Action OnInitialize; public DynamicData(Type type) : this(type, null, keepAlive: false) { } public DynamicData(object obj) : this(obj.GetType(), obj, keepAlive: true) { } public DynamicData(Type type, object obj) : this(type, obj, keepAlive: true) { } public DynamicData(Type type, object obj, bool keepAlive) { TargetType = type; lock (_CacheMap) { if (!_CacheMap.TryGetValue(type, out _Cache)) { _Cache = new _Cache_(type); _CacheMap.Add(type, _Cache); } } if (obj != null) { lock (_DataMap) { if (!_DataMap.TryGetValue(obj, out _Data)) { _Data = new _Data_(type); _DataMap.Add(obj, _Data); } } Weak = new WeakReference(obj); if (keepAlive) { KeepAlive = obj; } } else { lock (_DataStaticMap) { if (!_DataStaticMap.TryGetValue(type, out _Data)) { _Data = new _Data_(type); _DataStaticMap.Add(type, _Data); } } } DynamicData.OnInitialize?.Invoke(this, type, obj); } public static DynamicData For(object obj) { lock (_DynamicDataMap) { if (!_DynamicDataMap.TryGetValue(obj, out var value)) { value = new DynamicData(obj); _DynamicDataMap.Add(obj, value); } return value; } } public static Func New(params object[] args) { T target = (T)Activator.CreateInstance(typeof(T), BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, args, null); return (object other) => Set(target, other); } public static Func New(Type type, params object[] args) { object target = Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, args, null); return (object other) => Set(target, other); } public static Func NewWrap(params object[] args) { T target = (T)Activator.CreateInstance(typeof(T), BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, args, null); return (object other) => Wrap(target, other); } public static Func NewWrap(Type type, params object[] args) { object target = Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, args, null); return (object other) => Wrap(target, other); } public static dynamic Wrap(object target, object other = null) { DynamicData dynamicData = new DynamicData(target); dynamicData.CopyFrom(other); return dynamicData; } public static T Set(T target, object other = null) { return (T)Set((object)target, other); } public static object Set(object target, object other = null) { DynamicData dynamicData = new DynamicData(target); dynamicData.CopyFrom(other); return dynamicData.Target; } public void RegisterProperty(string name, Func getter, Action setter) { Getters[name] = getter; Setters[name] = setter; } public void UnregisterProperty(string name) { Getters.Remove(name); Setters.Remove(name); } public void RegisterMethod(string name, Func cb) { Methods[name] = cb; } public void UnregisterMethod(string name) { Methods.Remove(name); } public void CopyFrom(object other) { if (other != null) { PropertyInfo[] properties = other.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo propertyInfo in properties) { Set(propertyInfo.Name, propertyInfo.GetValue(other, null)); } } } public object Get(string name) { TryGet(name, out var value); return value; } public bool TryGet(string name, out object value) { object target = Target; if (_Data.Getters.TryGetValue(name, out var value2)) { value = value2(target); return true; } if (_Cache.Getters.TryGetValue(name, out value2)) { value = value2(target); return true; } if (_Data.Data.TryGetValue(name, out value)) { return true; } return false; } public T Get(string name) { return (T)Get(name); } public bool TryGet(string name, out T value) { object value2; bool result = TryGet(name, out value2); value = (T)value2; return result; } public void Set(string name, object value) { object target = Target; if (_Data.Setters.TryGetValue(name, out var value2)) { value2(target, value); } else if (_Cache.Setters.TryGetValue(name, out value2)) { value2(target, value); } else { Data[name] = value; } } public void Add(KeyValuePair kvp) { Set(kvp.Key, kvp.Value); } public void Add(string key, object value) { Set(key, value); } public object Invoke(string name, params object[] args) { TryInvoke(name, args, out var result); return result; } public bool TryInvoke(string name, object[] args, out object result) { if (_Data.Methods.TryGetValue(name, out var value)) { result = value(Target, args); return true; } if (_Cache.Methods.TryGetValue(name, out value)) { result = value(Target, args); return true; } result = null; return false; } public T Invoke(string name, params object[] args) { return (T)Invoke(name, args); } public bool TryInvoke(string name, object[] args, out T result) { object result2; bool result3 = TryInvoke(name, args, out result2); result = (T)result2; return result3; } private void Dispose(bool disposing) { KeepAlive = null; } ~DynamicData() { Dispose(disposing: false); } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } public override IEnumerable GetDynamicMemberNames() { return _Data.Data.Keys.Union(_Data.Getters.Keys).Union(_Data.Setters.Keys).Union(_Data.Methods.Keys) .Union(_Cache.Getters.Keys) .Union(_Cache.Setters.Keys) .Union(_Cache.Methods.Keys); } public override bool TryConvert(ConvertBinder binder, out object result) { if (TargetType.IsCompatible(binder.Type) || TargetType.IsCompatible(binder.ReturnType) || binder.Type == typeof(object) || binder.ReturnType == typeof(object)) { result = Target; return true; } if (typeof(DynamicData).IsCompatible(binder.Type) || typeof(DynamicData).IsCompatible(binder.ReturnType)) { result = this; return true; } result = null; return false; } public override bool TryGetMember(GetMemberBinder binder, out object result) { if (Methods.ContainsKey(binder.Name)) { result = null; return false; } result = Get(binder.Name); return true; } public override bool TrySetMember(SetMemberBinder binder, object value) { Set(binder.Name, value); return true; } public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { return TryInvoke(binder.Name, args, out result); } public IEnumerator> GetEnumerator() { foreach (string item in _Data.Data.Keys.Union(_Data.Getters.Keys).Union(_Data.Setters.Keys).Union(_Cache.Getters.Keys) .Union(_Cache.Setters.Keys)) { yield return new KeyValuePair(item, Get(item)); } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public sealed class DynData : IDisposable where TTarget : class { private class _Data_ : IDisposable { public readonly Dictionary> Getters = new Dictionary>(); public readonly Dictionary> Setters = new Dictionary>(); public readonly Dictionary Data = new Dictionary(); public readonly HashSet Disposable = new HashSet(); ~_Data_() { Dispose(); } public void Dispose() { lock (Data) { if (Data.Count == 0) { return; } foreach (string item in Disposable) { if (Data.TryGetValue(item, out var value) && value is IDisposable disposable) { disposable.Dispose(); } } Disposable.Clear(); Data.Clear(); } } } private static int CreationsInProgress; private static readonly object[] _NoArgs; private static readonly _Data_ _DataStatic; private static readonly ConditionalWeakTable _DataMap; private static readonly Dictionary> _SpecialGetters; private static readonly Dictionary> _SpecialSetters; private readonly WeakReference Weak; private TTarget KeepAlive; private readonly _Data_ _Data; public Dictionary> Getters => _Data.Getters; public Dictionary> Setters => _Data.Setters; public Dictionary Data => _Data.Data; public bool IsAlive { get { if (Weak != null) { return Weak.SafeGetIsAlive(); } return true; } } public TTarget Target => Weak?.SafeGetTarget() as TTarget; public object this[string name] { get { if (_SpecialGetters.TryGetValue(name, out var value) || Getters.TryGetValue(name, out value)) { return value(Weak?.SafeGetTarget() as TTarget); } if (Data.TryGetValue(name, out var value2)) { return value2; } return null; } set { if (_SpecialSetters.TryGetValue(name, out var value2) || Setters.TryGetValue(name, out value2)) { value2(Weak?.SafeGetTarget() as TTarget, value); return; } object obj; if (_Data.Disposable.Contains(name) && (obj = this[name]) != null && obj is IDisposable disposable) { disposable.Dispose(); } Data[name] = value; } } public static event Action, TTarget> OnInitialize; static DynData() { CreationsInProgress = 0; _NoArgs = new object[0]; _DataStatic = new _Data_(); _DataMap = new ConditionalWeakTable(); _SpecialGetters = new Dictionary>(); _SpecialSetters = new Dictionary>(); FieldInfo[] fields = typeof(TTarget).GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo field in fields) { string name = field.Name; _SpecialGetters[name] = (TTarget obj) => field.GetValue(obj); _SpecialSetters[name] = delegate(TTarget obj, object value) { field.SetValue(obj, value); }; } PropertyInfo[] properties = typeof(TTarget).GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (PropertyInfo propertyInfo in properties) { string name2 = propertyInfo.Name; MethodInfo get = propertyInfo.GetGetMethod(nonPublic: true); if (get != null) { _SpecialGetters[name2] = (TTarget obj) => get.Invoke(obj, _NoArgs); } MethodInfo set = propertyInfo.GetSetMethod(nonPublic: true); if (set != null) { _SpecialSetters[name2] = delegate(TTarget obj, object value) { set.Invoke(obj, new object[1] { value }); }; } } } public DynData() : this((TTarget)null, keepAlive: false) { } public DynData(TTarget obj) : this(obj, keepAlive: true) { } public DynData(TTarget obj, bool keepAlive) { if (obj != null) { WeakReference weak = new WeakReference(obj); CreationsInProgress++; lock (_DataMap) { if (!_DataMap.TryGetValue(obj, out _Data)) { _Data = new _Data_(); _DataMap.Add(obj, _Data); } } CreationsInProgress--; Weak = weak; if (keepAlive) { KeepAlive = obj; } } else { _Data = _DataStatic; } DynData.OnInitialize?.Invoke(this, obj); } public T Get(string name) { return (T)this[name]; } public void Set(string name, T value) { this[name] = value; } public void RegisterProperty(string name, Func getter, Action setter) { Getters[name] = getter; Setters[name] = setter; } public void UnregisterProperty(string name) { Getters.Remove(name); Setters.Remove(name); } private void Dispose(bool disposing) { KeepAlive = null; } ~DynData() { Dispose(disposing: false); } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } } public static class Extensions { private static readonly Type t_StateMachineAttribute = typeof(object).Assembly.GetType("System.Runtime.CompilerServices.StateMachineAttribute"); private static readonly PropertyInfo p_StateMachineType = t_StateMachineAttribute?.GetProperty("StateMachineType"); private static readonly Type t_Code = typeof(Code); private static readonly Type t_OpCodes = typeof(OpCodes); private static readonly Dictionary _ToLongOp = new Dictionary(); private static readonly Dictionary _ToShortOp = new Dictionary(); private static readonly object[] _NoArgs = new object[0]; private static readonly Dictionary fmap_mono_assembly = new Dictionary(); private static readonly bool _MonoAssemblyNameHasArch = new AssemblyName("Dummy, ProcessorArchitecture=MSIL").ProcessorArchitecture == ProcessorArchitecture.MSIL; private static readonly Type _RTDynamicMethod = typeof(DynamicMethod).GetNestedType("RTDynamicMethod", BindingFlags.Public | BindingFlags.NonPublic); private static readonly Type t_ParamArrayAttribute = typeof(ParamArrayAttribute); private static readonly FieldInfo f_GenericParameter_position = typeof(GenericParameter).GetField("position", BindingFlags.Instance | BindingFlags.NonPublic); private static readonly FieldInfo f_GenericParameter_type = typeof(GenericParameter).GetField("type", BindingFlags.Instance | BindingFlags.NonPublic); private static readonly Dictionary _GetManagedSizeCache = new Dictionary { { typeof(void), 0 } }; private static MethodInfo _GetManagedSizeHelper; private static readonly Dictionary> _GetLdftnPointerCache = new Dictionary>(); public static string ToHexadecimalString(this byte[] data) { return BitConverter.ToString(data).Replace("-", string.Empty, StringComparison.Ordinal); } public static T InvokePassing(this MulticastDelegate md, T val, params object[] args) { if ((object)md == null) { return val; } object[] array = new object[args.Length + 1]; array[0] = val; Array.Copy(args, 0, array, 1, args.Length); Delegate[] invocationList = md.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { array[0] = invocationList[i].DynamicInvoke(array); } return (T)array[0]; } public static bool InvokeWhileTrue(this MulticastDelegate md, params object[] args) { if ((object)md == null) { return true; } Delegate[] invocationList = md.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { if (!(bool)invocationList[i].DynamicInvoke(args)) { return false; } } return true; } public static bool InvokeWhileFalse(this MulticastDelegate md, params object[] args) { if ((object)md == null) { return false; } Delegate[] invocationList = md.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { if ((bool)invocationList[i].DynamicInvoke(args)) { return true; } } return false; } public static T InvokeWhileNull(this MulticastDelegate md, params object[] args) where T : class { if ((object)md == null) { return null; } Delegate[] invocationList = md.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { T val = (T)invocationList[i].DynamicInvoke(args); if (val != null) { return val; } } return null; } public static string SpacedPascalCase(this string input) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < input.Length; i++) { char c = input[i]; if (i > 0 && char.IsUpper(c)) { stringBuilder.Append(' '); } stringBuilder.Append(c); } return stringBuilder.ToString(); } public static string ReadNullTerminatedString(this BinaryReader stream) { string text = ""; char c; while ((c = stream.ReadChar()) != 0) { text += c; } return text; } public static void WriteNullTerminatedString(this BinaryWriter stream, string text) { if (text != null) { foreach (char ch in text) { stream.Write(ch); } } stream.Write('\0'); } public static T CastDelegate(this Delegate source) where T : class { return source.CastDelegate(typeof(T)) as T; } public static Delegate CastDelegate(this Delegate source, Type type) { if ((object)source == null) { return null; } Delegate[] invocationList = source.GetInvocationList(); if (invocationList.Length == 1) { return invocationList[0].Method.CreateDelegate(type, invocationList[0].Target); } Delegate[] array = new Delegate[invocationList.Length]; for (int i = 0; i < invocationList.Length; i++) { array[i] = invocationList[i].CastDelegate(type); } return Delegate.Combine(array); } public static bool TryCastDelegate(this Delegate source, out T result) where T : class { if (source is T val) { result = val; return true; } Delegate result2; bool result3 = source.TryCastDelegate(typeof(T), out result2); result = result2 as T; return result3; } public static bool TryCastDelegate(this Delegate source, Type type, out Delegate result) { result = null; if ((object)source == null) { return false; } try { Delegate[] invocationList = source.GetInvocationList(); if (invocationList.Length == 1) { result = invocationList[0].Method.CreateDelegate(type, invocationList[0].Target); return true; } Delegate[] array = new Delegate[invocationList.Length]; for (int i = 0; i < invocationList.Length; i++) { array[i] = invocationList[i].CastDelegate(type); } result = Delegate.Combine(array); return true; } catch { return false; } } public static void LogDetailed(this Exception e, string tag = null) { if (tag == null) { Console.WriteLine("--------------------------------"); Console.WriteLine("Detailed exception log:"); } for (Exception ex = e; ex != null; ex = ex.InnerException) { Console.WriteLine("--------------------------------"); Console.WriteLine(ex.GetType().FullName + ": " + ex.Message + "\n" + ex.StackTrace); if (ex is ReflectionTypeLoadException ex2) { for (int i = 0; i < ex2.Types.Length; i++) { Console.WriteLine("ReflectionTypeLoadException.Types[" + i + "]: " + ex2.Types[i]); } for (int j = 0; j < ex2.LoaderExceptions.Length; j++) { ex2.LoaderExceptions[j].LogDetailed(tag + ((tag == null) ? "" : ", ") + "rtle:" + j); } } if (ex is TypeLoadException) { Console.WriteLine("TypeLoadException.TypeName: " + ((TypeLoadException)ex).TypeName); } if (ex is BadImageFormatException) { Console.WriteLine("BadImageFormatException.FileName: " + ((BadImageFormatException)ex).FileName); } } } public static MethodInfo GetStateMachineTarget(this MethodInfo method) { if (p_StateMachineType == null) { return null; } object[] customAttributes = method.GetCustomAttributes(inherit: false); for (int i = 0; i < customAttributes.Length; i++) { Attribute attribute = (Attribute)customAttributes[i]; if (t_StateMachineAttribute.IsCompatible(attribute.GetType())) { return (p_StateMachineType.GetValue(attribute, null) as Type)?.GetMethod("MoveNext", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } } return null; } public static MethodBase GetActualGenericMethodDefinition(this MethodInfo method) { return (method.IsGenericMethod ? method.GetGenericMethodDefinition() : method).GetUnfilledMethodOnGenericType(); } public static MethodBase GetUnfilledMethodOnGenericType(this MethodBase method) { if (method.DeclaringType != null && method.DeclaringType.IsGenericType) { Type genericTypeDefinition = method.DeclaringType.GetGenericTypeDefinition(); method = MethodBase.GetMethodFromHandle(method.MethodHandle, genericTypeDefinition.TypeHandle); } return method; } public static bool Is(this MemberReference member, string fullName) { if (member == null) { return false; } return member.FullName.Replace("+", "/", StringComparison.Ordinal) == fullName.Replace("+", "/", StringComparison.Ordinal); } public static bool Is(this MemberReference member, string typeFullName, string name) { if (member == null) { return false; } if (((MemberReference)member.DeclaringType).FullName.Replace("+", "/", StringComparison.Ordinal) == typeFullName.Replace("+", "/", StringComparison.Ordinal)) { return member.Name == name; } return false; } public static bool Is(this MemberReference member, Type type, string name) { if (member == null) { return false; } if (((MemberReference)member.DeclaringType).FullName.Replace("+", "/", StringComparison.Ordinal) == type.FullName.Replace("+", "/", StringComparison.Ordinal)) { return member.Name == name; } return false; } public static bool Is(this MethodReference method, string fullName) { if (method == null) { return false; } if (fullName.Contains(" ", StringComparison.Ordinal)) { if (method.GetID(null, null, withType: true, simple: true).Replace("+", "/", StringComparison.Ordinal) == fullName.Replace("+", "/", StringComparison.Ordinal)) { return true; } if (method.GetID().Replace("+", "/", StringComparison.Ordinal) == fullName.Replace("+", "/", StringComparison.Ordinal)) { return true; } } return ((MemberReference)method).FullName.Replace("+", "/", StringComparison.Ordinal) == fullName.Replace("+", "/", StringComparison.Ordinal); } public static bool Is(this MethodReference method, string typeFullName, string name) { if (method == null) { return false; } if (name.Contains(" ", StringComparison.Ordinal) && ((MemberReference)((MemberReference)method).DeclaringType).FullName.Replace("+", "/", StringComparison.Ordinal) == typeFullName.Replace("+", "/", StringComparison.Ordinal) && method.GetID(null, null, withType: false).Replace("+", "/", StringComparison.Ordinal) == name.Replace("+", "/", StringComparison.Ordinal)) { return true; } if (((MemberReference)((MemberReference)method).DeclaringType).FullName.Replace("+", "/", StringComparison.Ordinal) == typeFullName.Replace("+", "/", StringComparison.Ordinal)) { return ((MemberReference)method).Name == name; } return false; } public static bool Is(this MethodReference method, Type type, string name) { if (method == null) { return false; } if (name.Contains(" ", StringComparison.Ordinal) && ((MemberReference)((MemberReference)method).DeclaringType).FullName.Replace("+", "/", StringComparison.Ordinal) == type.FullName.Replace("+", "/", StringComparison.Ordinal) && method.GetID(null, null, withType: false).Replace("+", "/", StringComparison.Ordinal) == name.Replace("+", "/", StringComparison.Ordinal)) { return true; } if (((MemberReference)((MemberReference)method).DeclaringType).FullName.Replace("+", "/", StringComparison.Ordinal) == type.FullName.Replace("+", "/", StringComparison.Ordinal)) { return ((MemberReference)method).Name == name; } return false; } public static void ReplaceOperands(this ILProcessor il, object from, object to) { //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Unknown result type (might be due to invalid IL or missing references) Enumerator enumerator = il.Body.Instructions.GetEnumerator(); try { while (enumerator.MoveNext()) { Instruction current = enumerator.Current; if (current.Operand?.Equals(from) ?? (from == null)) { current.Operand = to; } } } finally { ((IDisposable)enumerator).Dispose(); } } public static FieldReference Import(this ILProcessor il, FieldInfo field) { return ((MemberReference)il.Body.Method).Module.ImportReference(field); } public static MethodReference Import(this ILProcessor il, MethodBase method) { return ((MemberReference)il.Body.Method).Module.ImportReference(method); } public static TypeReference Import(this ILProcessor il, Type type) { return ((MemberReference)il.Body.Method).Module.ImportReference(type); } public static MemberReference Import(this ILProcessor il, MemberInfo member) { if (member == null) { throw new ArgumentNullException("member"); } if (!(member is FieldInfo field)) { if (!(member is MethodBase method)) { if (member is Type type) { return (MemberReference)(object)il.Import(type); } throw new NotSupportedException("Unsupported member type " + member.GetType().FullName); } return (MemberReference)(object)il.Import(method); } return (MemberReference)(object)il.Import(field); } public static Instruction Create(this ILProcessor il, OpCode opcode, FieldInfo field) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return il.Create(opcode, il.Import(field)); } public static Instruction Create(this ILProcessor il, OpCode opcode, MethodBase method) { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0009: Unknown result type (might be due to invalid IL or missing references) if (method is DynamicMethod) { return il.Create(opcode, (object)method); } return il.Create(opcode, il.Import(method)); } public static Instruction Create(this ILProcessor il, OpCode opcode, Type type) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return il.Create(opcode, il.Import(type)); } public static Instruction Create(this ILProcessor il, OpCode opcode, object operand) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) Instruction obj = il.Create(OpCodes.Nop); obj.OpCode = opcode; obj.Operand = operand; return obj; } public static Instruction Create(this ILProcessor il, OpCode opcode, MemberInfo member) { //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) if (member == null) { throw new ArgumentNullException("member"); } if (!(member is FieldInfo field)) { if (!(member is MethodBase method)) { if (member is Type type) { return il.Create(opcode, type); } throw new NotSupportedException("Unsupported member type " + member.GetType().FullName); } return il.Create(opcode, method); } return il.Create(opcode, field); } public static void Emit(this ILProcessor il, OpCode opcode, FieldInfo field) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) il.Emit(opcode, il.Import(field)); } public static void Emit(this ILProcessor il, OpCode opcode, MethodBase method) { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0009: Unknown result type (might be due to invalid IL or missing references) if (method is DynamicMethod) { il.Emit(opcode, (object)method); } else { il.Emit(opcode, il.Import(method)); } } public static void Emit(this ILProcessor il, OpCode opcode, Type type) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) il.Emit(opcode, il.Import(type)); } public static void Emit(this ILProcessor il, OpCode opcode, MemberInfo member) { //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) if (member == null) { throw new ArgumentNullException("member"); } if (!(member is FieldInfo field)) { if (!(member is MethodBase method)) { if (!(member is Type type)) { throw new NotSupportedException("Unsupported member type " + member.GetType().FullName); } il.Emit(opcode, type); } else { il.Emit(opcode, method); } } else { il.Emit(opcode, field); } } public static void Emit(this ILProcessor il, OpCode opcode, object operand) { //IL_0002: Unknown result type (might be due to invalid IL or missing references) il.Append(il.Create(opcode, operand)); } public static TypeDefinition SafeResolve(this TypeReference r) { try { return r.Resolve(); } catch { return null; } } public static FieldDefinition SafeResolve(this FieldReference r) { try { return r.Resolve(); } catch { return null; } } public static MethodDefinition SafeResolve(this MethodReference r) { try { return r.Resolve(); } catch { return null; } } public static PropertyDefinition SafeResolve(this PropertyReference r) { try { return r.Resolve(); } catch { return null; } } public static CustomAttribute GetCustomAttribute(this ICustomAttributeProvider cap, string attribute) { //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (cap == null || !cap.HasCustomAttributes) { return null; } Enumerator enumerator = cap.CustomAttributes.GetEnumerator(); try { while (enumerator.MoveNext()) { CustomAttribute current = enumerator.Current; if (((MemberReference)current.AttributeType).FullName == attribute) { return current; } } } finally { ((IDisposable)enumerator).Dispose(); } return null; } public static bool HasCustomAttribute(this ICustomAttributeProvider cap, string attribute) { return cap.GetCustomAttribute(attribute) != null; } public static int GetInt(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_0052: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_0061: Unknown result type (might be due to invalid IL or missing references) //IL_0062: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_007f: Unknown result type (might be due to invalid IL or missing references) //IL_0080: Unknown result type (might be due to invalid IL or missing references) //IL_008e: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_009e: Unknown result type (might be due to invalid IL or missing references) OpCode opCode = instr.OpCode; if (opCode == OpCodes.Ldc_I4_M1) { return -1; } if (opCode == OpCodes.Ldc_I4_0) { return 0; } if (opCode == OpCodes.Ldc_I4_1) { return 1; } if (opCode == OpCodes.Ldc_I4_2) { return 2; } if (opCode == OpCodes.Ldc_I4_3) { return 3; } if (opCode == OpCodes.Ldc_I4_4) { return 4; } if (opCode == OpCodes.Ldc_I4_5) { return 5; } if (opCode == OpCodes.Ldc_I4_6) { return 6; } if (opCode == OpCodes.Ldc_I4_7) { return 7; } if (opCode == OpCodes.Ldc_I4_8) { return 8; } if (opCode == OpCodes.Ldc_I4_S) { return (sbyte)instr.Operand; } return (int)instr.Operand; } public static int? GetIntOrNull(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_006b: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_007f: Unknown result type (might be due to invalid IL or missing references) //IL_0080: Unknown result type (might be due to invalid IL or missing references) //IL_0093: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_00a7: Unknown result type (might be due to invalid IL or missing references) //IL_00a8: Unknown result type (might be due to invalid IL or missing references) //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_00bc: Unknown result type (might be due to invalid IL or missing references) //IL_00cf: Unknown result type (might be due to invalid IL or missing references) //IL_00d0: Unknown result type (might be due to invalid IL or missing references) //IL_00ed: Unknown result type (might be due to invalid IL or missing references) //IL_00ee: Unknown result type (might be due to invalid IL or missing references) OpCode opCode = instr.OpCode; if (opCode == OpCodes.Ldc_I4_M1) { return -1; } if (opCode == OpCodes.Ldc_I4_0) { return 0; } if (opCode == OpCodes.Ldc_I4_1) { return 1; } if (opCode == OpCodes.Ldc_I4_2) { return 2; } if (opCode == OpCodes.Ldc_I4_3) { return 3; } if (opCode == OpCodes.Ldc_I4_4) { return 4; } if (opCode == OpCodes.Ldc_I4_5) { return 5; } if (opCode == OpCodes.Ldc_I4_6) { return 6; } if (opCode == OpCodes.Ldc_I4_7) { return 7; } if (opCode == OpCodes.Ldc_I4_8) { return 8; } if (opCode == OpCodes.Ldc_I4_S) { return (sbyte)instr.Operand; } if (opCode == OpCodes.Ldc_I4) { return (int)instr.Operand; } return null; } public static bool IsBaseMethodCall(this MethodBody body, MethodReference called) { //IL_0016: Unknown result type (might be due to invalid IL or missing references) MethodDefinition method = body.Method; if (called == null) { return false; } TypeReference val = ((MemberReference)called).DeclaringType; while (val is TypeSpecification) { val = ((TypeSpecification)val).ElementType; } string patchFullName = ((MemberReference)(object)val).GetPatchFullName(); bool flag = false; try { TypeDefinition val2 = method.DeclaringType; while ((val2 = val2.BaseType?.SafeResolve()) != null) { if (((MemberReference)(object)val2).GetPatchFullName() == patchFullName) { flag = true; break; } } } catch { flag = ((MemberReference)(object)method.DeclaringType).GetPatchFullName() == patchFullName; } if (!flag) { return false; } return true; } public static bool IsCallvirt(this MethodReference method) { if (!method.HasThis) { return false; } if (((MemberReference)method).DeclaringType.IsValueType) { return false; } return true; } public static bool IsStruct(this TypeReference type) { if (!type.IsValueType) { return false; } if (type.IsPrimitive) { return false; } return true; } public static OpCode ToLongOp(this OpCode op) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_004a: Expected I4, but got Unknown //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_004c: Unknown result type (might be due to invalid IL or missing references) //IL_004d: Unknown result type (might be due to invalid IL or missing references) //IL_00b5: Unknown result type (might be due to invalid IL or missing references) //IL_0098: Unknown result type (might be due to invalid IL or missing references) //IL_0093: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_009e: Unknown result type (might be due to invalid IL or missing references) //IL_00a5: Expected I4, but got Unknown //IL_00a5: Unknown result type (might be due to invalid IL or missing references) //IL_00a7: Unknown result type (might be due to invalid IL or missing references) string name = Enum.GetName(t_Code, ((OpCode)(ref op)).Code); if (!name.EndsWith("_S", StringComparison.Ordinal)) { return op; } lock (_ToLongOp) { if (_ToLongOp.TryGetValue((int)((OpCode)(ref op)).Code, out var value)) { return value; } return _ToLongOp[(int)((OpCode)(ref op)).Code] = (OpCode)(((??)(OpCode?)t_OpCodes.GetField(name.Substring(0, name.Length - 2))?.GetValue(null)) ?? op); } } public static OpCode ToShortOp(this OpCode op) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_004a: Expected I4, but got Unknown //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_004c: Unknown result type (might be due to invalid IL or missing references) //IL_004d: Unknown result type (might be due to invalid IL or missing references) //IL_00b1: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_0099: Unknown result type (might be due to invalid IL or missing references) //IL_009a: Unknown result type (might be due to invalid IL or missing references) //IL_00a1: Expected I4, but got Unknown //IL_00a1: Unknown result type (might be due to invalid IL or missing references) //IL_00a3: Unknown result type (might be due to invalid IL or missing references) string name = Enum.GetName(t_Code, ((OpCode)(ref op)).Code); if (name.EndsWith("_S", StringComparison.Ordinal)) { return op; } lock (_ToShortOp) { if (_ToShortOp.TryGetValue((int)((OpCode)(ref op)).Code, out var value)) { return value; } return _ToShortOp[(int)((OpCode)(ref op)).Code] = (OpCode)(((??)(OpCode?)t_OpCodes.GetField(name + "_S")?.GetValue(null)) ?? op); } } public static void RecalculateILOffsets(this MethodDefinition method) { if (method.HasBody) { int num = 0; for (int i = 0; i < method.Body.Instructions.Count; i++) { Instruction val = method.Body.Instructions[i]; val.Offset = num; num += val.GetSize(); } } } public static void FixShortLongOps(this MethodDefinition method) { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_00a2: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: Unknown result type (might be due to invalid IL or missing references) //IL_00b0: Unknown result type (might be due to invalid IL or missing references) //IL_00bc: Unknown result type (might be due to invalid IL or missing references) if (!method.HasBody) { return; } for (int i = 0; i < method.Body.Instructions.Count; i++) { Instruction val = method.Body.Instructions[i]; if (val.Operand is Instruction) { val.OpCode = val.OpCode.ToLongOp(); } } method.RecalculateILOffsets(); bool flag; do { flag = false; for (int j = 0; j < method.Body.Instructions.Count; j++) { Instruction val2 = method.Body.Instructions[j]; object operand = val2.Operand; Instruction val3 = (Instruction)((operand is Instruction) ? operand : null); if (val3 != null) { int num = val3.Offset - (val2.Offset + val2.GetSize()); if (num == (sbyte)num) { OpCode opCode = val2.OpCode; val2.OpCode = val2.OpCode.ToShortOp(); flag = opCode != val2.OpCode; } } } } while (flag); } public static bool Is(this MemberInfo minfo, MemberReference mref) { return mref.Is(minfo); } public static bool Is(this MemberReference mref, MemberInfo minfo) { if (mref == null) { return false; } TypeReference val = mref.DeclaringType; if (((val != null) ? ((MemberReference)val).FullName : null) == "") { val = null; } GenericParameter val2 = (GenericParameter)(object)((mref is GenericParameter) ? mref : null); if (val2 != null) { if (!(minfo is Type type)) { return false; } if (!type.IsGenericParameter) { IGenericParameterProvider owner = val2.Owner; IGenericInstance val3 = (IGenericInstance)(object)((owner is IGenericInstance) ? owner : null); if (val3 != null) { return ((MemberReference)(object)val3.GenericArguments[val2.Position]).Is(type); } return false; } return val2.Position == type.GenericParameterPosition; } if (minfo.DeclaringType != null) { if (val == null) { return false; } Type type2 = minfo.DeclaringType; if (minfo is Type && type2.IsGenericType && !type2.IsGenericTypeDefinition) { type2 = type2.GetGenericTypeDefinition(); } if (!((MemberReference)(object)val).Is(type2)) { return false; } } else if (val != null) { return false; } if (!(mref is TypeSpecification) && mref.Name != minfo.Name) { return false; } TypeReference val4 = (TypeReference)(object)((mref is TypeReference) ? mref : null); if (val4 != null) { if (!(minfo is Type type3)) { return false; } if (type3.IsGenericParameter) { return false; } GenericInstanceType val5 = (GenericInstanceType)(object)((mref is GenericInstanceType) ? mref : null); if (val5 != null) { if (!type3.IsGenericType) { return false; } Collection genericArguments = val5.GenericArguments; Type[] genericArguments2 = type3.GetGenericArguments(); if (genericArguments.Count != genericArguments2.Length) { return false; } for (int i = 0; i < genericArguments.Count; i++) { if (!((MemberReference)(object)genericArguments[i]).Is(genericArguments2[i])) { return false; } } return ((MemberReference)(object)((TypeSpecification)val5).ElementType).Is(type3.GetGenericTypeDefinition()); } if (val4.HasGenericParameters) { if (!type3.IsGenericType) { return false; } Collection genericParameters = val4.GenericParameters; Type[] genericArguments3 = type3.GetGenericArguments(); if (genericParameters.Count != genericArguments3.Length) { return false; } for (int j = 0; j < genericParameters.Count; j++) { if (!((MemberReference)(object)genericParameters[j]).Is(genericArguments3[j])) { return false; } } } else if (type3.IsGenericType) { return false; } ArrayType val6 = (ArrayType)(object)((mref is ArrayType) ? mref : null); if (val6 != null) { if (!type3.IsArray) { return false; } if (val6.Dimensions.Count == type3.GetArrayRank()) { return ((MemberReference)(object)((TypeSpecification)val6).ElementType).Is(type3.GetElementType()); } return false; } ByReferenceType val7 = (ByReferenceType)(object)((mref is ByReferenceType) ? mref : null); if (val7 != null) { if (!type3.IsByRef) { return false; } return ((MemberReference)(object)((TypeSpecification)val7).ElementType).Is(type3.GetElementType()); } PointerType val8 = (PointerType)(object)((mref is PointerType) ? mref : null); if (val8 != null) { if (!type3.IsPointer) { return false; } return ((MemberReference)(object)((TypeSpecification)val8).ElementType).Is(type3.GetElementType()); } TypeSpecification val9 = (TypeSpecification)(object)((mref is TypeSpecification) ? mref : null); if (val9 != null) { return ((MemberReference)(object)val9.ElementType).Is(type3.HasElementType ? type3.GetElementType() : type3); } if (val != null) { return mref.Name == type3.Name; } return mref.FullName == type3.FullName.Replace("+", "/", StringComparison.Ordinal); } if (minfo is Type) { return false; } MethodReference methodRef = (MethodReference)(object)((mref is MethodReference) ? mref : null); if (methodRef != null) { if (!(minfo is MethodBase methodBase)) { return false; } Collection parameters = methodRef.Parameters; ParameterInfo[] parameters2 = methodBase.GetParameters(); if (parameters.Count != parameters2.Length) { return false; } GenericInstanceMethod val10 = (GenericInstanceMethod)(object)((mref is GenericInstanceMethod) ? mref : null); if (val10 != null) { if (!methodBase.IsGenericMethod) { return false; } Collection genericArguments4 = val10.GenericArguments; Type[] genericArguments5 = methodBase.GetGenericArguments(); if (genericArguments4.Count != genericArguments5.Length) { return false; } for (int k = 0; k < genericArguments4.Count; k++) { if (!((MemberReference)(object)genericArguments4[k]).Is(genericArguments5[k])) { return false; } } return ((MemberReference)(object)((MethodSpecification)val10).ElementMethod).Is((methodBase as MethodInfo)?.GetGenericMethodDefinition() ?? methodBase); } if (methodRef.HasGenericParameters) { if (!methodBase.IsGenericMethod) { return false; } Collection genericParameters2 = methodRef.GenericParameters; Type[] genericArguments6 = methodBase.GetGenericArguments(); if (genericParameters2.Count != genericArguments6.Length) { return false; } for (int l = 0; l < genericParameters2.Count; l++) { if (!((MemberReference)(object)genericParameters2[l]).Is(genericArguments6[l])) { return false; } } } else if (methodBase.IsGenericMethod) { return false; } Relinker relinker = null; relinker = delegate(IMetadataTokenProvider paramMemberRef, IGenericParameterProvider ctx) { TypeReference val15 = (TypeReference)(object)((paramMemberRef is TypeReference) ? paramMemberRef : null); return (IMetadataTokenProvider)((val15 == null) ? ((object)paramMemberRef) : ((object)ResolveParameter(val15))); }; if (!((MemberReference)(object)methodRef.ReturnType.Relink(relinker, null)).Is((methodBase as MethodInfo)?.ReturnType ?? typeof(void)) && !((MemberReference)(object)methodRef.ReturnType).Is((methodBase as MethodInfo)?.ReturnType ?? typeof(void))) { return false; } for (int m = 0; m < parameters.Count; m++) { if (!((MemberReference)(object)((ParameterReference)parameters[m]).ParameterType.Relink(relinker, null)).Is(parameters2[m].ParameterType) && !((MemberReference)(object)((ParameterReference)parameters[m]).ParameterType).Is(parameters2[m].ParameterType)) { return false; } } return true; } if (minfo is MethodInfo) { return false; } if (mref is FieldReference != minfo is FieldInfo) { return false; } if (mref is PropertyReference != minfo is PropertyInfo) { return false; } if (mref is EventReference != minfo is EventInfo) { return false; } return true; TypeReference ResolveParameter(TypeReference paramTypeRef) { GenericParameter val11 = (GenericParameter)(object)((paramTypeRef is GenericParameter) ? paramTypeRef : null); if (val11 != null) { if (val11.Owner is MethodReference) { MethodReference obj = methodRef; GenericInstanceMethod val12 = (GenericInstanceMethod)(object)((obj is GenericInstanceMethod) ? obj : null); if (val12 != null) { return val12.GenericArguments[val11.Position]; } } IGenericParameterProvider owner2 = val11.Owner; TypeReference val13 = (TypeReference)(object)((owner2 is TypeReference) ? owner2 : null); if (val13 != null) { TypeReference declaringType = ((MemberReference)methodRef).DeclaringType; GenericInstanceType val14 = (GenericInstanceType)(object)((declaringType is GenericInstanceType) ? declaringType : null); if (val14 != null && ((MemberReference)val13).FullName == ((MemberReference)((TypeSpecification)val14).ElementType).FullName) { return val14.GenericArguments[val11.Position]; } } return paramTypeRef; } if (paramTypeRef == ((MemberReference)methodRef).DeclaringType.GetElementType()) { return ((MemberReference)methodRef).DeclaringType; } return paramTypeRef; } } public static IMetadataTokenProvider ImportReference(this ModuleDefinition mod, IMetadataTokenProvider mtp) { //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_0014: Expected O, but got Unknown //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Expected O, but got Unknown //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Expected O, but got Unknown if (mtp is TypeReference) { return (IMetadataTokenProvider)(object)mod.ImportReference((TypeReference)mtp); } if (mtp is FieldReference) { return (IMetadataTokenProvider)(object)mod.ImportReference((FieldReference)mtp); } if (mtp is MethodReference) { return (IMetadataTokenProvider)(object)mod.ImportReference((MethodReference)mtp); } return mtp; } public static void AddRange(this Collection list, IEnumerable other) { foreach (T item in other) { list.Add(item); } } public static void AddRange(this IDictionary dict, IDictionary other) { foreach (DictionaryEntry item in other) { dict.Add(item.Key, item.Value); } } public static void AddRange(this IDictionary dict, IDictionary other) { foreach (KeyValuePair item in other) { dict.Add(item.Key, item.Value); } } public static void AddRange(this Dictionary dict, Dictionary other) { foreach (KeyValuePair item in other) { dict.Add(item.Key, item.Value); } } public static void InsertRange(this Collection list, int index, IEnumerable other) { foreach (T item in other) { list.Insert(index++, item); } } public static bool IsCompatible(this Type type, Type other) { if (!type._IsCompatible(other)) { return other._IsCompatible(type); } return true; } private static bool _IsCompatible(this Type type, Type other) { if (type == other) { return true; } if (type.IsAssignableFrom(other)) { return true; } if (other.IsEnum && type.IsCompatible(Enum.GetUnderlyingType(other))) { return true; } if ((other.IsPointer || other.IsByRef) && type == typeof(IntPtr)) { return true; } return false; } public static T GetDeclaredMember(this T member) where T : MemberInfo { if (member.DeclaringType == member.ReflectedType) { return member; } int metadataToken = member.MetadataToken; MemberInfo[] members = member.DeclaringType.GetMembers((BindingFlags)(-1)); foreach (MemberInfo memberInfo in members) { if (memberInfo.MetadataToken == metadataToken) { return (T)memberInfo; } } return member; } public unsafe static void SetMonoCorlibInternal(this Assembly asm, bool value) { if (!ReflectionHelper.IsMono) { return; } Type type = asm?.GetType(); if (type == null) { return; } FieldInfo value2; lock (fmap_mono_assembly) { if (!fmap_mono_assembly.TryGetValue(type, out value2)) { value2 = type.GetField("_mono_assembly", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? type.GetField("dynamic_assembly", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); fmap_mono_assembly[type] = value2; } } if (value2 == null) { return; } AssemblyName assemblyName = new AssemblyName(asm.FullName); lock (ReflectionHelper.AssemblyCache) { WeakReference value3 = new WeakReference(asm); ReflectionHelper.AssemblyCache[asm.GetRuntimeHashedFullName()] = value3; ReflectionHelper.AssemblyCache[assemblyName.FullName] = value3; ReflectionHelper.AssemblyCache[assemblyName.Name] = value3; } long num = 0L; object value4 = value2.GetValue(asm); if (!(value4 is IntPtr intPtr)) { if (value4 is UIntPtr uIntPtr) { num = (long)(ulong)uIntPtr; } } else { num = (long)intPtr; } int num2 = IntPtr.Size + IntPtr.Size + IntPtr.Size + IntPtr.Size + IntPtr.Size + IntPtr.Size + 20 + 4 + 4 + 4 + (_MonoAssemblyNameHasArch ? ((!ReflectionHelper.IsCore) ? ((IntPtr.Size == 4) ? 12 : 16) : ((IntPtr.Size == 4) ? 20 : 24)) : (ReflectionHelper.IsCore ? 16 : 8)) + IntPtr.Size + IntPtr.Size + 1 + 1 + 1; byte* ptr = (byte*)(num + num2); *ptr = (byte)(value ? 1 : 0); } public static bool IsDynamicMethod(this MethodBase method) { if (_RTDynamicMethod != null) { if (!(method is DynamicMethod)) { return method.GetType() == _RTDynamicMethod; } return true; } if (method is DynamicMethod) { return true; } if (method.MetadataToken != 0 || !method.IsStatic || !method.IsPublic || (method.Attributes & MethodAttributes.PrivateScope) != 0) { return false; } MethodInfo[] methods = method.DeclaringType.GetMethods(BindingFlags.Static | BindingFlags.Public); foreach (MethodInfo methodInfo in methods) { if (method == methodInfo) { return false; } } return true; } public static object SafeGetTarget(this WeakReference weak) { try { return weak.Target; } catch (InvalidOperationException) { return null; } } public static bool SafeGetIsAlive(this WeakReference weak) { try { return weak.IsAlive; } catch (InvalidOperationException) { return false; } } public static T CreateDelegate(this MethodBase method) where T : Delegate { return (T)method.CreateDelegate(typeof(T), null); } public static T CreateDelegate(this MethodBase method, object target) where T : Delegate { return (T)method.CreateDelegate(typeof(T), target); } public static Delegate CreateDelegate(this MethodBase method, Type delegateType) { return method.CreateDelegate(delegateType, null); } public static Delegate CreateDelegate(this MethodBase method, Type delegateType, object target) { if (!typeof(Delegate).IsAssignableFrom(delegateType)) { throw new ArgumentException("Type argument must be a delegate type!"); } if (method is DynamicMethod dynamicMethod) { return dynamicMethod.CreateDelegate(delegateType, target); } if (method is MethodInfo methodInfo) { return methodInfo.CreateDelegate(delegateType, target); } RuntimeMethodHandle methodHandle = method.MethodHandle; RuntimeHelpers.PrepareMethod(methodHandle); IntPtr functionPointer = methodHandle.GetFunctionPointer(); return (Delegate)Activator.CreateInstance(delegateType, target, functionPointer); } public static MethodDefinition FindMethod(this TypeDefinition type, string id, bool simple = true) { //IL_00ad: Unknown result type (might be due to invalid IL or missing references) //IL_00b2: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_00f5: Unknown result type (might be due to invalid IL or missing references) //IL_00fa: Unknown result type (might be due to invalid IL or missing references) //IL_0065: Unknown result type (might be due to invalid IL or missing references) //IL_006a: Unknown result type (might be due to invalid IL or missing references) Enumerator enumerator; if (simple && !id.Contains(" ", StringComparison.Ordinal)) { enumerator = type.Methods.GetEnumerator(); try { while (enumerator.MoveNext()) { MethodDefinition current = enumerator.Current; if (((MethodReference)(object)current).GetID(null, null, withType: true, simple: true) == id) { return current; } } } finally { ((IDisposable)enumerator).Dispose(); } enumerator = type.Methods.GetEnumerator(); try { while (enumerator.MoveNext()) { MethodDefinition current2 = enumerator.Current; if (((MethodReference)(object)current2).GetID(null, null, withType: false, simple: true) == id) { return current2; } } } finally { ((IDisposable)enumerator).Dispose(); } } enumerator = type.Methods.GetEnumerator(); try { while (enumerator.MoveNext()) { MethodDefinition current3 = enumerator.Current; if (((MethodReference)(object)current3).GetID() == id) { return current3; } } } finally { ((IDisposable)enumerator).Dispose(); } enumerator = type.Methods.GetEnumerator(); try { while (enumerator.MoveNext()) { MethodDefinition current4 = enumerator.Current; if (((MethodReference)(object)current4).GetID(null, null, withType: false) == id) { return current4; } } } finally { ((IDisposable)enumerator).Dispose(); } return null; } public static MethodDefinition FindMethodDeep(this TypeDefinition type, string id, bool simple = true) { MethodDefinition obj = type.FindMethod(id, simple); if (obj == null) { TypeReference baseType = type.BaseType; if (baseType == null) { return null; } TypeDefinition obj2 = baseType.Resolve(); if (obj2 == null) { return null; } obj = obj2.FindMethodDeep(id, simple); } return obj; } public static MethodInfo FindMethod(this Type type, string id, bool simple = true) { MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo[] array; if (simple && !id.Contains(" ", StringComparison.Ordinal)) { array = methods; foreach (MethodInfo methodInfo in array) { if (methodInfo.GetID(null, null, withType: true, proxyMethod: false, simple: true) == id) { return methodInfo; } } array = methods; foreach (MethodInfo methodInfo2 in array) { if (methodInfo2.GetID(null, null, withType: false, proxyMethod: false, simple: true) == id) { return methodInfo2; } } } array = methods; foreach (MethodInfo methodInfo3 in array) { if (methodInfo3.GetID(null, null, withType: true, proxyMethod: false, simple: false) == id) { return methodInfo3; } } array = methods; foreach (MethodInfo methodInfo4 in array) { if (methodInfo4.GetID(null, null, withType: false, proxyMethod: false, simple: false) == id) { return methodInfo4; } } return null; } public static MethodInfo FindMethodDeep(this Type type, string id, bool simple = true) { MethodInfo methodInfo = type.FindMethod(id, simple); if ((object)methodInfo == null) { Type? baseType = type.BaseType; if ((object)baseType == null) { return null; } methodInfo = baseType.FindMethodDeep(id, simple); } return methodInfo; } public static PropertyDefinition FindProperty(this TypeDefinition type, string name) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) Enumerator enumerator = type.Properties.GetEnumerator(); try { while (enumerator.MoveNext()) { PropertyDefinition current = enumerator.Current; if (((MemberReference)current).Name == name) { return current; } } } finally { ((IDisposable)enumerator).Dispose(); } return null; } public static PropertyDefinition FindPropertyDeep(this TypeDefinition type, string name) { PropertyDefinition obj = type.FindProperty(name); if (obj == null) { TypeReference baseType = type.BaseType; if (baseType == null) { return null; } TypeDefinition obj2 = baseType.Resolve(); if (obj2 == null) { return null; } obj = obj2.FindPropertyDeep(name); } return obj; } public static FieldDefinition FindField(this TypeDefinition type, string name) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) Enumerator enumerator = type.Fields.GetEnumerator(); try { while (enumerator.MoveNext()) { FieldDefinition current = enumerator.Current; if (((MemberReference)current).Name == name) { return current; } } } finally { ((IDisposable)enumerator).Dispose(); } return null; } public static FieldDefinition FindFieldDeep(this TypeDefinition type, string name) { FieldDefinition obj = type.FindField(name); if (obj == null) { TypeReference baseType = type.BaseType; if (baseType == null) { return null; } TypeDefinition obj2 = baseType.Resolve(); if (obj2 == null) { return null; } obj = obj2.FindFieldDeep(name); } return obj; } public static EventDefinition FindEvent(this TypeDefinition type, string name) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) Enumerator enumerator = type.Events.GetEnumerator(); try { while (enumerator.MoveNext()) { EventDefinition current = enumerator.Current; if (((MemberReference)current).Name == name) { return current; } } } finally { ((IDisposable)enumerator).Dispose(); } return null; } public static EventDefinition FindEventDeep(this TypeDefinition type, string name) { EventDefinition obj = type.FindEvent(name); if (obj == null) { TypeReference baseType = type.BaseType; if (baseType == null) { return null; } TypeDefinition obj2 = baseType.Resolve(); if (obj2 == null) { return null; } obj = obj2.FindEventDeep(name); } return obj; } public static string GetID(this MethodReference method, string name = null, string type = null, bool withType = true, bool simple = false) { StringBuilder stringBuilder = new StringBuilder(); if (simple) { if (withType && (type != null || ((MemberReference)method).DeclaringType != null)) { stringBuilder.Append(type ?? ((MemberReference)(object)((MemberReference)method).DeclaringType).GetPatchFullName()).Append("::"); } stringBuilder.Append(name ?? ((MemberReference)method).Name); return stringBuilder.ToString(); } stringBuilder.Append(((MemberReference)(object)method.ReturnType).GetPatchFullName()).Append(" "); if (withType && (type != null || ((MemberReference)method).DeclaringType != null)) { stringBuilder.Append(type ?? ((MemberReference)(object)((MemberReference)method).DeclaringType).GetPatchFullName()).Append("::"); } stringBuilder.Append(name ?? ((MemberReference)method).Name); GenericInstanceMethod val = (GenericInstanceMethod)(object)((method is GenericInstanceMethod) ? method : null); if (val != null && val.GenericArguments.Count != 0) { stringBuilder.Append("<"); Collection genericArguments = val.GenericArguments; for (int i = 0; i < genericArguments.Count; i++) { if (i > 0) { stringBuilder.Append(","); } stringBuilder.Append(((MemberReference)(object)genericArguments[i]).GetPatchFullName()); } stringBuilder.Append(">"); } else if (method.GenericParameters.Count != 0) { stringBuilder.Append("<"); Collection genericParameters = method.GenericParameters; for (int j = 0; j < genericParameters.Count; j++) { if (j > 0) { stringBuilder.Append(","); } stringBuilder.Append(((MemberReference)genericParameters[j]).Name); } stringBuilder.Append(">"); } stringBuilder.Append("("); if (method.HasParameters) { Collection parameters = method.Parameters; for (int k = 0; k < parameters.Count; k++) { ParameterDefinition val2 = parameters[k]; if (k > 0) { stringBuilder.Append(","); } if (((ParameterReference)val2).ParameterType.IsSentinel) { stringBuilder.Append("...,"); } stringBuilder.Append(((MemberReference)(object)((ParameterReference)val2).ParameterType).GetPatchFullName()); } } stringBuilder.Append(")"); return stringBuilder.ToString(); } public static string GetID(this CallSite method) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(((MemberReference)(object)method.ReturnType).GetPatchFullName()).Append(" "); stringBuilder.Append("("); if (method.HasParameters) { Collection parameters = method.Parameters; for (int i = 0; i < parameters.Count; i++) { ParameterDefinition val = parameters[i]; if (i > 0) { stringBuilder.Append(","); } if (((ParameterReference)val).ParameterType.IsSentinel) { stringBuilder.Append("...,"); } stringBuilder.Append(((MemberReference)(object)((ParameterReference)val).ParameterType).GetPatchFullName()); } } stringBuilder.Append(")"); return stringBuilder.ToString(); } public static string GetID(this MethodBase method, string name = null, string type = null, bool withType = true, bool proxyMethod = false, bool simple = false) { while (method is MethodInfo && method.IsGenericMethod && !method.IsGenericMethodDefinition) { method = ((MethodInfo)method).GetGenericMethodDefinition(); } StringBuilder stringBuilder = new StringBuilder(); if (simple) { if (withType && (type != null || method.DeclaringType != null)) { stringBuilder.Append(type ?? method.DeclaringType.FullName).Append("::"); } stringBuilder.Append(name ?? method.Name); return stringBuilder.ToString(); } stringBuilder.Append((method as MethodInfo)?.ReturnType?.FullName ?? "System.Void").Append(" "); if (withType && (type != null || method.DeclaringType != null)) { stringBuilder.Append(type ?? method.DeclaringType.FullName.Replace("+", "/", StringComparison.Ordinal)).Append("::"); } stringBuilder.Append(name ?? method.Name); if (method.ContainsGenericParameters) { stringBuilder.Append("<"); Type[] genericArguments = method.GetGenericArguments(); for (int i = 0; i < genericArguments.Length; i++) { if (i > 0) { stringBuilder.Append(","); } stringBuilder.Append(genericArguments[i].Name); } stringBuilder.Append(">"); } stringBuilder.Append("("); ParameterInfo[] parameters = method.GetParameters(); for (int j = (proxyMethod ? 1 : 0); j < parameters.Length; j++) { ParameterInfo parameterInfo = parameters[j]; if (j > (proxyMethod ? 1 : 0)) { stringBuilder.Append(","); } bool flag; try { flag = CustomAttributeExtensions.IsDefined(parameterInfo, t_ParamArrayAttribute, inherit: false); } catch (NotSupportedException) { flag = false; } if (flag) { stringBuilder.Append("...,"); } stringBuilder.Append(parameterInfo.ParameterType.FullName); } stringBuilder.Append(")"); return stringBuilder.ToString(); } public static string GetPatchName(this MemberReference mr) { MemberReference obj = ((mr is ICustomAttributeProvider) ? mr : null); return ((obj != null) ? ((ICustomAttributeProvider)(object)obj).GetPatchName() : null) ?? mr.Name; } public static string GetPatchFullName(this MemberReference mr) { MemberReference obj = ((mr is ICustomAttributeProvider) ? mr : null); return ((obj != null) ? ((ICustomAttributeProvider)(object)obj).GetPatchFullName(mr) : null) ?? mr.FullName; } private static string GetPatchName(this ICustomAttributeProvider cap) { //IL_004e: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Unknown result type (might be due to invalid IL or missing references) CustomAttribute customAttribute = cap.GetCustomAttribute("MonoMod.MonoModPatch"); string text; if (customAttribute != null) { CustomAttributeArgument val = customAttribute.ConstructorArguments[0]; text = (string)((CustomAttributeArgument)(ref val)).Value; int num = text.LastIndexOf('.'); if (num != -1 && num != text.Length - 1) { text = text.Substring(num + 1); } return text; } text = ((MemberReference)cap).Name; if (!text.StartsWith("patch_", StringComparison.Ordinal)) { return text; } return text.Substring(6); } private static string GetPatchFullName(this ICustomAttributeProvider cap, MemberReference mr) { //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_00e5: Unknown result type (might be due to invalid IL or missing references) //IL_00ec: Expected O, but got Unknown //IL_019d: Unknown result type (might be due to invalid IL or missing references) //IL_01a4: Expected O, but got Unknown //IL_0247: Unknown result type (might be due to invalid IL or missing references) //IL_027d: Unknown result type (might be due to invalid IL or missing references) //IL_02a7: Unknown result type (might be due to invalid IL or missing references) //IL_02ae: Expected O, but got Unknown //IL_0323: Unknown result type (might be due to invalid IL or missing references) //IL_032a: Expected O, but got Unknown //IL_01ee: Unknown result type (might be due to invalid IL or missing references) //IL_01f3: Unknown result type (might be due to invalid IL or missing references) TypeReference val = (TypeReference)(object)((cap is TypeReference) ? cap : null); if (val != null) { CustomAttribute customAttribute = cap.GetCustomAttribute("MonoMod.MonoModPatch"); string text; if (customAttribute != null) { CustomAttributeArgument val2 = customAttribute.ConstructorArguments[0]; text = (string)((CustomAttributeArgument)(ref val2)).Value; } else { text = ((MemberReference)cap).Name; text = (text.StartsWith("patch_", StringComparison.Ordinal) ? text.Substring(6) : text); } if (text.StartsWith("global::", StringComparison.Ordinal)) { text = text.Substring(8); } else if (!text.Contains(".", StringComparison.Ordinal) && !text.Contains("/", StringComparison.Ordinal)) { if (!string.IsNullOrEmpty(val.Namespace)) { text = val.Namespace + "." + text; } else if (val.IsNested) { text = ((MemberReference)(object)((MemberReference)val).DeclaringType).GetPatchFullName() + "/" + text; } } if (mr is TypeSpecification) { List list = new List(); TypeSpecification val3 = (TypeSpecification)mr; TypeReference elementType; do { list.Add(val3); elementType = val3.ElementType; } while ((val3 = (TypeSpecification)(object)((elementType is TypeSpecification) ? elementType : null)) != null); StringBuilder stringBuilder = new StringBuilder(text.Length + list.Count * 4); stringBuilder.Append(text); for (int num = list.Count - 1; num > -1; num--) { val3 = list[num]; if (((TypeReference)val3).IsByReference) { stringBuilder.Append("&"); } else if (((TypeReference)val3).IsPointer) { stringBuilder.Append("*"); } else if (!((TypeReference)val3).IsPinned && !((TypeReference)val3).IsSentinel) { if (((TypeReference)val3).IsArray) { ArrayType val4 = (ArrayType)val3; if (val4.IsVector) { stringBuilder.Append("[]"); } else { stringBuilder.Append("["); for (int i = 0; i < val4.Dimensions.Count; i++) { if (i > 0) { stringBuilder.Append(","); } ArrayDimension val5 = val4.Dimensions[i]; stringBuilder.Append(((object)(ArrayDimension)(ref val5)).ToString()); } stringBuilder.Append("]"); } } else if (((TypeReference)val3).IsRequiredModifier) { stringBuilder.Append("modreq(").Append(((RequiredModifierType)val3).ModifierType).Append(")"); } else if (((TypeReference)val3).IsOptionalModifier) { stringBuilder.Append("modopt(").Append(((OptionalModifierType)val3).ModifierType).Append(")"); } else if (((TypeReference)val3).IsGenericInstance) { GenericInstanceType val6 = (GenericInstanceType)val3; stringBuilder.Append("<"); for (int j = 0; j < val6.GenericArguments.Count; j++) { if (j > 0) { stringBuilder.Append(","); } stringBuilder.Append(((MemberReference)(object)val6.GenericArguments[j]).GetPatchFullName()); } stringBuilder.Append(">"); } else { if (!((TypeReference)val3).IsFunctionPointer) { throw new NotSupportedException($"MonoMod can't handle TypeSpecification: {((MemberReference)val).FullName} ({((object)val).GetType()})"); } FunctionPointerType val7 = (FunctionPointerType)val3; stringBuilder.Append(" ").Append(((MemberReference)(object)val7.ReturnType).GetPatchFullName()).Append(" *("); if (val7.HasParameters) { for (int k = 0; k < val7.Parameters.Count; k++) { ParameterDefinition val8 = val7.Parameters[k]; if (k > 0) { stringBuilder.Append(","); } if (((ParameterReference)val8).ParameterType.IsSentinel) { stringBuilder.Append("...,"); } stringBuilder.Append(((MemberReference)((ParameterReference)val8).ParameterType).FullName); } } stringBuilder.Append(")"); } } } text = stringBuilder.ToString(); } return text; } FieldReference val9 = (FieldReference)(object)((cap is FieldReference) ? cap : null); if (val9 != null) { return ((MemberReference)(object)val9.FieldType).GetPatchFullName() + " " + ((MemberReference)(object)((MemberReference)val9).DeclaringType).GetPatchFullName() + "::" + cap.GetPatchName(); } if (cap is MethodReference) { throw new InvalidOperationException("GetPatchFullName not supported on MethodReferences - use GetID instead"); } throw new InvalidOperationException($"GetPatchFullName not supported on type {((object)cap).GetType()}"); } public static MethodDefinition Clone(this MethodDefinition o, MethodDefinition c = null) { //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Expected O, but got Unknown //IL_0078: Unknown result type (might be due to invalid IL or missing references) //IL_0084: Unknown result type (might be due to invalid IL or missing references) //IL_00b8: Unknown result type (might be due to invalid IL or missing references) //IL_00bd: Unknown result type (might be due to invalid IL or missing references) //IL_00f8: Unknown result type (might be due to invalid IL or missing references) //IL_00fd: Unknown result type (might be due to invalid IL or missing references) //IL_0138: Unknown result type (might be due to invalid IL or missing references) //IL_013d: Unknown result type (might be due to invalid IL or missing references) //IL_017b: Unknown result type (might be due to invalid IL or missing references) //IL_0180: Unknown result type (might be due to invalid IL or missing references) //IL_01c9: Unknown result type (might be due to invalid IL or missing references) //IL_01ce: Unknown result type (might be due to invalid IL or missing references) if (o == null) { return null; } if (c == null) { c = new MethodDefinition(((MemberReference)o).Name, o.Attributes, ((MethodReference)o).ReturnType); } ((MemberReference)c).Name = ((MemberReference)o).Name; c.Attributes = o.Attributes; ((MethodReference)c).ReturnType = ((MethodReference)o).ReturnType; c.DeclaringType = o.DeclaringType; ((MemberReference)c).MetadataToken = ((MemberReference)c).MetadataToken; c.Body = o.Body?.Clone(c); c.Attributes = o.Attributes; c.ImplAttributes = o.ImplAttributes; c.PInvokeInfo = o.PInvokeInfo; c.IsPreserveSig = o.IsPreserveSig; c.IsPInvokeImpl = o.IsPInvokeImpl; Enumerator enumerator = ((MethodReference)o).GenericParameters.GetEnumerator(); try { while (enumerator.MoveNext()) { GenericParameter current = enumerator.Current; ((MethodReference)c).GenericParameters.Add(current.Clone()); } } finally { ((IDisposable)enumerator).Dispose(); } Enumerator enumerator2 = ((MethodReference)o).Parameters.GetEnumerator(); try { while (enumerator2.MoveNext()) { ParameterDefinition current2 = enumerator2.Current; ((MethodReference)c).Parameters.Add(current2.Clone()); } } finally { ((IDisposable)enumerator2).Dispose(); } Enumerator enumerator3 = o.CustomAttributes.GetEnumerator(); try { while (enumerator3.MoveNext()) { CustomAttribute current3 = enumerator3.Current; c.CustomAttributes.Add(current3.Clone()); } } finally { ((IDisposable)enumerator3).Dispose(); } Enumerator enumerator4 = o.Overrides.GetEnumerator(); try { while (enumerator4.MoveNext()) { MethodReference current4 = enumerator4.Current; c.Overrides.Add(current4); } } finally { ((IDisposable)enumerator4).Dispose(); } if (c.Body != null) { Enumerator enumerator5 = c.Body.Instructions.GetEnumerator(); try { while (enumerator5.MoveNext()) { Instruction current5 = enumerator5.Current; object operand = current5.Operand; GenericParameter val = (GenericParameter)((operand is GenericParameter) ? operand : null); int num; if (val != null && (num = ((MethodReference)o).GenericParameters.IndexOf(val)) != -1) { current5.Operand = ((MethodReference)c).GenericParameters[num]; continue; } object operand2 = current5.Operand; ParameterDefinition val2 = (ParameterDefinition)((operand2 is ParameterDefinition) ? operand2 : null); if (val2 != null && (num = ((MethodReference)o).Parameters.IndexOf(val2)) != -1) { current5.Operand = ((MethodReference)c).Parameters[num]; } } } finally { ((IDisposable)enumerator5).Dispose(); } } return c; } public static MethodBody Clone(this MethodBody bo, MethodDefinition m) { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Expected O, but got Unknown //IL_005b: Unknown result type (might be due to invalid IL or missing references) //IL_00af: Unknown result type (might be due to invalid IL or missing references) //IL_00b4: Unknown result type (might be due to invalid IL or missing references) if (bo == null) { return null; } MethodBody bc = new MethodBody(m); bc.MaxStackSize = bo.MaxStackSize; bc.InitLocals = bo.InitLocals; bc.LocalVarToken = bo.LocalVarToken; bc.Instructions.AddRange(((IEnumerable)bo.Instructions).Select(delegate(Instruction o) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) Instruction obj = Instruction.Create(OpCodes.Nop); obj.OpCode = o.OpCode; obj.Operand = o.Operand; obj.Offset = o.Offset; return obj; })); Enumerator enumerator = bc.Instructions.GetEnumerator(); try { while (enumerator.MoveNext()) { Instruction current = enumerator.Current; object operand = current.Operand; Instruction val = (Instruction)((operand is Instruction) ? operand : null); if (val != null) { current.Operand = bc.Instructions[bo.Instructions.IndexOf(val)]; } else if (current.Operand is Instruction[] source) { current.Operand = source.Select((Instruction i) => bc.Instructions[bo.Instructions.IndexOf(i)]).ToArray(); } } } finally { ((IDisposable)enumerator).Dispose(); } bc.ExceptionHandlers.AddRange(((IEnumerable)bo.ExceptionHandlers).Select((Func)((ExceptionHandler o) => new ExceptionHandler(o.HandlerType) { TryStart = ((o.TryStart == null) ? null : bc.Instructions[bo.Instructions.IndexOf(o.TryStart)]), TryEnd = ((o.TryEnd == null) ? null : bc.Instructions[bo.Instructions.IndexOf(o.TryEnd)]), FilterStart = ((o.FilterStart == null) ? null : bc.Instructions[bo.Instructions.IndexOf(o.FilterStart)]), HandlerStart = ((o.HandlerStart == null) ? null : bc.Instructions[bo.Instructions.IndexOf(o.HandlerStart)]), HandlerEnd = ((o.HandlerEnd == null) ? null : bc.Instructions[bo.Instructions.IndexOf(o.HandlerEnd)]), CatchType = o.CatchType }))); bc.Variables.AddRange(((IEnumerable)bo.Variables).Select((Func)((VariableDefinition o) => new VariableDefinition(((VariableReference)o).VariableType)))); m.CustomDebugInformations.AddRange((IEnumerable)bo.Method.CustomDebugInformations); m.DebugInformation.SequencePoints.AddRange(((IEnumerable)bo.Method.DebugInformation.SequencePoints).Select((Func)((SequencePoint o) => new SequencePoint(((IEnumerable)bc.Instructions).FirstOrDefault((Func)((Instruction i) => i.Offset == o.Offset)), o.Document) { StartLine = o.StartLine, StartColumn = o.StartColumn, EndLine = o.EndLine, EndColumn = o.EndColumn }))); return bc; } public static GenericParameter Update(this GenericParameter param, int position, GenericParameterType type) { //IL_0017: Unknown result type (might be due to invalid IL or missing references) f_GenericParameter_position.SetValue(param, position); f_GenericParameter_type.SetValue(param, type); return param; } public static GenericParameter ResolveGenericParameter(this IGenericParameterProvider provider, GenericParameter orig) { //IL_002d: Unknown result type (might be due to invalid IL or missing references) //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Expected O, but got Unknown if (provider is GenericParameter && ((MemberReference)(GenericParameter)provider).Name == ((MemberReference)orig).Name) { return (GenericParameter)provider; } Enumerator enumerator = provider.GenericParameters.GetEnumerator(); try { while (enumerator.MoveNext()) { GenericParameter current = enumerator.Current; if (((MemberReference)current).Name == ((MemberReference)orig).Name) { return current; } } } finally { ((IDisposable)enumerator).Dispose(); } int position = orig.Position; if (provider is MethodReference && orig.DeclaringMethod != null) { if (position < provider.GenericParameters.Count) { return provider.GenericParameters[position]; } return orig.Clone().Update(position, (GenericParameterType)1); } if (provider is TypeReference && ((MemberReference)orig).DeclaringType != null) { if (position < provider.GenericParameters.Count) { return provider.GenericParameters[position]; } return orig.Clone().Update(position, (GenericParameterType)0); } IGenericParameterProvider obj = ((provider is TypeSpecification) ? provider : null); object obj2 = ((obj != null) ? ((IGenericParameterProvider)(object)((TypeSpecification)obj).ElementType).ResolveGenericParameter(orig) : null); if (obj2 == null) { IGenericParameterProvider obj3 = ((provider is MemberReference) ? provider : null); if (obj3 == null) { return null; } TypeReference declaringType = ((MemberReference)obj3).DeclaringType; if (declaringType == null) { return null; } obj2 = ((IGenericParameterProvider)(object)declaringType).ResolveGenericParameter(orig); } return (GenericParameter)obj2; } public static IMetadataTokenProvider Relink(this IMetadataTokenProvider mtp, Relinker relinker, IGenericParameterProvider context) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_0015: Expected O, but got Unknown //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Expected O, but got Unknown //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Expected O, but got Unknown //IL_004b: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Expected O, but got Unknown //IL_0061: Unknown result type (might be due to invalid IL or missing references) //IL_006d: Expected O, but got Unknown //IL_0077: Unknown result type (might be due to invalid IL or missing references) //IL_0083: Expected O, but got Unknown if (mtp is TypeReference) { return (IMetadataTokenProvider)(object)Extensions.Relink((TypeReference)mtp, relinker, context); } if (mtp is GenericParameterConstraint) { return (IMetadataTokenProvider)(object)Extensions.Relink((GenericParameterConstraint)mtp, relinker, context); } if (mtp is MethodReference) { return Extensions.Relink((MethodReference)mtp, relinker, context); } if (mtp is FieldReference) { return Extensions.Relink((FieldReference)mtp, relinker, context); } if (mtp is ParameterDefinition) { return (IMetadataTokenProvider)(object)Extensions.Relink((ParameterDefinition)mtp, relinker, context); } if (mtp is CallSite) { return (IMetadataTokenProvider)(object)Extensions.Relink((CallSite)mtp, relinker, context); } throw new InvalidOperationException($"MonoMod can't handle metadata token providers of the type {((object)mtp).GetType()}"); } public static TypeReference Relink(this TypeReference type, Relinker relinker, IGenericParameterProvider context) { //IL_027d: Unknown result type (might be due to invalid IL or missing references) //IL_0283: Expected O, but got Unknown //IL_0029: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Expected O, but got Unknown //IL_01ec: Unknown result type (might be due to invalid IL or missing references) //IL_01f6: Expected O, but got Unknown //IL_0038: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Expected O, but got Unknown //IL_0047: Unknown result type (might be due to invalid IL or missing references) //IL_004d: Expected O, but got Unknown //IL_0056: Unknown result type (might be due to invalid IL or missing references) //IL_005c: Expected O, but got Unknown //IL_0066: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Expected O, but got Unknown //IL_00af: Unknown result type (might be due to invalid IL or missing references) //IL_00c1: Unknown result type (might be due to invalid IL or missing references) //IL_00c7: Expected O, but got Unknown //IL_00d0: Unknown result type (might be due to invalid IL or missing references) //IL_00e2: Unknown result type (might be due to invalid IL or missing references) //IL_00e8: Expected O, but got Unknown //IL_0082: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Unknown result type (might be due to invalid IL or missing references) //IL_00f1: Unknown result type (might be due to invalid IL or missing references) //IL_00f8: Expected O, but got Unknown //IL_00f9: Unknown result type (might be due to invalid IL or missing references) //IL_0103: Unknown result type (might be due to invalid IL or missing references) //IL_0108: Unknown result type (might be due to invalid IL or missing references) //IL_0156: Unknown result type (might be due to invalid IL or missing references) //IL_015d: Expected O, but got Unknown if (type == null) { return null; } TypeSpecification val = (TypeSpecification)(object)((type is TypeSpecification) ? type : null); if (val != null) { TypeReference val2 = val.ElementType.Relink(relinker, context); if (type.IsSentinel) { return (TypeReference)new SentinelType(val2); } if (type.IsByReference) { return (TypeReference)new ByReferenceType(val2); } if (type.IsPointer) { return (TypeReference)new PointerType(val2); } if (type.IsPinned) { return (TypeReference)new PinnedType(val2); } if (type.IsArray) { ArrayType val3 = new ArrayType(val2, ((ArrayType)type).Rank); for (int i = 0; i < val3.Rank; i++) { val3.Dimensions[i] = ((ArrayType)type).Dimensions[i]; } return (TypeReference)(object)val3; } if (type.IsRequiredModifier) { return (TypeReference)new RequiredModifierType(((RequiredModifierType)type).ModifierType.Relink(relinker, context), val2); } if (type.IsOptionalModifier) { return (TypeReference)new OptionalModifierType(((OptionalModifierType)type).ModifierType.Relink(relinker, context), val2); } if (type.IsGenericInstance) { GenericInstanceType val4 = new GenericInstanceType(val2); Enumerator enumerator = ((GenericInstanceType)type).GenericArguments.GetEnumerator(); try { while (enumerator.MoveNext()) { TypeReference current = enumerator.Current; val4.GenericArguments.Add(current?.Relink(relinker, context)); } return (TypeReference)(object)val4; } finally { ((IDisposable)enumerator).Dispose(); } } if (type.IsFunctionPointer) { FunctionPointerType val5 = (FunctionPointerType)type; val5.ReturnType = val5.ReturnType.Relink(relinker, context); for (int j = 0; j < val5.Parameters.Count; j++) { ((ParameterReference)val5.Parameters[j]).ParameterType = ((ParameterReference)val5.Parameters[j]).ParameterType.Relink(relinker, context); } return (TypeReference)(object)val5; } throw new NotSupportedException($"MonoMod can't handle TypeSpecification: {((MemberReference)type).FullName} ({((object)type).GetType()})"); } if (!type.IsGenericParameter || context == null) { return (TypeReference)relinker((IMetadataTokenProvider)(object)type, context); } GenericParameter val6 = context.ResolveGenericParameter((GenericParameter)type); if (val6 == null) { throw new RelinkTargetNotFoundException(string.Format("{0} {1} (context: {2})", "MonoMod relinker failed finding", ((MemberReference)type).FullName, context), (IMetadataTokenProvider)(object)type, (IMetadataTokenProvider)(object)context); } for (int k = 0; k < val6.Constraints.Count; k++) { if (!val6.Constraints[k].GetConstraintType().IsGenericInstance) { val6.Constraints[k] = val6.Constraints[k].Relink(relinker, context); } } return (TypeReference)(object)val6; } public static GenericParameterConstraint Relink(this GenericParameterConstraint constraint, Relinker relinker, IGenericParameterProvider context) { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Expected O, but got Unknown //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) if (constraint == null) { return null; } GenericParameterConstraint val = new GenericParameterConstraint(constraint.ConstraintType.Relink(relinker, context)); Enumerator enumerator = constraint.CustomAttributes.GetEnumerator(); try { while (enumerator.MoveNext()) { CustomAttribute current = enumerator.Current; val.CustomAttributes.Add(current.Relink(relinker, context)); } return val; } finally { ((IDisposable)enumerator).Dispose(); } } public static IMetadataTokenProvider Relink(this MethodReference method, Relinker relinker, IGenericParameterProvider context) { //IL_008e: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Expected O, but got Unknown //IL_0096: Unknown result type (might be due to invalid IL or missing references) //IL_00be: Unknown result type (might be due to invalid IL or missing references) //IL_00c3: Unknown result type (might be due to invalid IL or missing references) //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Expected O, but got Unknown //IL_0020: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Expected O, but got Unknown //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_006f: Unknown result type (might be due to invalid IL or missing references) //IL_0075: Expected O, but got Unknown //IL_011d: Unknown result type (might be due to invalid IL or missing references) //IL_0122: Unknown result type (might be due to invalid IL or missing references) //IL_0172: Unknown result type (might be due to invalid IL or missing references) //IL_0178: Expected O, but got Unknown if (method.IsGenericInstance) { GenericInstanceMethod val = (GenericInstanceMethod)method; GenericInstanceMethod val2 = new GenericInstanceMethod((MethodReference)((MethodSpecification)val).ElementMethod.Relink(relinker, context)); Enumerator enumerator = val.GenericArguments.GetEnumerator(); try { while (enumerator.MoveNext()) { TypeReference current = enumerator.Current; val2.GenericArguments.Add(current.Relink(relinker, context)); } } finally { ((IDisposable)enumerator).Dispose(); } return (IMetadataTokenProvider)(MethodReference)relinker((IMetadataTokenProvider)(object)val2, context); } MethodReference val3 = new MethodReference(((MemberReference)method).Name, method.ReturnType, ((MemberReference)method).DeclaringType.Relink(relinker, context)); val3.CallingConvention = method.CallingConvention; val3.ExplicitThis = method.ExplicitThis; val3.HasThis = method.HasThis; Enumerator enumerator2 = method.GenericParameters.GetEnumerator(); try { while (enumerator2.MoveNext()) { GenericParameter current2 = enumerator2.Current; val3.GenericParameters.Add(current2.Relink(relinker, context)); } } finally { ((IDisposable)enumerator2).Dispose(); } val3.ReturnType = val3.ReturnType?.Relink(relinker, (IGenericParameterProvider)(object)val3); Enumerator enumerator3 = method.Parameters.GetEnumerator(); try { while (enumerator3.MoveNext()) { ParameterDefinition current3 = enumerator3.Current; ((ParameterReference)current3).ParameterType = ((ParameterReference)current3).ParameterType.Relink(relinker, (IGenericParameterProvider)(object)method); val3.Parameters.Add(current3); } } finally { ((IDisposable)enumerator3).Dispose(); } return (IMetadataTokenProvider)(MethodReference)relinker((IMetadataTokenProvider)(object)val3, context); } public static CallSite Relink(this CallSite method, Relinker relinker, IGenericParameterProvider context) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected O, but got Unknown //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_00a0: Unknown result type (might be due to invalid IL or missing references) //IL_00a6: Expected O, but got Unknown CallSite val = new CallSite(method.ReturnType); val.CallingConvention = method.CallingConvention; val.ExplicitThis = method.ExplicitThis; val.HasThis = method.HasThis; val.ReturnType = val.ReturnType?.Relink(relinker, context); Enumerator enumerator = method.Parameters.GetEnumerator(); try { while (enumerator.MoveNext()) { ParameterDefinition current = enumerator.Current; ((ParameterReference)current).ParameterType = ((ParameterReference)current).ParameterType.Relink(relinker, context); val.Parameters.Add(current); } } finally { ((IDisposable)enumerator).Dispose(); } return (CallSite)relinker((IMetadataTokenProvider)(object)val, context); } public static IMetadataTokenProvider Relink(this FieldReference field, Relinker relinker, IGenericParameterProvider context) { //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Expected O, but got Unknown TypeReference val = ((MemberReference)field).DeclaringType.Relink(relinker, context); return relinker((IMetadataTokenProvider)new FieldReference(((MemberReference)field).Name, field.FieldType.Relink(relinker, (IGenericParameterProvider)(object)val), val), context); } public static ParameterDefinition Relink(this ParameterDefinition param, Relinker relinker, IGenericParameterProvider context) { //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_005f: Unknown result type (might be due to invalid IL or missing references) //IL_006b: Unknown result type (might be due to invalid IL or missing references) //IL_0077: Unknown result type (might be due to invalid IL or missing references) //IL_0083: Unknown result type (might be due to invalid IL or missing references) //IL_0090: Expected O, but got Unknown IMethodSignature method = param.Method; IMethodSignature obj = ((method is MethodReference) ? method : null); param = ((obj != null) ? ((MethodReference)obj).Parameters[((ParameterReference)param).Index] : null) ?? param; ParameterDefinition val = new ParameterDefinition(((ParameterReference)param).Name, param.Attributes, ((ParameterReference)param).ParameterType.Relink(relinker, context)) { IsIn = param.IsIn, IsLcid = param.IsLcid, IsOptional = param.IsOptional, IsOut = param.IsOut, IsReturnValue = param.IsReturnValue, MarshalInfo = param.MarshalInfo }; if (param.HasConstant) { val.Constant = param.Constant; } return val; } public static ParameterDefinition Clone(this ParameterDefinition param) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_0060: Expected O, but got Unknown //IL_007a: Unknown result type (might be due to invalid IL or missing references) //IL_007f: Unknown result type (might be due to invalid IL or missing references) ParameterDefinition val = new ParameterDefinition(((ParameterReference)param).Name, param.Attributes, ((ParameterReference)param).ParameterType) { IsIn = param.IsIn, IsLcid = param.IsLcid, IsOptional = param.IsOptional, IsOut = param.IsOut, IsReturnValue = param.IsReturnValue, MarshalInfo = param.MarshalInfo }; if (param.HasConstant) { val.Constant = param.Constant; } Enumerator enumerator = param.CustomAttributes.GetEnumerator(); try { while (enumerator.MoveNext()) { CustomAttribute current = enumerator.Current; val.CustomAttributes.Add(current.Clone()); } return val; } finally { ((IDisposable)enumerator).Dispose(); } } public static CustomAttribute Relink(this CustomAttribute attrib, Relinker relinker, IGenericParameterProvider context) { //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Expected O, but got Unknown //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Expected O, but got Unknown //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_002d: Unknown result type (might be due to invalid IL or missing references) //IL_0049: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0077: Unknown result type (might be due to invalid IL or missing references) //IL_007c: Unknown result type (might be due to invalid IL or missing references) //IL_0081: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Unknown result type (might be due to invalid IL or missing references) //IL_0097: Unknown result type (might be due to invalid IL or missing references) //IL_00a9: Unknown result type (might be due to invalid IL or missing references) //IL_00ae: Unknown result type (might be due to invalid IL or missing references) //IL_00b7: Unknown result type (might be due to invalid IL or missing references) //IL_00bc: Unknown result type (might be due to invalid IL or missing references) //IL_00e5: Unknown result type (might be due to invalid IL or missing references) //IL_00ea: Unknown result type (might be due to invalid IL or missing references) //IL_00ef: Unknown result type (might be due to invalid IL or missing references) //IL_00f4: Unknown result type (might be due to invalid IL or missing references) //IL_0105: Unknown result type (might be due to invalid IL or missing references) //IL_010a: Unknown result type (might be due to invalid IL or missing references) //IL_011c: Unknown result type (might be due to invalid IL or missing references) //IL_0121: Unknown result type (might be due to invalid IL or missing references) //IL_012a: Unknown result type (might be due to invalid IL or missing references) //IL_012f: Unknown result type (might be due to invalid IL or missing references) CustomAttribute val = new CustomAttribute((MethodReference)attrib.Constructor.Relink(relinker, context)); Enumerator enumerator = attrib.ConstructorArguments.GetEnumerator(); try { while (enumerator.MoveNext()) { CustomAttributeArgument current = enumerator.Current; val.ConstructorArguments.Add(new CustomAttributeArgument(((CustomAttributeArgument)(ref current)).Type.Relink(relinker, context), ((CustomAttributeArgument)(ref current)).Value)); } } finally { ((IDisposable)enumerator).Dispose(); } Enumerator enumerator2 = attrib.Fields.GetEnumerator(); CustomAttributeArgument argument; try { while (enumerator2.MoveNext()) { CustomAttributeNamedArgument current2 = enumerator2.Current; Collection fields = val.Fields; string name = ((CustomAttributeNamedArgument)(ref current2)).Name; argument = ((CustomAttributeNamedArgument)(ref current2)).Argument; TypeReference obj = ((CustomAttributeArgument)(ref argument)).Type.Relink(relinker, context); argument = ((CustomAttributeNamedArgument)(ref current2)).Argument; fields.Add(new CustomAttributeNamedArgument(name, new CustomAttributeArgument(obj, ((CustomAttributeArgument)(ref argument)).Value))); } } finally { ((IDisposable)enumerator2).Dispose(); } enumerator2 = attrib.Properties.GetEnumerator(); try { while (enumerator2.MoveNext()) { CustomAttributeNamedArgument current3 = enumerator2.Current; Collection properties = val.Properties; string name2 = ((CustomAttributeNamedArgument)(ref current3)).Name; argument = ((CustomAttributeNamedArgument)(ref current3)).Argument; TypeReference obj2 = ((CustomAttributeArgument)(ref argument)).Type.Relink(relinker, context); argument = ((CustomAttributeNamedArgument)(ref current3)).Argument; properties.Add(new CustomAttributeNamedArgument(name2, new CustomAttributeArgument(obj2, ((CustomAttributeArgument)(ref argument)).Value))); } return val; } finally { ((IDisposable)enumerator2).Dispose(); } } public static CustomAttribute Clone(this CustomAttribute attrib) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected O, but got Unknown //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Unknown result type (might be due to invalid IL or missing references) //IL_005f: Unknown result type (might be due to invalid IL or missing references) //IL_0064: Unknown result type (might be due to invalid IL or missing references) //IL_0069: Unknown result type (might be due to invalid IL or missing references) //IL_006e: Unknown result type (might be due to invalid IL or missing references) //IL_007f: Unknown result type (might be due to invalid IL or missing references) //IL_0084: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_00a2: Unknown result type (might be due to invalid IL or missing references) //IL_00cb: Unknown result type (might be due to invalid IL or missing references) //IL_00d0: Unknown result type (might be due to invalid IL or missing references) //IL_00d5: Unknown result type (might be due to invalid IL or missing references) //IL_00da: Unknown result type (might be due to invalid IL or missing references) //IL_00eb: Unknown result type (might be due to invalid IL or missing references) //IL_00f0: Unknown result type (might be due to invalid IL or missing references) //IL_00fb: Unknown result type (might be due to invalid IL or missing references) //IL_0100: Unknown result type (might be due to invalid IL or missing references) //IL_0109: Unknown result type (might be due to invalid IL or missing references) //IL_010e: Unknown result type (might be due to invalid IL or missing references) CustomAttribute val = new CustomAttribute(attrib.Constructor); Enumerator enumerator = attrib.ConstructorArguments.GetEnumerator(); try { while (enumerator.MoveNext()) { CustomAttributeArgument current = enumerator.Current; val.ConstructorArguments.Add(new CustomAttributeArgument(((CustomAttributeArgument)(ref current)).Type, ((CustomAttributeArgument)(ref current)).Value)); } } finally { ((IDisposable)enumerator).Dispose(); } Enumerator enumerator2 = attrib.Fields.GetEnumerator(); CustomAttributeArgument argument; try { while (enumerator2.MoveNext()) { CustomAttributeNamedArgument current2 = enumerator2.Current; Collection fields = val.Fields; string name = ((CustomAttributeNamedArgument)(ref current2)).Name; argument = ((CustomAttributeNamedArgument)(ref current2)).Argument; TypeReference type = ((CustomAttributeArgument)(ref argument)).Type; argument = ((CustomAttributeNamedArgument)(ref current2)).Argument; fields.Add(new CustomAttributeNamedArgument(name, new CustomAttributeArgument(type, ((CustomAttributeArgument)(ref argument)).Value))); } } finally { ((IDisposable)enumerator2).Dispose(); } enumerator2 = attrib.Properties.GetEnumerator(); try { while (enumerator2.MoveNext()) { CustomAttributeNamedArgument current3 = enumerator2.Current; Collection properties = val.Properties; string name2 = ((CustomAttributeNamedArgument)(ref current3)).Name; argument = ((CustomAttributeNamedArgument)(ref current3)).Argument; TypeReference type2 = ((CustomAttributeArgument)(ref argument)).Type; argument = ((CustomAttributeNamedArgument)(ref current3)).Argument; properties.Add(new CustomAttributeNamedArgument(name2, new CustomAttributeArgument(type2, ((CustomAttributeArgument)(ref argument)).Value))); } return val; } finally { ((IDisposable)enumerator2).Dispose(); } } public static GenericParameter Relink(this GenericParameter param, Relinker relinker, IGenericParameterProvider context) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Expected O, but got Unknown //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0077: Unknown result type (might be due to invalid IL or missing references) //IL_007c: Unknown result type (might be due to invalid IL or missing references) GenericParameter val = Update(new GenericParameter(((MemberReference)param).Name, param.Owner) { Attributes = param.Attributes }, param.Position, param.Type); Enumerator enumerator = param.CustomAttributes.GetEnumerator(); try { while (enumerator.MoveNext()) { CustomAttribute current = enumerator.Current; val.CustomAttributes.Add(current.Relink(relinker, context)); } } finally { ((IDisposable)enumerator).Dispose(); } Enumerator enumerator2 = param.Constraints.GetEnumerator(); try { while (enumerator2.MoveNext()) { GenericParameterConstraint current2 = enumerator2.Current; val.Constraints.Add(current2.Relink(relinker, context)); } return val; } finally { ((IDisposable)enumerator2).Dispose(); } } public static GenericParameter Clone(this GenericParameter param) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Expected O, but got Unknown //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0075: Unknown result type (might be due to invalid IL or missing references) //IL_007a: Unknown result type (might be due to invalid IL or missing references) GenericParameter val = Update(new GenericParameter(((MemberReference)param).Name, param.Owner) { Attributes = param.Attributes }, param.Position, param.Type); Enumerator enumerator = param.CustomAttributes.GetEnumerator(); try { while (enumerator.MoveNext()) { CustomAttribute current = enumerator.Current; val.CustomAttributes.Add(current.Clone()); } } finally { ((IDisposable)enumerator).Dispose(); } Enumerator enumerator2 = param.Constraints.GetEnumerator(); try { while (enumerator2.MoveNext()) { GenericParameterConstraint current2 = enumerator2.Current; val.Constraints.Add(current2); } return val; } finally { ((IDisposable)enumerator2).Dispose(); } } public static int GetManagedSize(this Type t) { //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Expected O, but got Unknown //IL_0052: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_0069: Expected O, but got Unknown //IL_008a: Unknown result type (might be due to invalid IL or missing references) //IL_0091: Expected O, but got Unknown //IL_0098: Unknown result type (might be due to invalid IL or missing references) //IL_009f: Expected O, but got Unknown //IL_00c7: Unknown result type (might be due to invalid IL or missing references) //IL_00d3: Unknown result type (might be due to invalid IL or missing references) if (_GetManagedSizeCache.TryGetValue(t, out var value)) { return value; } if (_GetManagedSizeHelper == null) { ModuleDefinition val = ModuleDefinition.CreateModule("MonoMod.Utils.GetManagedSizeHelper", new ModuleParameters { Kind = (ModuleKind)0, ReflectionImporterProvider = MMReflectionImporter.Provider }); Assembly assembly; try { TypeDefinition val2 = new TypeDefinition("MonoMod.Utils", "GetManagedSizeHelper", (TypeAttributes)385) { BaseType = val.TypeSystem.Object }; val.Types.Add(val2); MethodDefinition val3 = new MethodDefinition("GetManagedSizeHelper", (MethodAttributes)150, val.TypeSystem.Int32); GenericParameter val4 = new GenericParameter("T", (IGenericParameterProvider)(object)val3); ((MethodReference)val3).GenericParameters.Add(val4); val2.Methods.Add(val3); ILProcessor iLProcessor = val3.Body.GetILProcessor(); iLProcessor.Emit(OpCodes.Sizeof, (TypeReference)(object)val4); iLProcessor.Emit(OpCodes.Ret); assembly = ReflectionHelper.Load(val); } finally { ((IDisposable)val)?.Dispose(); } _GetManagedSizeHelper = assembly.GetType("MonoMod.Utils.GetManagedSizeHelper").GetMethod("GetManagedSizeHelper"); } value = _GetManagedSizeHelper.MakeGenericMethod(t).CreateDelegate>()(); lock (_GetManagedSizeCache) { return _GetManagedSizeCache[t] = value; } } public static Type GetThisParamType(this MethodBase method) { Type type = method.DeclaringType; if (type.IsValueType) { type = type.MakeByRefType(); } return type; } public static IntPtr GetLdftnPointer(this MethodBase m) { //IL_004c: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) if (_GetLdftnPointerCache.TryGetValue(m, out var value)) { return value(); } DynamicMethodDefinition dynamicMethodDefinition = new DynamicMethodDefinition("GetLdftnPointer<" + m.GetID(null, null, withType: true, proxyMethod: false, simple: true) + ">", typeof(IntPtr), Type.EmptyTypes); ILProcessor iLProcessor = dynamicMethodDefinition.GetILProcessor(); iLProcessor.Emit(OpCodes.Ldftn, ((MemberReference)dynamicMethodDefinition.Definition).Module.ImportReference(m)); iLProcessor.Emit(OpCodes.Ret); lock (_GetLdftnPointerCache) { Func func2 = (_GetLdftnPointerCache[m] = dynamicMethodDefinition.Generate().CreateDelegate>()); return func2(); } } } public delegate object FastReflectionDelegate(object target, params object[] args); public static class FastReflectionHelper { private static readonly Type[] _DynamicMethodDelegateArgs = new Type[2] { typeof(object), typeof(object[]) }; private static readonly Dictionary _MethodCache = new Dictionary(); private static FastReflectionDelegate _CreateFastDelegate(MethodBase method, bool directBoxValueAccess = true) { //IL_0048: Unknown result type (might be due to invalid IL or missing references) //IL_0060: Unknown result type (might be due to invalid IL or missing references) //IL_01da: Unknown result type (might be due to invalid IL or missing references) //IL_00c5: Unknown result type (might be due to invalid IL or missing references) //IL_00d0: Unknown result type (might be due to invalid IL or missing references) //IL_01fd: Unknown result type (might be due to invalid IL or missing references) //IL_00ff: Unknown result type (might be due to invalid IL or missing references) //IL_00ad: Unknown result type (might be due to invalid IL or missing references) //IL_00b8: Unknown result type (might be due to invalid IL or missing references) //IL_0210: Unknown result type (might be due to invalid IL or missing references) //IL_00e5: Unknown result type (might be due to invalid IL or missing references) //IL_011b: Unknown result type (might be due to invalid IL or missing references) //IL_0269: Unknown result type (might be due to invalid IL or missing references) //IL_012f: Unknown result type (might be due to invalid IL or missing references) //IL_013c: Unknown result type (might be due to invalid IL or missing references) //IL_0147: Unknown result type (might be due to invalid IL or missing references) //IL_01b5: Unknown result type (might be due to invalid IL or missing references) //IL_0274: Unknown result type (might be due to invalid IL or missing references) //IL_025a: Unknown result type (might be due to invalid IL or missing references) //IL_0192: Unknown result type (might be due to invalid IL or missing references) //IL_019d: Unknown result type (might be due to invalid IL or missing references) //IL_01a8: Unknown result type (might be due to invalid IL or missing references) //IL_017d: Unknown result type (might be due to invalid IL or missing references) //IL_0187: Expected O, but got Unknown //IL_0182: Unknown result type (might be due to invalid IL or missing references) //IL_018c: Expected O, but got Unknown //IL_0187: Unknown result type (might be due to invalid IL or missing references) //IL_0191: Expected O, but got Unknown DynamicMethodDefinition dynamicMethodDefinition = new DynamicMethodDefinition("FastReflection<" + method.GetID(null, null, withType: true, proxyMethod: false, simple: true) + ">", typeof(object), _DynamicMethodDelegateArgs); ILProcessor iLProcessor = dynamicMethodDefinition.GetILProcessor(); ParameterInfo[] parameters = method.GetParameters(); bool flag = true; if (!method.IsStatic) { iLProcessor.Emit(OpCodes.Ldarg_0); if (method.DeclaringType.IsValueType) { iLProcessor.Emit(OpCodes.Unbox_Any, method.DeclaringType); } } for (int i = 0; i < parameters.Length; i++) { Type type = parameters[i].ParameterType; bool isByRef = type.IsByRef; if (isByRef) { type = type.GetElementType(); } bool isValueType = type.IsValueType; if (isByRef && isValueType && !directBoxValueAccess) { iLProcessor.Emit(OpCodes.Ldarg_1); iLProcessor.Emit(OpCodes.Ldc_I4, i); } iLProcessor.Emit(OpCodes.Ldarg_1); iLProcessor.Emit(OpCodes.Ldc_I4, i); if (isByRef && !isValueType) { iLProcessor.Emit(OpCodes.Ldelema, typeof(object)); continue; } iLProcessor.Emit(OpCodes.Ldelem_Ref); if (!isValueType) { continue; } if (!isByRef || !directBoxValueAccess) { iLProcessor.Emit(OpCodes.Unbox_Any, type); if (isByRef) { iLProcessor.Emit(OpCodes.Box, type); iLProcessor.Emit(OpCodes.Dup); iLProcessor.Emit(OpCodes.Unbox, type); if (flag) { flag = false; dynamicMethodDefinition.Definition.Body.Variables.Add(new VariableDefinition((TypeReference)new PinnedType((TypeReference)new PointerType(((MemberReference)dynamicMethodDefinition.Definition).Module.TypeSystem.Void)))); } iLProcessor.Emit(OpCodes.Stloc_0); iLProcessor.Emit(OpCodes.Stelem_Ref); iLProcessor.Emit(OpCodes.Ldloc_0); } } else { iLProcessor.Emit(OpCodes.Unbox, type); } } if (method.IsConstructor) { iLProcessor.Emit(OpCodes.Newobj, method as ConstructorInfo); } else if (method.IsFinal || !method.IsVirtual) { iLProcessor.Emit(OpCodes.Call, method as MethodInfo); } else { iLProcessor.Emit(OpCodes.Callvirt, method as MethodInfo); } Type type2 = (method.IsConstructor ? method.DeclaringType : (method as MethodInfo).ReturnType); if (type2 != typeof(void)) { if (type2.IsValueType) { iLProcessor.Emit(OpCodes.Box, type2); } } else { iLProcessor.Emit(OpCodes.Ldnull); } iLProcessor.Emit(OpCodes.Ret); return (FastReflectionDelegate)dynamicMethodDefinition.Generate().CreateDelegate(typeof(FastReflectionDelegate)); } public static FastReflectionDelegate CreateFastDelegate(this MethodInfo method, bool directBoxValueAccess = true) { return method.GetFastDelegate(directBoxValueAccess); } public static FastReflectionDelegate GetFastDelegate(this MethodInfo method, bool directBoxValueAccess = true) { if (_MethodCache.TryGetValue(method, out var value)) { return value; } value = _CreateFastDelegate(method, directBoxValueAccess); _MethodCache.Add(method, value); return value; } } public class GenericMethodInstantiationComparer : IEqualityComparer { internal static Type CannonicalFillType = typeof(object).Assembly.GetType("System.__Canon"); private readonly IEqualityComparer genericTypeComparer; public GenericMethodInstantiationComparer() : this(new GenericTypeInstantiationComparer()) { } public GenericMethodInstantiationComparer(IEqualityComparer typeComparer) { genericTypeComparer = typeComparer; } public bool Equals(MethodBase x, MethodBase y) { if ((object)x == null && (object)y == null) { return true; } if ((object)x == null || (object)y == null) { return false; } bool flag = (x.IsGenericMethod && !x.ContainsGenericParameters) || (x.DeclaringType?.IsGenericType ?? false); bool flag2 = (y.IsGenericMethod && !y.ContainsGenericParameters) || (y.DeclaringType?.IsGenericType ?? false); if (flag != flag2) { return false; } if (!flag) { return x.Equals(y); } if (!genericTypeComparer.Equals(x.DeclaringType, y.DeclaringType)) { return false; } MethodBase methodBase = ((!(x is MethodInfo method)) ? x.GetUnfilledMethodOnGenericType() : method.GetActualGenericMethodDefinition()); MethodBase methodBase2 = ((!(y is MethodInfo method2)) ? y.GetUnfilledMethodOnGenericType() : method2.GetActualGenericMethodDefinition()); if (!methodBase.Equals(methodBase2)) { return false; } if (methodBase.Name != methodBase2.Name) { return false; } ParameterInfo[] parameters = x.GetParameters(); ParameterInfo[] parameters2 = y.GetParameters(); if (parameters.Length != parameters2.Length) { return false; } ParameterInfo[] parameters3 = methodBase.GetParameters(); methodBase2.GetParameters(); for (int i = 0; i < parameters.Length; i++) { Type type = parameters[i].ParameterType; Type type2 = parameters2[i].ParameterType; if (parameters3[i].ParameterType.IsGenericParameter) { if (!type.IsValueType) { type = CannonicalFillType ?? typeof(object); } if (!type2.IsValueType) { type2 = CannonicalFillType ?? typeof(object); } } if (!genericTypeComparer.Equals(type, type2)) { return false; } } return true; } public int GetHashCode(MethodBase method) { if (!method.IsGenericMethod || method.ContainsGenericParameters) { Type? declaringType = method.DeclaringType; if ((object)declaringType == null || !declaringType.IsGenericType) { return method.GetHashCode(); } } int num = -559038737; if (method.DeclaringType != null) { num ^= method.DeclaringType.Assembly.GetHashCode(); num ^= genericTypeComparer.GetHashCode(method.DeclaringType); } num ^= method.Name.GetHashCode(StringComparison.Ordinal); ParameterInfo[] parameters = method.GetParameters(); int num2 = parameters.Length; num2 ^= num2 << 4; num2 ^= num2 << 8; num2 ^= num2 << 16; num ^= num2; if (method.IsGenericMethod) { Type[] genericArguments = method.GetGenericArguments(); for (int i = 0; i < genericArguments.Length; i++) { int num3 = i % 32; Type type = genericArguments[i]; int num4 = (type.IsValueType ? genericTypeComparer.GetHashCode(type) : (CannonicalFillType?.GetHashCode() ?? 1431655765)); num4 = (num4 << num3) | (num4 >> 32 - num3); num ^= num4; } } MethodBase methodBase = ((!(method is MethodInfo method2)) ? method.GetUnfilledMethodOnGenericType() : method2.GetActualGenericMethodDefinition()); ParameterInfo[] parameters2 = methodBase.GetParameters(); for (int j = 0; j < parameters.Length; j++) { int num5 = j % 32; Type parameterType = parameters[j].ParameterType; int num6 = genericTypeComparer.GetHashCode(parameterType); if (parameters2[j].ParameterType.IsGenericParameter && !parameterType.IsValueType) { num6 = CannonicalFillType?.GetHashCode() ?? 1431655765; } num6 = (num6 >> num5) | (num6 << 32 - num5); num ^= num6; } return num; } } public class GenericTypeInstantiationComparer : IEqualityComparer { private static Type CannonicalFillType = GenericMethodInstantiationComparer.CannonicalFillType; public bool Equals(Type x, Type y) { if ((object)x == null && (object)y == null) { return true; } if ((object)x == null || (object)y == null) { return false; } bool isGenericType = x.IsGenericType; bool isGenericType2 = y.IsGenericType; if (isGenericType != isGenericType2) { return false; } if (!isGenericType) { return x.Equals(y); } Type genericTypeDefinition = x.GetGenericTypeDefinition(); Type genericTypeDefinition2 = y.GetGenericTypeDefinition(); if (!genericTypeDefinition.Equals(genericTypeDefinition2)) { return false; } Type[] genericArguments = x.GetGenericArguments(); Type[] genericArguments2 = y.GetGenericArguments(); if (genericArguments.Length != genericArguments2.Length) { return false; } for (int i = 0; i < genericArguments.Length; i++) { Type type = genericArguments[i]; Type type2 = genericArguments2[i]; if (!type.IsValueType) { type = CannonicalFillType ?? typeof(object); } if (!type2.IsValueType) { type2 = CannonicalFillType ?? typeof(object); } if (!Equals(type, type2)) { return false; } } return true; } public int GetHashCode(Type type) { if (!type.IsGenericType) { return type.GetHashCode(); } int num = -559038737; num ^= (num << 16) | (num >> 16); num ^= type.Assembly.GetHashCode(); if (type.Namespace != null) { num ^= type.Namespace.GetHashCode(StringComparison.Ordinal); } num ^= type.Name.GetHashCode(StringComparison.Ordinal); Type[] genericArguments = type.GetGenericArguments(); for (int i = 0; i < genericArguments.Length; i++) { int num2 = i % 8 * 4; Type type2 = genericArguments[i]; int num3 = (type2.IsValueType ? GetHashCode(type2) : (CannonicalFillType?.GetHashCode() ?? (-1717986919))); num ^= (num3 << num2) | (num3 >> 32 - num2); } return num; } } public sealed class LazyDisposable : IDisposable { public event Action OnDispose; public LazyDisposable() { } public LazyDisposable(Action a) : this() { OnDispose += a; } public void Dispose() { this.OnDispose?.Invoke(); } } public sealed class LazyDisposable : IDisposable { private T Instance; public event Action OnDispose; public LazyDisposable(T instance) { Instance = instance; } public LazyDisposable(T instance, Action a) : this(instance) { OnDispose += a; } public void Dispose() { this.OnDispose?.Invoke(Instance); Instance = default(T); } } internal interface _IDMDGenerator { MethodInfo Generate(DynamicMethodDefinition dmd, object context); } public abstract class DMDGenerator : _IDMDGenerator where TSelf : DMDGenerator, new() { private static TSelf _Instance; protected abstract MethodInfo _Generate(DynamicMethodDefinition dmd, object context); MethodInfo _IDMDGenerator.Generate(DynamicMethodDefinition dmd, object context) { return _Postbuild(_Generate(dmd, context)); } public static MethodInfo Generate(DynamicMethodDefinition dmd, object context = null) { return _Postbuild((_Instance ?? (_Instance = new TSelf()))._Generate(dmd, context)); } internal static MethodInfo _Postbuild(MethodInfo mi) { if (mi == null) { return null; } if (ReflectionHelper.IsMono && !(mi is DynamicMethod) && mi.DeclaringType != null) { Module module = mi?.Module; if (module == null) { return mi; } Assembly assembly = module.Assembly; if (assembly?.GetType() == null) { return mi; } assembly.SetMonoCorlibInternal(value: true); } return mi; } } public sealed class DMDCecilGenerator : DMDGenerator { protected override MethodInfo _Generate(DynamicMethodDefinition dmd, object context) { //IL_004d: Unknown result type (might be due to invalid IL or missing references) //IL_0052: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Unknown result type (might be due to invalid IL or missing references) //IL_0069: Expected O, but got Unknown //IL_008e: Unknown result type (might be due to invalid IL or missing references) //IL_0098: Expected O, but got Unknown //IL_0194: Unknown result type (might be due to invalid IL or missing references) //IL_019b: Expected O, but got Unknown //IL_00b0: Unknown result type (might be due to invalid IL or missing references) //IL_00b7: Expected O, but got Unknown //IL_00dd: Unknown result type (might be due to invalid IL or missing references) //IL_01d8: Unknown result type (might be due to invalid IL or missing references) //IL_01ed: Unknown result type (might be due to invalid IL or missing references) //IL_01f2: Unknown result type (might be due to invalid IL or missing references) //IL_0203: Unknown result type (might be due to invalid IL or missing references) //IL_020e: Unknown result type (might be due to invalid IL or missing references) //IL_0215: Unknown result type (might be due to invalid IL or missing references) //IL_021c: Unknown result type (might be due to invalid IL or missing references) //IL_0228: Expected O, but got Unknown //IL_0233: Unknown result type (might be due to invalid IL or missing references) //IL_0238: Unknown result type (might be due to invalid IL or missing references) //IL_013f: Unknown result type (might be due to invalid IL or missing references) //IL_0144: Unknown result type (might be due to invalid IL or missing references) //IL_015b: Expected O, but got Unknown //IL_0304: Unknown result type (might be due to invalid IL or missing references) //IL_0309: Unknown result type (might be due to invalid IL or missing references) //IL_0355: Unknown result type (might be due to invalid IL or missing references) //IL_035a: Unknown result type (might be due to invalid IL or missing references) //IL_05cb: Unknown result type (might be due to invalid IL or missing references) //IL_05d5: Expected O, but got Unknown //IL_05a3: Unknown result type (might be due to invalid IL or missing references) //IL_05aa: Expected O, but got Unknown //IL_041e: Unknown result type (might be due to invalid IL or missing references) //IL_042a: Unknown result type (might be due to invalid IL or missing references) //IL_042f: Unknown result type (might be due to invalid IL or missing references) //IL_044b: Unknown result type (might be due to invalid IL or missing references) //IL_0450: Unknown result type (might be due to invalid IL or missing references) //IL_048d: Unknown result type (might be due to invalid IL or missing references) //IL_0497: Expected O, but got Unknown //IL_04f9: Unknown result type (might be due to invalid IL or missing references) //IL_0500: Expected O, but got Unknown //IL_0523: Unknown result type (might be due to invalid IL or missing references) MethodDefinition def = dmd.Definition; TypeDefinition val = (TypeDefinition)((context is TypeDefinition) ? context : null); bool flag = false; ModuleDefinition module = ((val != null) ? ((MemberReference)val).Module : null); HashSet hashSet = null; if (val == null) { flag = true; hashSet = new HashSet(); string dumpName = dmd.GetDumpName("Cecil"); module = ModuleDefinition.CreateModule(dumpName, new ModuleParameters { Kind = (ModuleKind)0, ReflectionImporterProvider = MMReflectionImporter.ProviderNoDefault }); module.Assembly.CustomAttributes.Add(new CustomAttribute(module.ImportReference((MethodBase)DynamicMethodDefinition.c_UnverifiableCodeAttribute))); if (dmd.Debug) { CustomAttribute val2 = new CustomAttribute(module.ImportReference((MethodBase)DynamicMethodDefinition.c_DebuggableAttribute)); val2.ConstructorArguments.Add(new CustomAttributeArgument(module.ImportReference(typeof(DebuggableAttribute.DebuggingModes)), (object)(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations))); module.Assembly.CustomAttributes.Add(val2); } val = new TypeDefinition("", $"DMD<{dmd.OriginalMethod?.Name?.Replace('.', '_')}>?{GetHashCode()}", (TypeAttributes)385) { BaseType = module.TypeSystem.Object }; module.Types.Add(val); } try { MethodDefinition clone = null; TypeReference val3 = new TypeReference("System.Runtime.CompilerServices", "IsVolatile", module, module.TypeSystem.CoreLibrary); Relinker relinker = (IMetadataTokenProvider mtp, IGenericParameterProvider ctx) => (IMetadataTokenProvider)((mtp == def) ? ((object)clone) : ((object)module.ImportReference(mtp))); clone = new MethodDefinition(dmd.Name ?? ("_" + ((MemberReference)def).Name.Replace('.', '_')), def.Attributes, module.TypeSystem.Void) { MethodReturnType = ((MethodReference)def).MethodReturnType, Attributes = (MethodAttributes)150, ImplAttributes = (MethodImplAttributes)0, DeclaringType = val, NoInlining = true }; Enumerator enumerator = ((MethodReference)def).Parameters.GetEnumerator(); try { while (enumerator.MoveNext()) { ParameterDefinition current = enumerator.Current; ((MethodReference)clone).Parameters.Add(current.Clone().Relink(relinker, (IGenericParameterProvider)(object)clone)); } } finally { ((IDisposable)enumerator).Dispose(); } ((MethodReference)clone).ReturnType = ((MethodReference)def).ReturnType.Relink(relinker, (IGenericParameterProvider)(object)clone); val.Methods.Add(clone); ((MethodReference)clone).HasThis = ((MethodReference)def).HasThis; MethodBody val5 = (clone.Body = def.Body.Clone(clone)); MethodBody val6 = val5; Enumerator enumerator2 = clone.Body.Variables.GetEnumerator(); try { while (enumerator2.MoveNext()) { VariableDefinition current2 = enumerator2.Current; ((VariableReference)current2).VariableType = ((VariableReference)current2).VariableType.Relink(relinker, (IGenericParameterProvider)(object)clone); } } finally { ((IDisposable)enumerator2).Dispose(); } Enumerator enumerator3 = clone.Body.ExceptionHandlers.GetEnumerator(); try { while (enumerator3.MoveNext()) { ExceptionHandler current3 = enumerator3.Current; if (current3.CatchType != null) { current3.CatchType = current3.CatchType.Relink(relinker, (IGenericParameterProvider)(object)clone); } } } finally { ((IDisposable)enumerator3).Dispose(); } for (int i = 0; i < val6.Instructions.Count; i++) { Instruction obj = val6.Instructions[i]; object obj2 = obj.Operand; ParameterDefinition val7 = (ParameterDefinition)((obj2 is ParameterDefinition) ? obj2 : null); if (val7 != null) { obj2 = ((MethodReference)clone).Parameters[((ParameterReference)val7).Index]; } else { IMetadataTokenProvider val8 = (IMetadataTokenProvider)((obj2 is IMetadataTokenProvider) ? obj2 : null); if (val8 != null) { obj2 = val8.Relink(relinker, (IGenericParameterProvider)(object)clone); } } Instruction previous = obj.Previous; OpCode? val9 = ((previous != null) ? new OpCode?(previous.OpCode) : null); OpCode @volatile = OpCodes.Volatile; if (val9.HasValue && (!val9.HasValue || val9.GetValueOrDefault() == @volatile)) { FieldReference val10 = (FieldReference)((obj2 is FieldReference) ? obj2 : null); if (val10 != null) { TypeReference fieldType = val10.FieldType; TypeReference obj3 = ((fieldType is RequiredModifierType) ? fieldType : null); if (((obj3 != null) ? ((RequiredModifierType)obj3).ModifierType : null) != val3) { val10.FieldType = (TypeReference)new RequiredModifierType(val3, val10.FieldType); } } } _ = obj2 is DynamicMethodReference; if (hashSet != null) { MemberReference val11 = (MemberReference)((obj2 is MemberReference) ? obj2 : null); if (val11 != null) { MemberReference obj4 = ((val11 is TypeReference) ? val11 : null); IMetadataScope val12 = ((obj4 != null) ? ((TypeReference)obj4).Scope : null) ?? val11.DeclaringType.Scope; if (!hashSet.Contains(val12.Name)) { CustomAttribute val13 = new CustomAttribute(module.ImportReference((MethodBase)DynamicMethodDefinition.c_IgnoresAccessChecksToAttribute)); val13.ConstructorArguments.Add(new CustomAttributeArgument(module.ImportReference(typeof(DebuggableAttribute.DebuggingModes)), (object)val12.Name)); module.Assembly.CustomAttributes.Add(val13); hashSet.Add(val12.Name); } } } obj.Operand = obj2; } ((MethodReference)clone).HasThis = false; if (((MethodReference)def).HasThis) { TypeReference val14 = (TypeReference)(object)def.DeclaringType; if (val14.IsValueType) { val14 = (TypeReference)new ByReferenceType(val14); } ((MethodReference)clone).Parameters.Insert(0, new ParameterDefinition("<>_this", (ParameterAttributes)0, val14.Relink(relinker, (IGenericParameterProvider)(object)clone))); } if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MONOMOD_DMD_DUMP"))) { string fullPath = Path.GetFullPath(Environment.GetEnvironmentVariable("MONOMOD_DMD_DUMP")); string path = ((ModuleReference)module).Name + ".dll"; string path2 = Path.Combine(fullPath, path); fullPath = Path.GetDirectoryName(path2); if (!string.IsNullOrEmpty(fullPath) && !Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } if (File.Exists(path2)) { File.Delete(path2); } using Stream stream = File.OpenWrite(path2); module.Write(stream); } return ReflectionHelper.Load(module).GetType(((MemberReference)val).FullName.Replace("+", "\\+", StringComparison.Ordinal), throwOnError: false, ignoreCase: false).GetMethod(((MemberReference)clone).Name, BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); } finally { if (flag) { module.Dispose(); } } } } internal static class _DMDEmit { private static readonly Dictionary _ReflOpCodes; private static readonly Dictionary _CecilOpCodes; private static readonly MethodInfo _ILGen_make_room; private static readonly MethodInfo _ILGen_emit_int; private static readonly MethodInfo _ILGen_ll_emit; private static readonly MethodInfo _ILGen_EnsureCapacity; private static readonly MethodInfo _ILGen_PutInteger4; private static readonly MethodInfo _ILGen_InternalEmit; private static readonly MethodInfo _ILGen_UpdateStackSize; private static readonly FieldInfo f_DynILGen_m_scope; private static readonly FieldInfo f_DynScope_m_tokens; private static readonly Type[] CorElementTypes; static _DMDEmit() { //IL_024d: Unknown result type (might be due to invalid IL or missing references) //IL_0252: Unknown result type (might be due to invalid IL or missing references) //IL_025f: Unknown result type (might be due to invalid IL or missing references) _ReflOpCodes = new Dictionary(); _CecilOpCodes = new Dictionary(); _ILGen_make_room = typeof(ILGenerator).GetMethod("make_room", BindingFlags.Instance | BindingFlags.NonPublic); _ILGen_emit_int = typeof(ILGenerator).GetMethod("emit_int", BindingFlags.Instance | BindingFlags.NonPublic); _ILGen_ll_emit = typeof(ILGenerator).GetMethod("ll_emit", BindingFlags.Instance | BindingFlags.NonPublic); _ILGen_EnsureCapacity = typeof(ILGenerator).GetMethod("EnsureCapacity", BindingFlags.Instance | BindingFlags.NonPublic); _ILGen_PutInteger4 = typeof(ILGenerator).GetMethod("PutInteger4", BindingFlags.Instance | BindingFlags.NonPublic); _ILGen_InternalEmit = typeof(ILGenerator).GetMethod("InternalEmit", BindingFlags.Instance | BindingFlags.NonPublic); _ILGen_UpdateStackSize = typeof(ILGenerator).GetMethod("UpdateStackSize", BindingFlags.Instance | BindingFlags.NonPublic); f_DynILGen_m_scope = typeof(ILGenerator).Assembly.GetType("System.Reflection.Emit.DynamicILGenerator")?.GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic); f_DynScope_m_tokens = typeof(ILGenerator).Assembly.GetType("System.Reflection.Emit.DynamicScope")?.GetField("m_tokens", BindingFlags.Instance | BindingFlags.NonPublic); CorElementTypes = new Type[14] { null, typeof(void), typeof(bool), typeof(char), typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(string), typeof(IntPtr) }; FieldInfo[] fields = typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < fields.Length; i++) { OpCode value = (OpCode)fields[i].GetValue(null); _ReflOpCodes[value.Value] = value; } fields = typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < fields.Length; i++) { OpCode value2 = (OpCode)fields[i].GetValue(null); _CecilOpCodes[((OpCode)(ref value2)).Value] = value2; } } public static void Generate(DynamicMethodDefinition dmd, MethodBase _mb, ILGenerator il) { //IL_00ba: Unknown result type (might be due to invalid IL or missing references) //IL_00bf: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_0068: Expected I4, but got Unknown //IL_01a0: Unknown result type (might be due to invalid IL or missing references) //IL_01a5: Unknown result type (might be due to invalid IL or missing references) //IL_01de: Unknown result type (might be due to invalid IL or missing references) //IL_01e3: Unknown result type (might be due to invalid IL or missing references) //IL_0324: Unknown result type (might be due to invalid IL or missing references) //IL_0329: Unknown result type (might be due to invalid IL or missing references) //IL_032d: Unknown result type (might be due to invalid IL or missing references) //IL_0333: Invalid comparison between Unknown and I4 //IL_0342: Unknown result type (might be due to invalid IL or missing references) //IL_0347: Unknown result type (might be due to invalid IL or missing references) //IL_024e: Unknown result type (might be due to invalid IL or missing references) //IL_0253: Unknown result type (might be due to invalid IL or missing references) //IL_0255: Unknown result type (might be due to invalid IL or missing references) //IL_0270: Expected I4, but got Unknown //IL_02c3: Unknown result type (might be due to invalid IL or missing references) //IL_02c8: Unknown result type (might be due to invalid IL or missing references) //IL_02ca: Unknown result type (might be due to invalid IL or missing references) //IL_02cd: Invalid comparison between Unknown and I4 //IL_03e1: Unknown result type (might be due to invalid IL or missing references) //IL_03e6: Unknown result type (might be due to invalid IL or missing references) //IL_03a8: Unknown result type (might be due to invalid IL or missing references) //IL_03ad: Unknown result type (might be due to invalid IL or missing references) //IL_04e9: Unknown result type (might be due to invalid IL or missing references) //IL_04ee: Unknown result type (might be due to invalid IL or missing references) //IL_02d8: Unknown result type (might be due to invalid IL or missing references) //IL_02dd: Unknown result type (might be due to invalid IL or missing references) //IL_02cf: Unknown result type (might be due to invalid IL or missing references) //IL_02d2: Invalid comparison between Unknown and I4 //IL_02f0: Unknown result type (might be due to invalid IL or missing references) //IL_02f5: Unknown result type (might be due to invalid IL or missing references) //IL_04bf: Unknown result type (might be due to invalid IL or missing references) //IL_04c4: Unknown result type (might be due to invalid IL or missing references) //IL_0476: Unknown result type (might be due to invalid IL or missing references) //IL_047b: Unknown result type (might be due to invalid IL or missing references) MethodDefinition definition = dmd.Definition; DynamicMethod dynamicMethod = _mb as DynamicMethod; if (dmd.Debug) { _ = definition.DebugInformation; } if (dynamicMethod != null) { Enumerator enumerator = ((MethodReference)definition).Parameters.GetEnumerator(); try { while (enumerator.MoveNext()) { ParameterDefinition current = enumerator.Current; dynamicMethod.DefineParameter(((ParameterReference)current).Index + 1, (ParameterAttributes)current.Attributes, ((ParameterReference)current).Name); } } finally { ((IDisposable)enumerator).Dispose(); } } LocalBuilder[] array = ((IEnumerable)definition.Body.Variables).Select((VariableDefinition var) => il.DeclareLocal(((VariableReference)var).VariableType.ResolveReflection(), var.IsPinned)).ToArray(); Dictionary labelMap = new Dictionary(); Enumerator enumerator2 = definition.Body.Instructions.GetEnumerator(); try { while (enumerator2.MoveNext()) { Instruction current2 = enumerator2.Current; if (current2.Operand is Instruction[] array2) { Instruction[] array3 = array2; foreach (Instruction key in array3) { if (!labelMap.ContainsKey(key)) { labelMap[key] = il.DefineLabel(); } } } else { object operand = current2.Operand; Instruction val = (Instruction)((operand is Instruction) ? operand : null); if (val != null && !labelMap.ContainsKey(val)) { labelMap[val] = il.DefineLabel(); } } } } finally { ((IDisposable)enumerator2).Dispose(); } int num = (((MethodReference)definition).HasThis ? 1 : 0); _ = new object[2]; bool flag = false; enumerator2 = definition.Body.Instructions.GetEnumerator(); try { while (enumerator2.MoveNext()) { Instruction current3 = enumerator2.Current; if (labelMap.TryGetValue(current3, out var value)) { il.MarkLabel(value); } Enumerator enumerator3 = definition.Body.ExceptionHandlers.GetEnumerator(); try { while (enumerator3.MoveNext()) { ExceptionHandler current4 = enumerator3.Current; if (flag && current4.HandlerEnd == current3) { il.EndExceptionBlock(); } ExceptionHandlerType handlerType; if (current4.TryStart == current3) { il.BeginExceptionBlock(); } else if (current4.FilterStart == current3) { il.BeginExceptFilterBlock(); } else if (current4.HandlerStart == current3) { handlerType = current4.HandlerType; switch ((int)handlerType) { case 1: il.BeginCatchBlock(null); break; case 0: il.BeginCatchBlock(current4.CatchType.ResolveReflection()); break; case 2: il.BeginFinallyBlock(); break; case 4: il.BeginFaultBlock(); break; } } if (current4.HandlerStart != current3.Next) { continue; } handlerType = current4.HandlerType; if ((int)handlerType != 1) { if ((int)handlerType != 2 || !(current3.OpCode == OpCodes.Endfinally)) { continue; } } else if (!(current3.OpCode == OpCodes.Endfilter)) { continue; } goto IL_0530; } } finally { ((IDisposable)enumerator3).Dispose(); } OpCode opCode = current3.OpCode; if ((int)((OpCode)(ref opCode)).OperandType == 5) { ILGenerator iLGenerator = il; Dictionary reflOpCodes = _ReflOpCodes; opCode = current3.OpCode; iLGenerator.Emit(reflOpCodes[((OpCode)(ref opCode)).Value]); } else { object obj = current3.Operand; if (obj is Instruction[] source) { obj = source.Select((Instruction target) => labelMap[target]).ToArray(); current3.OpCode = current3.OpCode.ToLongOp(); } else { Instruction val2 = (Instruction)((obj is Instruction) ? obj : null); if (val2 != null) { obj = labelMap[val2]; current3.OpCode = current3.OpCode.ToLongOp(); } else { VariableDefinition val3 = (VariableDefinition)((obj is VariableDefinition) ? obj : null); if (val3 != null) { obj = array[((VariableReference)val3).Index]; } else { ParameterDefinition val4 = (ParameterDefinition)((obj is ParameterDefinition) ? obj : null); if (val4 != null) { obj = ((ParameterReference)val4).Index + num; } else { MemberReference val5 = (MemberReference)((obj is MemberReference) ? obj : null); if (val5 != null) { obj = ((val5 == definition) ? _mb : val5.ResolveReflection()); } else { CallSite val6 = (CallSite)((obj is CallSite) ? obj : null); if (val6 != null) { if (dynamicMethod != null) { ILGenerator il2 = il; Dictionary reflOpCodes2 = _ReflOpCodes; opCode = current3.OpCode; _EmitCallSite(dynamicMethod, il2, reflOpCodes2[((OpCode)(ref opCode)).Value], val6); continue; } throw new NotSupportedException(); } } } } } } if (obj == null) { throw new NullReferenceException($"Unexpected null in {definition} @ {current3}"); } ILGenerator il3 = il; Dictionary reflOpCodes3 = _ReflOpCodes; opCode = current3.OpCode; il3.DynEmit(reflOpCodes3[((OpCode)(ref opCode)).Value], obj); } if (!flag) { enumerator3 = definition.Body.ExceptionHandlers.GetEnumerator(); try { while (enumerator3.MoveNext()) { if (enumerator3.Current.HandlerEnd == current3.Next) { il.EndExceptionBlock(); } } } finally { ((IDisposable)enumerator3).Dispose(); } } flag = false; continue; IL_0530: flag = true; } } finally { ((IDisposable)enumerator2).Dispose(); } } public static void ResolveWithModifiers(TypeReference typeRef, out Type type, out Type[] typeModReq, out Type[] typeModOpt, List modReq = null, List modOpt = null) { if (modReq == null) { modReq = new List(); } else { modReq.Clear(); } if (modOpt == null) { modOpt = new List(); } else { modOpt.Clear(); } TypeReference val = typeRef; while (true) { TypeSpecification val2 = (TypeSpecification)(object)((val is TypeSpecification) ? val : null); if (val2 == null) { break; } RequiredModifierType val3 = (RequiredModifierType)(object)((val is RequiredModifierType) ? val : null); if (val3 == null) { OptionalModifierType val4 = (OptionalModifierType)(object)((val is OptionalModifierType) ? val : null); if (val4 != null) { modOpt.Add(val4.ModifierType.ResolveReflection()); } } else { modReq.Add(val3.ModifierType.ResolveReflection()); } val = val2.ElementType; } type = typeRef.ResolveReflection(); typeModReq = modReq.ToArray(); typeModOpt = modOpt.ToArray(); } internal static void _EmitCallSite(DynamicMethod dm, ILGenerator il, OpCode opcode, CallSite csite) { //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_004e: Expected I4, but got Unknown //IL_0096: Unknown result type (might be due to invalid IL or missing references) //IL_009b: Unknown result type (might be due to invalid IL or missing references) List _tokens = null; _tokens = f_DynScope_m_tokens.GetValue(f_DynILGen_m_scope.GetValue(il)) as List; byte[] signature = new byte[32]; int currSig = 0; int num = -1; AddData((int)csite.CallingConvention); num = currSig++; List modReq = new List(); List modOpt = new List(); ResolveWithModifiers(csite.ReturnType, out var type, out var typeModReq, out var typeModOpt, modReq, modOpt); AddArgument(type, typeModReq, typeModOpt); Enumerator enumerator = csite.Parameters.GetEnumerator(); try { while (enumerator.MoveNext()) { ParameterDefinition current = enumerator.Current; if (((ParameterReference)current).ParameterType.IsSentinel) { AddElementType(65); } if (((ParameterReference)current).ParameterType.IsPinned) { AddElementType(69); } ResolveWithModifiers(((ParameterReference)current).ParameterType, out var type2, out var typeModReq2, out var typeModOpt2, modReq, modOpt); AddArgument(type2, typeModReq2, typeModOpt2); } } finally { ((IDisposable)enumerator).Dispose(); } AddElementType(0); int num2 = currSig; int num3 = ((csite.Parameters.Count < 128) ? 1 : ((csite.Parameters.Count >= 16384) ? 4 : 2)); byte[] array = new byte[currSig + num3 - 1]; array[0] = signature[0]; Buffer.BlockCopy(signature, num + 1, array, num + num3, num2 - (num + 1)); signature = array; currSig = num; AddData(csite.Parameters.Count); currSig = num2 + (num3 - 1); if (signature.Length > currSig) { array = new byte[currSig]; Array.Copy(signature, array, currSig); signature = array; } if (_ILGen_emit_int != null) { _ILGen_make_room.Invoke(il, new object[1] { 6 }); _ILGen_ll_emit.Invoke(il, new object[1] { opcode }); _ILGen_emit_int.Invoke(il, new object[1] { GetTokenForSig(signature) }); return; } _ILGen_EnsureCapacity.Invoke(il, new object[1] { 7 }); _ILGen_InternalEmit.Invoke(il, new object[1] { opcode }); if (opcode.StackBehaviourPop == StackBehaviour.Varpop) { _ILGen_UpdateStackSize.Invoke(il, new object[2] { opcode, -csite.Parameters.Count - 1 }); } _ILGen_PutInteger4.Invoke(il, new object[1] { GetTokenForSig(signature) }); void AddArgument(Type clsArgument, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers) { if (optionalCustomModifiers != null) { Type[] array3 = optionalCustomModifiers; for (int l = 0; l < array3.Length; l++) { InternalAddTypeToken(GetTokenForType(array3[l]), 32); } } if (requiredCustomModifiers != null) { Type[] array3 = requiredCustomModifiers; for (int l = 0; l < array3.Length; l++) { InternalAddTypeToken(GetTokenForType(array3[l]), 31); } } AddOneArgTypeHelper(clsArgument); } void AddData(int data) { if (currSig + 4 > signature.Length) { signature = ExpandArray(signature); } if (data <= 127) { signature[currSig++] = (byte)((uint)data & 0xFFu); } else if (data <= 16383) { signature[currSig++] = (byte)((uint)(data >> 8) | 0x80u); signature[currSig++] = (byte)((uint)data & 0xFFu); } else { if (data > 536870911) { throw new ArgumentException("Integer or token was too large to be encoded."); } signature[currSig++] = (byte)((uint)(data >> 24) | 0xC0u); signature[currSig++] = (byte)((uint)(data >> 16) & 0xFFu); signature[currSig++] = (byte)((uint)(data >> 8) & 0xFFu); signature[currSig++] = (byte)((uint)data & 0xFFu); } } void AddElementType(byte cvt) { if (currSig + 1 > signature.Length) { signature = ExpandArray(signature); } signature[currSig++] = cvt; } void AddOneArgTypeHelper(Type clsArgument) { AddOneArgTypeHelperWorker(clsArgument, lastWasGenericInst: false); } void AddOneArgTypeHelperWorker(Type clsArgument, bool lastWasGenericInst) { if (clsArgument.IsGenericType && (!clsArgument.IsGenericTypeDefinition || !lastWasGenericInst)) { AddElementType(21); AddOneArgTypeHelperWorker(clsArgument.GetGenericTypeDefinition(), lastWasGenericInst: true); Type[] genericArguments = clsArgument.GetGenericArguments(); AddData(genericArguments.Length); Type[] array2 = genericArguments; for (int i = 0; i < array2.Length; i++) { AddOneArgTypeHelper(array2[i]); } } else if (clsArgument.IsByRef) { AddElementType(16); clsArgument = clsArgument.GetElementType(); AddOneArgTypeHelper(clsArgument); } else if (clsArgument.IsPointer) { AddElementType(15); AddOneArgTypeHelper(clsArgument.GetElementType()); } else if (clsArgument.IsArray) { AddElementType(20); AddOneArgTypeHelper(clsArgument.GetElementType()); int arrayRank = clsArgument.GetArrayRank(); AddData(arrayRank); AddData(0); AddData(arrayRank); for (int j = 0; j < arrayRank; j++) { AddData(0); } } else { byte b = 0; for (int k = 0; k < CorElementTypes.Length; k++) { if (clsArgument == CorElementTypes[k]) { b = (byte)k; break; } } if (b == 0) { b = (byte)((clsArgument == typeof(object)) ? 28 : ((!clsArgument.IsValueType) ? 18 : 17)); } if (b <= 14 || b == 22 || b == 24 || b == 25 || b == 28) { AddElementType(b); } else if (clsArgument.IsValueType) { InternalAddTypeToken(GetTokenForType(clsArgument), 17); } else { InternalAddTypeToken(GetTokenForType(clsArgument), 18); } } } void AddToken(int token) { int num4 = token & 0xFFFFFF; int num5 = token & -16777216; if (num4 > 67108863) { throw new ArgumentException("Integer or token was too large to be encoded."); } num4 <<= 2; switch (num5) { case 16777216: num4 |= 1; break; case 452984832: num4 |= 2; break; } AddData(num4); } static byte[] ExpandArray(byte[] inArray, int requiredLength = -1) { if (requiredLength < inArray.Length) { requiredLength = inArray.Length * 2; } byte[] array4 = new byte[requiredLength]; Buffer.BlockCopy(inArray, 0, array4, 0, inArray.Length); return array4; } int GetTokenForSig(byte[] v) { return _GetTokenForSig(v); } int GetTokenForType(Type v) { return _GetTokenForType(v); } void InternalAddTypeToken(int clsToken, byte CorType) { AddElementType(CorType); AddToken(clsToken); } int _GetTokenForSig(byte[] v) { _tokens.Add(v); return (_tokens.Count - 1) | 0x11000000; } int _GetTokenForType(Type v) { _tokens.Add(v.TypeHandle); return (_tokens.Count - 1) | 0x2000000; } } } public sealed class DMDEmitDynamicMethodGenerator : DMDGenerator { private static readonly FieldInfo _DynamicMethod_returnType = typeof(DynamicMethod).GetField("returnType", BindingFlags.Instance | BindingFlags.NonPublic) ?? typeof(DynamicMethod).GetField("m_returnType", BindingFlags.Instance | BindingFlags.NonPublic); protected override MethodInfo _Generate(DynamicMethodDefinition dmd, object context) { MethodBase originalMethod = dmd.OriginalMethod; MethodDefinition definition = dmd.Definition; Type[] array; if (originalMethod != null) { ParameterInfo[] parameters = originalMethod.GetParameters(); int num = 0; if (!originalMethod.IsStatic) { num++; array = new Type[parameters.Length + 1]; array[0] = originalMethod.GetThisParamType(); } else { array = new Type[parameters.Length]; } for (int i = 0; i < parameters.Length; i++) { array[i + num] = parameters[i].ParameterType; } } else { int num2 = 0; if (((MethodReference)definition).HasThis) { num2++; array = new Type[((MethodReference)definition).Parameters.Count + 1]; Type type2 = ((TypeReference)(object)definition.DeclaringType).ResolveReflection(); if (type2.IsValueType) { type2 = type2.MakeByRefType(); } array[0] = type2; } else { array = new Type[((MethodReference)definition).Parameters.Count]; } for (int j = 0; j < ((MethodReference)definition).Parameters.Count; j++) { array[j + num2] = ((ParameterReference)((MethodReference)definition).Parameters[j]).ParameterType.ResolveReflection(); } } string text = dmd.Name ?? ("DMD<" + (originalMethod?.GetID(null, null, withType: true, proxyMethod: false, simple: true) ?? ((MethodReference)(object)definition).GetID(null, null, withType: true, simple: true)) + ">"); Type type3 = (originalMethod as MethodInfo)?.ReturnType ?? ((MethodReference)definition).ReturnType?.ResolveReflection(); MMDbgLog.Log(string.Format("new DynamicMethod: {0} {1}({2})", type3, text, string.Join(",", array.Select((Type type) => type?.ToString()).ToArray()))); if (originalMethod != null) { MMDbgLog.Log("orig: " + ((originalMethod as MethodInfo)?.ReturnType?.ToString() ?? "NULL") + " " + originalMethod.Name + "(" + string.Join(",", (from arg in originalMethod.GetParameters() select arg?.ParameterType?.ToString() ?? "NULL").ToArray()) + ")"); } MMDbgLog.Log("mdef: " + (((object)((MethodReference)definition).ReturnType)?.ToString() ?? "NULL") + " " + text + "(" + string.Join(",", ((IEnumerable)((MethodReference)definition).Parameters).Select((ParameterDefinition arg) => ((arg == null) ? null : ((object)((ParameterReference)arg).ParameterType)?.ToString()) ?? "NULL").ToArray()) + ")"); DynamicMethod dynamicMethod = new DynamicMethod(text, typeof(void), array, originalMethod?.DeclaringType ?? dmd.OwnerType ?? typeof(DynamicMethodDefinition), skipVisibility: true); _DynamicMethod_returnType.SetValue(dynamicMethod, type3); ILGenerator iLGenerator = dynamicMethod.GetILGenerator(); _DMDEmit.Generate(dmd, dynamicMethod, iLGenerator); return dynamicMethod; } } public sealed class DynamicMethodDefinition : IDisposable { private enum TokenResolutionMode { Any, Type, Method, Field } private static OpCode[] _CecilOpCodes1X; private static OpCode[] _CecilOpCodes2X; internal static readonly bool _IsNewMonoSRE; internal static readonly bool _IsOldMonoSRE; private static bool _PreferCecil; internal static readonly ConstructorInfo c_DebuggableAttribute; internal static readonly ConstructorInfo c_UnverifiableCodeAttribute; internal static readonly ConstructorInfo c_IgnoresAccessChecksToAttribute; internal static readonly Type t__IDMDGenerator; internal static readonly Dictionary _DMDGeneratorCache; private MethodDefinition _Definition; private ModuleDefinition _Module; public string Name; public Type OwnerType; public bool Debug; private Guid GUID = Guid.NewGuid(); private bool _IsDisposed; public static bool IsDynamicILAvailable => !_PreferCecil; [Obsolete("Use OriginalMethod instead.")] public MethodBase Method => OriginalMethod; public MethodBase OriginalMethod { get; private set; } public MethodDefinition Definition => _Definition; public ModuleDefinition Module => _Module; private static void _InitCopier() { //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_003f: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0048: Invalid comparison between Unknown and I4 //IL_007a: Unknown result type (might be due to invalid IL or missing references) //IL_007b: Unknown result type (might be due to invalid IL or missing references) //IL_0060: Unknown result type (might be due to invalid IL or missing references) //IL_0061: Unknown result type (might be due to invalid IL or missing references) _CecilOpCodes1X = (OpCode[])(object)new OpCode[225]; _CecilOpCodes2X = (OpCode[])(object)new OpCode[31]; FieldInfo[] fields = typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < fields.Length; i++) { OpCode val = (OpCode)fields[i].GetValue(null); if ((int)((OpCode)(ref val)).OpCodeType != 2) { if (((OpCode)(ref val)).Size == 1) { _CecilOpCodes1X[((OpCode)(ref val)).Value] = val; } else { _CecilOpCodes2X[((OpCode)(ref val)).Value & 0xFF] = val; } } } } private void _CopyMethodToDefinition() { //IL_0100: Unknown result type (might be due to invalid IL or missing references) //IL_010a: Expected O, but got Unknown //IL_00ec: Unknown result type (might be due to invalid IL or missing references) //IL_00f3: Expected O, but got Unknown //IL_0148: Unknown result type (might be due to invalid IL or missing references) //IL_0182: Unknown result type (might be due to invalid IL or missing references) //IL_0174: Unknown result type (might be due to invalid IL or missing references) //IL_0201: Unknown result type (might be due to invalid IL or missing references) //IL_0206: Unknown result type (might be due to invalid IL or missing references) //IL_0218: Unknown result type (might be due to invalid IL or missing references) //IL_021d: Unknown result type (might be due to invalid IL or missing references) //IL_0221: Unknown result type (might be due to invalid IL or missing references) //IL_0226: Unknown result type (might be due to invalid IL or missing references) //IL_0228: Unknown result type (might be due to invalid IL or missing references) //IL_022c: Unknown result type (might be due to invalid IL or missing references) //IL_0230: Invalid comparison between Unknown and I4 //IL_0232: Unknown result type (might be due to invalid IL or missing references) //IL_0236: Invalid comparison between Unknown and I4 //IL_02d6: Unknown result type (might be due to invalid IL or missing references) //IL_02dd: Expected O, but got Unknown //IL_0323: Unknown result type (might be due to invalid IL or missing references) //IL_0329: Invalid comparison between Unknown and I4 //IL_0374: Unknown result type (might be due to invalid IL or missing references) MethodBase originalMethod = OriginalMethod; Module moduleFrom = originalMethod.Module; MethodBody methodBody = originalMethod.GetMethodBody(); byte[] array = methodBody?.GetILAsByteArray(); if (array == null) { throw new NotSupportedException("Body-less method"); } MethodDefinition def = Definition; ModuleDefinition moduleTo = ((MemberReference)def).Module; MethodBody bodyTo = def.Body; bodyTo.GetILProcessor(); Type[] typeArguments = null; if (originalMethod.DeclaringType.IsGenericType) { typeArguments = originalMethod.DeclaringType.GetGenericArguments(); } Type[] methodArguments = null; if (originalMethod.IsGenericMethod) { methodArguments = originalMethod.GetGenericArguments(); } foreach (LocalVariableInfo localVariable in methodBody.LocalVariables) { TypeReference val = moduleTo.ImportReference(localVariable.LocalType); if (localVariable.IsPinned) { val = (TypeReference)new PinnedType(val); } bodyTo.Variables.Add(new VariableDefinition(val)); } using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(array))) { Instruction val2 = null; Instruction val3 = null; while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { int offset2 = (int)binaryReader.BaseStream.Position; val2 = Instruction.Create(OpCodes.Nop); byte b = binaryReader.ReadByte(); val2.OpCode = ((b != 254) ? _CecilOpCodes1X[b] : _CecilOpCodes2X[binaryReader.ReadByte()]); val2.Offset = offset2; if (val3 != null) { val3.Next = val2; } val2.Previous = val3; ReadOperand(binaryReader, val2); bodyTo.Instructions.Add(val2); val3 = val2; } } Enumerator enumerator2 = bodyTo.Instructions.GetEnumerator(); try { while (enumerator2.MoveNext()) { Instruction current2 = enumerator2.Current; OpCode opCode = current2.OpCode; OperandType operandType = ((OpCode)(ref opCode)).OperandType; if ((int)operandType != 0) { if ((int)operandType == 10) { int[] array2 = (int[])current2.Operand; Instruction[] array3 = (Instruction[])(object)new Instruction[array2.Length]; for (int i = 0; i < array2.Length; i++) { array3[i] = GetInstruction(array2[i]); } current2.Operand = array3; continue; } if ((int)operandType != 15) { continue; } } current2.Operand = GetInstruction((int)current2.Operand); } } finally { ((IDisposable)enumerator2).Dispose(); } foreach (ExceptionHandlingClause exceptionHandlingClause in methodBody.ExceptionHandlingClauses) { ExceptionHandler val4 = new ExceptionHandler((ExceptionHandlerType)exceptionHandlingClause.Flags); bodyTo.ExceptionHandlers.Add(val4); val4.TryStart = GetInstruction(exceptionHandlingClause.TryOffset); val4.TryEnd = GetInstruction(exceptionHandlingClause.TryOffset + exceptionHandlingClause.TryLength); val4.FilterStart = (((int)val4.HandlerType != 1) ? null : GetInstruction(exceptionHandlingClause.FilterOffset)); val4.HandlerStart = GetInstruction(exceptionHandlingClause.HandlerOffset); val4.HandlerEnd = GetInstruction(exceptionHandlingClause.HandlerOffset + exceptionHandlingClause.HandlerLength); val4.CatchType = (((int)val4.HandlerType != 0) ? null : ((exceptionHandlingClause.CatchType == null) ? null : moduleTo.ImportReference(exceptionHandlingClause.CatchType))); } Instruction GetInstruction(int offset) { int num = bodyTo.Instructions.Count - 1; if (offset < 0 || offset > bodyTo.Instructions[num].Offset) { return null; } int num2 = 0; int num3 = num; while (num2 <= num3) { int num4 = num2 + (num3 - num2) / 2; Instruction val5 = bodyTo.Instructions[num4]; if (offset == val5.Offset) { return val5; } if (offset < val5.Offset) { num3 = num4 - 1; } else { num2 = num4 + 1; } } return null; } void ReadOperand(BinaryReader reader, Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0068: Expected I4, but got Unknown //IL_027e: Unknown result type (might be due to invalid IL or missing references) //IL_0283: Unknown result type (might be due to invalid IL or missing references) //IL_0205: Unknown result type (might be due to invalid IL or missing references) //IL_020a: Unknown result type (might be due to invalid IL or missing references) //IL_020e: Unknown result type (might be due to invalid IL or missing references) //IL_0215: Invalid comparison between Unknown and I4 //IL_023f: Unknown result type (might be due to invalid IL or missing references) //IL_0244: Unknown result type (might be due to invalid IL or missing references) //IL_0248: Unknown result type (might be due to invalid IL or missing references) //IL_024f: Invalid comparison between Unknown and I4 //IL_00fc: Unknown result type (might be due to invalid IL or missing references) //IL_0101: Unknown result type (might be due to invalid IL or missing references) OpCode opCode2 = instr.OpCode; OperandType operandType2 = ((OpCode)(ref opCode2)).OperandType; switch ((int)operandType2) { case 5: instr.Operand = null; break; case 10: { int num6 = reader.ReadInt32(); int num7 = (int)reader.BaseStream.Position + 4 * num6; int[] array4 = new int[num6]; for (int j = 0; j < num6; j++) { array4[j] = reader.ReadInt32() + num7; } instr.Operand = array4; break; } case 15: { int num7 = reader.ReadSByte(); instr.Operand = (int)reader.BaseStream.Position + num7; break; } case 0: { int num7 = reader.ReadInt32(); instr.Operand = (int)reader.BaseStream.Position + num7; break; } case 16: instr.Operand = ((instr.OpCode == OpCodes.Ldc_I4_S) ? ((object)reader.ReadSByte()) : ((object)reader.ReadByte())); break; case 2: instr.Operand = reader.ReadInt32(); break; case 17: instr.Operand = reader.ReadSingle(); break; case 7: instr.Operand = reader.ReadDouble(); break; case 3: instr.Operand = reader.ReadInt64(); break; case 8: instr.Operand = moduleTo.ImportCallSite(moduleFrom, moduleFrom.ResolveSignature(reader.ReadInt32())); break; case 9: instr.Operand = moduleFrom.ResolveString(reader.ReadInt32()); break; case 11: instr.Operand = ResolveTokenAs(reader.ReadInt32(), TokenResolutionMode.Any); break; case 12: instr.Operand = ResolveTokenAs(reader.ReadInt32(), TokenResolutionMode.Type); break; case 4: instr.Operand = ResolveTokenAs(reader.ReadInt32(), TokenResolutionMode.Method); break; case 1: instr.Operand = ResolveTokenAs(reader.ReadInt32(), TokenResolutionMode.Field); break; case 13: case 18: { opCode2 = instr.OpCode; int num5 = (((int)((OpCode)(ref opCode2)).OperandType == 18) ? reader.ReadByte() : reader.ReadInt16()); instr.Operand = bodyTo.Variables[num5]; break; } case 14: case 19: { opCode2 = instr.OpCode; int num5 = (((int)((OpCode)(ref opCode2)).OperandType == 19) ? reader.ReadByte() : reader.ReadInt16()); instr.Operand = ((MethodReference)def).Parameters[num5]; break; } default: opCode2 = instr.OpCode; throw new NotSupportedException("Unsupported opcode $" + ((OpCode)(ref opCode2)).Name); } } MemberReference ResolveTokenAs(int token, TokenResolutionMode resolveMode) { //IL_01b3: Unknown result type (might be due to invalid IL or missing references) //IL_01b8: Unknown result type (might be due to invalid IL or missing references) //IL_01c4: Expected O, but got Unknown //IL_01e4: Unknown result type (might be due to invalid IL or missing references) //IL_01eb: Expected O, but got Unknown //IL_0205: Unknown result type (might be due to invalid IL or missing references) //IL_020b: Expected O, but got Unknown //IL_020f: Unknown result type (might be due to invalid IL or missing references) //IL_0215: Expected O, but got Unknown //IL_0219: Unknown result type (might be due to invalid IL or missing references) //IL_021f: Expected O, but got Unknown try { switch (resolveMode) { case TokenResolutionMode.Type: { Type type2 = moduleFrom.ResolveType(token, typeArguments, methodArguments); type2.FixReflectionCacheAuto(); return (MemberReference)(object)moduleTo.ImportReference(type2); } case TokenResolutionMode.Method: { MethodBase methodBase2 = moduleFrom.ResolveMethod(token, typeArguments, methodArguments); methodBase2.GetRealDeclaringType()?.FixReflectionCacheAuto(); return (MemberReference)(object)moduleTo.ImportReference(methodBase2); } case TokenResolutionMode.Field: { FieldInfo fieldInfo2 = moduleFrom.ResolveField(token, typeArguments, methodArguments); fieldInfo2.GetRealDeclaringType()?.FixReflectionCacheAuto(); return (MemberReference)(object)moduleTo.ImportReference(fieldInfo2); } case TokenResolutionMode.Any: { MemberInfo memberInfo = moduleFrom.ResolveMember(token, typeArguments, methodArguments); if (memberInfo is Type type) { type.FixReflectionCacheAuto(); return (MemberReference)(object)moduleTo.ImportReference(type); } if (memberInfo is MethodBase methodBase) { methodBase.GetRealDeclaringType()?.FixReflectionCacheAuto(); return (MemberReference)(object)moduleTo.ImportReference(methodBase); } if (!(memberInfo is FieldInfo fieldInfo)) { throw new NotSupportedException($"Invalid resolved member type {memberInfo.GetType()}"); } fieldInfo.GetRealDeclaringType()?.FixReflectionCacheAuto(); return (MemberReference)(object)moduleTo.ImportReference(fieldInfo); } default: throw new NotSupportedException($"Invalid TokenResolutionMode {resolveMode}"); } } catch (MissingMemberException) { string location = moduleFrom.Assembly.Location; if (!File.Exists(location)) { throw; } AssemblyDefinition val6 = AssemblyDefinition.ReadAssembly(location, new ReaderParameters { ReadingMode = (ReadingMode)2 }); try { MemberReference val7 = (MemberReference)((IEnumerable)val6.Modules).First((ModuleDefinition m) => ((ModuleReference)m).Name == moduleFrom.Name).LookupToken(token); return (MemberReference)(resolveMode switch { TokenResolutionMode.Type => (object)(TypeReference)val7, TokenResolutionMode.Method => (object)(MethodReference)val7, TokenResolutionMode.Field => (object)(FieldReference)val7, TokenResolutionMode.Any => val7, _ => throw new NotSupportedException($"Invalid TokenResolutionMode {resolveMode}"), }); } finally { ((IDisposable)val6)?.Dispose(); } } } } static DynamicMethodDefinition() { _IsNewMonoSRE = ReflectionHelper.IsMono && typeof(DynamicMethod).GetField("il_info", BindingFlags.Instance | BindingFlags.NonPublic) != null; _IsOldMonoSRE = ReflectionHelper.IsMono && !_IsNewMonoSRE && typeof(DynamicMethod).GetField("ilgen", BindingFlags.Instance | BindingFlags.NonPublic) != null; _PreferCecil = (ReflectionHelper.IsMono && !_IsNewMonoSRE && !_IsOldMonoSRE) || (!ReflectionHelper.IsMono && typeof(ILGenerator).Assembly.GetType("System.Reflection.Emit.DynamicILGenerator")?.GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic) == null); c_DebuggableAttribute = typeof(DebuggableAttribute).GetConstructor(new Type[1] { typeof(DebuggableAttribute.DebuggingModes) }); c_UnverifiableCodeAttribute = typeof(UnverifiableCodeAttribute).GetConstructor(new Type[0]); c_IgnoresAccessChecksToAttribute = typeof(IgnoresAccessChecksToAttribute).GetConstructor(new Type[1] { typeof(string) }); t__IDMDGenerator = typeof(_IDMDGenerator); _DMDGeneratorCache = new Dictionary(); _InitCopier(); } internal DynamicMethodDefinition() { Debug = Environment.GetEnvironmentVariable("MONOMOD_DMD_DEBUG") == "1"; } public DynamicMethodDefinition(MethodBase method) : this() { OriginalMethod = method ?? throw new ArgumentNullException("method"); Reload(); } public DynamicMethodDefinition(string name, Type returnType, Type[] parameterTypes) : this() { Name = name; OriginalMethod = null; _CreateDynModule(name, returnType, parameterTypes); } public ILProcessor GetILProcessor() { return Definition.Body.GetILProcessor(); } public ILGenerator GetILGenerator() { return new CecilILGenerator(Definition.Body.GetILProcessor()).GetProxy(); } private ModuleDefinition _CreateDynModule(string name, Type returnType, Type[] parameterTypes) { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Expected O, but got Unknown //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_005e: Expected O, but got Unknown //IL_008e: Unknown result type (might be due to invalid IL or missing references) //IL_0093: Unknown result type (might be due to invalid IL or missing references) //IL_0096: Expected O, but got Unknown //IL_009b: Expected O, but got Unknown //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_00c5: Expected O, but got Unknown ModuleDefinition val = (_Module = ModuleDefinition.CreateModule($"DMD:DynModule<{name}>?{GetHashCode()}", new ModuleParameters { Kind = (ModuleKind)0, ReflectionImporterProvider = MMReflectionImporter.ProviderNoDefault })); TypeDefinition val2 = new TypeDefinition("", $"DMD<{name}>?{GetHashCode()}", (TypeAttributes)1); val.Types.Add(val2); MethodDefinition val3 = new MethodDefinition(name, (MethodAttributes)150, (returnType != null) ? val.ImportReference(returnType) : val.TypeSystem.Void); MethodDefinition val4 = val3; _Definition = val3; MethodDefinition val5 = val4; foreach (Type type in parameterTypes) { ((MethodReference)val5).Parameters.Add(new ParameterDefinition(val.ImportReference(type))); } val2.Methods.Add(val5); return val; } public void Reload() { MethodBase originalMethod = OriginalMethod; if (originalMethod == null) { throw new InvalidOperationException(); } ModuleDefinition val = null; try { _Definition = null; ModuleDefinition module = _Module; if (module != null) { module.Dispose(); } _Module = null; ParameterInfo[] parameters = originalMethod.GetParameters(); int num = 0; Type[] array; if (!originalMethod.IsStatic) { num++; array = new Type[parameters.Length + 1]; array[0] = originalMethod.GetThisParamType(); } else { array = new Type[parameters.Length]; } for (int i = 0; i < parameters.Length; i++) { array[i + num] = parameters[i].ParameterType; } val = _CreateDynModule(originalMethod.GetID(null, null, withType: true, proxyMethod: false, simple: true), (originalMethod as MethodInfo)?.ReturnType, array); _CopyMethodToDefinition(); MethodDefinition definition = Definition; if (!originalMethod.IsStatic) { ((ParameterReference)((MethodReference)definition).Parameters[0]).Name = "this"; } for (int j = 0; j < parameters.Length; j++) { ((ParameterReference)((MethodReference)definition).Parameters[j + num]).Name = parameters[j].Name; } _Module = val; val = null; } catch { if (val != null) { val.Dispose(); } throw; } } public MethodInfo Generate() { return Generate(null); } public MethodInfo Generate(object context) { string environmentVariable = Environment.GetEnvironmentVariable("MONOMOD_DMD_TYPE"); switch (environmentVariable?.ToLower(CultureInfo.InvariantCulture)) { case "dynamicmethod": case "dm": return DMDGenerator.Generate(this, context); case "cecil": case "md": return DMDGenerator.Generate(this, context); default: { Type type = ReflectionHelper.GetType(environmentVariable); if (type != null) { if (!t__IDMDGenerator.IsCompatible(type)) { throw new ArgumentException("Invalid DMDGenerator type: " + environmentVariable); } if (!_DMDGeneratorCache.TryGetValue(environmentVariable, out var value)) { value = (_DMDGeneratorCache[environmentVariable] = Activator.CreateInstance(type) as _IDMDGenerator); } return value.Generate(this, context); } if (_PreferCecil) { return DMDGenerator.Generate(this, context); } if (Debug) { return DMDGenerator.Generate(this, context); } return DMDGenerator.Generate(this, context); } } } public void Dispose() { if (!_IsDisposed) { _IsDisposed = true; _Module.Dispose(); } } public string GetDumpName(string type) { return string.Format("DMDASM.{0:X8}{1}", GUID.GetHashCode(), string.IsNullOrEmpty(type) ? "" : ("." + type)); } } public static class DynamicMethodHelper { private static List References = new List(); private static readonly MethodInfo _GetMethodFromHandle = typeof(MethodBase).GetMethod("GetMethodFromHandle", new Type[1] { typeof(RuntimeMethodHandle) }); private static readonly MethodInfo _GetReference = typeof(DynamicMethodHelper).GetMethod("GetReference"); public static object GetReference(int id) { return References[id]; } public static void SetReference(int id, object obj) { References[id] = obj; } private static int AddReference(object obj) { lock (References) { References.Add(obj); return References.Count - 1; } } public static void FreeReference(int id) { References[id] = null; } public static DynamicMethod Stub(this DynamicMethod dm) { ILGenerator iLGenerator = dm.GetILGenerator(); for (int i = 0; i < 32; i++) { iLGenerator.Emit(OpCodes.Nop); } if (dm.ReturnType != typeof(void)) { iLGenerator.DeclareLocal(dm.ReturnType); iLGenerator.Emit(OpCodes.Ldloca_S, (sbyte)0); iLGenerator.Emit(OpCodes.Initobj, dm.ReturnType); iLGenerator.Emit(OpCodes.Ldloc_0); } iLGenerator.Emit(OpCodes.Ret); return dm; } public static DynamicMethodDefinition Stub(this DynamicMethodDefinition dmd) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_0061: Expected O, but got Unknown //IL_0062: Unknown result type (might be due to invalid IL or missing references) //IL_006e: Unknown result type (might be due to invalid IL or missing references) //IL_0084: Unknown result type (might be due to invalid IL or missing references) ILProcessor iLProcessor = dmd.GetILProcessor(); for (int i = 0; i < 32; i++) { iLProcessor.Emit(OpCodes.Nop); } if (((MethodReference)dmd.Definition).ReturnType != ((MemberReference)dmd.Definition).Module.TypeSystem.Void) { iLProcessor.Body.Variables.Add(new VariableDefinition(((MethodReference)dmd.Definition).ReturnType)); iLProcessor.Emit(OpCodes.Ldloca_S, (sbyte)0); iLProcessor.Emit(OpCodes.Initobj, ((MethodReference)dmd.Definition).ReturnType); iLProcessor.Emit(OpCodes.Ldloc_0); } iLProcessor.Emit(OpCodes.Ret); return dmd; } public static int EmitReference(this ILGenerator il, T obj) { Type typeFromHandle = typeof(T); int num = AddReference(obj); il.Emit(OpCodes.Ldc_I4, num); il.Emit(OpCodes.Call, _GetReference); if (typeFromHandle.IsValueType) { il.Emit(OpCodes.Unbox_Any, typeFromHandle); } return num; } public static int EmitReference(this ILProcessor il, T obj) { //IL_0029: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) ModuleDefinition module = ((MemberReference)il.Body.Method).Module; Type typeFromHandle = typeof(T); int num = AddReference(obj); il.Emit(OpCodes.Ldc_I4, num); il.Emit(OpCodes.Call, module.ImportReference((MethodBase)_GetReference)); if (typeFromHandle.IsValueType) { il.Emit(OpCodes.Unbox_Any, module.ImportReference(typeFromHandle)); } return num; } public static int EmitGetReference(this ILGenerator il, int id) { Type typeFromHandle = typeof(T); il.Emit(OpCodes.Ldc_I4, id); il.Emit(OpCodes.Call, _GetReference); if (typeFromHandle.IsValueType) { il.Emit(OpCodes.Unbox_Any, typeFromHandle); } return id; } public static int EmitGetReference(this ILProcessor il, int id) { //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) ModuleDefinition module = ((MemberReference)il.Body.Method).Module; Type typeFromHandle = typeof(T); il.Emit(OpCodes.Ldc_I4, id); il.Emit(OpCodes.Call, module.ImportReference((MethodBase)_GetReference)); if (typeFromHandle.IsValueType) { il.Emit(OpCodes.Unbox_Any, module.ImportReference(typeFromHandle)); } return id; } } public class DynamicMethodReference : MethodReference { public MethodInfo DynamicMethod; public DynamicMethodReference(ModuleDefinition module, MethodInfo dm) : base("", module.TypeSystem.Void) { DynamicMethod = dm; } } public static class DynDll { public static class DlopenFlags { public const int RTLD_LAZY = 1; public const int RTLD_NOW = 2; public const int RTLD_LOCAL = 0; public const int RTLD_GLOBAL = 256; } public static Dictionary> Mappings = new Dictionary>(); public static IntPtr OpenLibrary(string name, bool skipMapping = false, int? flags = null) { if (InternalTryOpenLibraryMapping(name, out var libraryPtr, skipMapping, flags)) { return libraryPtr; } return NativeLibrary.Load(name, Assembly.GetCallingAssembly(), flags.HasValue ? new DllImportSearchPath?((DllImportSearchPath)flags.Value) : null); } public static bool TryOpenLibrary(string name, out IntPtr libraryPtr, bool skipMapping = false, int? flags = null) { if (InternalTryOpenLibraryMapping(name, out libraryPtr, skipMapping, flags)) { return true; } return NativeLibrary.TryLoad(name, Assembly.GetCallingAssembly(), flags.HasValue ? new DllImportSearchPath?((DllImportSearchPath)flags.Value) : null, out libraryPtr); } private static bool InternalTryOpenLibraryMapping(string name, out IntPtr libraryPtr, bool skipMapping, int? flags) { if (name != null && !skipMapping && Mappings.TryGetValue(name, out var value)) { foreach (DynDllMapping item in value) { if (InternalTryOpenLibraryMapping(item.LibraryName, out libraryPtr, skipMapping: true, item.Flags)) { return true; } } libraryPtr = IntPtr.Zero; return true; } libraryPtr = IntPtr.Zero; return false; } public static bool CloseLibrary(IntPtr lib) { NativeLibrary.Free(lib); return true; } public static IntPtr GetFunction(this IntPtr libraryPtr, string name) { return NativeLibrary.GetExport(libraryPtr, name); } public static bool TryGetFunction(this IntPtr libraryPtr, string name, out IntPtr functionPtr) { return NativeLibrary.TryGetExport(libraryPtr, name, out functionPtr); } public static T AsDelegate(this IntPtr s) where T : class { return Marshal.GetDelegateForFunctionPointer(s, typeof(T)) as T; } public static void ResolveDynDllImports(this Type type, Dictionary> mappings = null) { InternalResolveDynDllImports(type, null, mappings); } public static void ResolveDynDllImports(object instance, Dictionary> mappings = null) { InternalResolveDynDllImports(instance.GetType(), instance, mappings); } private static void InternalResolveDynDllImports(Type type, object instance, Dictionary> mappings) { BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic; bindingFlags = ((instance != null) ? (bindingFlags | BindingFlags.Instance) : (bindingFlags | BindingFlags.Static)); FieldInfo[] fields = type.GetFields(bindingFlags); foreach (FieldInfo fieldInfo in fields) { bool flag = true; object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(DynDllImportAttribute), inherit: true); for (int j = 0; j < customAttributes.Length; j++) { DynDllImportAttribute dynDllImportAttribute = (DynDllImportAttribute)customAttributes[j]; flag = false; IntPtr libraryPtr = IntPtr.Zero; if (mappings != null && mappings.TryGetValue(dynDllImportAttribute.LibraryName, out var value)) { bool flag2 = false; foreach (DynDllMapping item in value) { if (TryOpenLibrary(item.LibraryName, out libraryPtr, skipMapping: true, item.Flags)) { flag2 = true; break; } } if (!flag2) { continue; } } else if (!TryOpenLibrary(dynDllImportAttribute.LibraryName, out libraryPtr)) { continue; } foreach (string item2 in dynDllImportAttribute.EntryPoints.Concat(new string[2] { fieldInfo.Name, fieldInfo.FieldType.Name })) { if (libraryPtr.TryGetFunction(item2, out var functionPtr)) { fieldInfo.SetValue(instance, Marshal.GetDelegateForFunctionPointer(functionPtr, fieldInfo.FieldType)); flag = true; break; } } if (flag) { break; } } if (!flag) { throw new EntryPointNotFoundException("No matching entry point found for " + fieldInfo.Name + " in " + fieldInfo.DeclaringType.FullName); } } } } [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] public class DynDllImportAttribute : Attribute { public string LibraryName { get; set; } public string[] EntryPoints { get; set; } public DynDllImportAttribute(string libraryName, params string[] entryPoints) { LibraryName = libraryName; EntryPoints = entryPoints; } } public sealed class DynDllMapping { public string LibraryName { get; set; } public int? Flags { get; set; } public DynDllMapping(string libraryName, int? flags = null) { LibraryName = libraryName ?? throw new ArgumentNullException("libraryName"); Flags = flags; } public static implicit operator DynDllMapping(string libraryName) { return new DynDllMapping(libraryName); } } public delegate IMetadataTokenProvider Relinker(IMetadataTokenProvider mtp, IGenericParameterProvider context); public static class GCListener { private sealed class CollectionDummy { ~CollectionDummy() { Unloading |= AppDomain.CurrentDomain.IsFinalizingForUnload() || Environment.HasShutdownStarted; if (!Unloading) { GC.ReRegisterForFinalize(this); } GCListener.OnCollect?.Invoke(); } } private static bool Unloading; public static event Action OnCollect; static GCListener() { new CollectionDummy(); Type type = typeof(Assembly).GetTypeInfo().Assembly.GetType("System.Runtime.Loader.AssemblyLoadContext"); if (type != null) { object target = type.GetMethod("GetLoadContext").Invoke(null, new object[1] { typeof(GCListener).Assembly }); EventInfo @event = type.GetEvent("Unloading"); @event.AddEventHandler(target, Delegate.CreateDelegate(@event.EventHandlerType, typeof(GCListener).GetMethod("UnloadingALC", BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(type))); } } private static void UnloadingALC(T alc) { Unloading = true; } } public interface ICallSiteGenerator { CallSite ToCallSite(ModuleDefinition module); } public sealed class MMReflectionImporter : IReflectionImporter { private class _Provider : IReflectionImporterProvider { public bool? UseDefault; public IReflectionImporter GetReflectionImporter(ModuleDefinition module) { MMReflectionImporter mMReflectionImporter = new MMReflectionImporter(module); if (UseDefault.HasValue) { mMReflectionImporter.UseDefault = UseDefault.Value; } return (IReflectionImporter)(object)mMReflectionImporter; } } private enum GenericImportKind { Open, Definition } public static readonly IReflectionImporterProvider Provider = (IReflectionImporterProvider)(object)new _Provider(); public static readonly IReflectionImporterProvider ProviderNoDefault = (IReflectionImporterProvider)(object)new _Provider { UseDefault = false }; private readonly ModuleDefinition Module; private readonly DefaultReflectionImporter Default; private readonly Dictionary CachedAsms = new Dictionary(); private readonly Dictionary CachedModuleTypes = new Dictionary(); private readonly Dictionary CachedTypes = new Dictionary(); private readonly Dictionary CachedFields = new Dictionary(); private readonly Dictionary CachedMethods = new Dictionary(); public bool UseDefault; private readonly Dictionary ElementTypes; public MMReflectionImporter(ModuleDefinition module) { //IL_0046: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Expected O, but got Unknown Module = module; Default = new DefaultReflectionImporter(module); ElementTypes = new Dictionary { { typeof(void), module.TypeSystem.Void }, { typeof(bool), module.TypeSystem.Boolean }, { typeof(char), module.TypeSystem.Char }, { typeof(sbyte), module.TypeSystem.SByte }, { typeof(byte), module.TypeSystem.Byte }, { typeof(short), module.TypeSystem.Int16 }, { typeof(ushort), module.TypeSystem.UInt16 }, { typeof(int), module.TypeSystem.Int32 }, { typeof(uint), module.TypeSystem.UInt32 }, { typeof(long), module.TypeSystem.Int64 }, { typeof(ulong), module.TypeSystem.UInt64 }, { typeof(float), module.TypeSystem.Single }, { typeof(double), module.TypeSystem.Double }, { typeof(string), module.TypeSystem.String }, { typeof(TypedReference), module.TypeSystem.TypedReference }, { typeof(IntPtr), module.TypeSystem.IntPtr }, { typeof(UIntPtr), module.TypeSystem.UIntPtr }, { typeof(object), module.TypeSystem.Object } }; } private bool TryGetCachedType(Type type, out TypeReference typeRef, GenericImportKind importKind) { if (importKind == GenericImportKind.Definition) { typeRef = null; return false; } return CachedTypes.TryGetValue(type, out typeRef); } private TypeReference SetCachedType(Type type, TypeReference typeRef, GenericImportKind importKind) { if (importKind == GenericImportKind.Definition) { return typeRef; } return CachedTypes[type] = typeRef; } [Obsolete("Please use the Assembly overload instead.")] public AssemblyNameReference ImportReference(AssemblyName asm) { return Default.ImportReference(asm); } public AssemblyNameReference ImportReference(Assembly asm) { if (CachedAsms.TryGetValue(asm, out var value)) { return value; } value = Default.ImportReference(asm.GetName()); value.ApplyRuntimeHash(asm); return CachedAsms[asm] = value; } public TypeReference ImportModuleType(Module module, IGenericParameterProvider context) { //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_003c: Expected O, but got Unknown //IL_0041: Expected O, but got Unknown if (CachedModuleTypes.TryGetValue(module, out var value)) { return value; } Dictionary cachedModuleTypes = CachedModuleTypes; TypeReference val = new TypeReference(string.Empty, "", Module, (IMetadataScope)(object)ImportReference(module.Assembly)); TypeReference result = val; cachedModuleTypes[module] = val; return result; } public TypeReference ImportReference(Type type, IGenericParameterProvider context) { return _ImportReference(type, context, (context == null) ? GenericImportKind.Definition : GenericImportKind.Open); } private bool _IsGenericInstance(Type type, GenericImportKind importKind) { if (!type.IsGenericType || type.IsGenericTypeDefinition) { if (type.IsGenericType && type.IsGenericTypeDefinition) { return importKind == GenericImportKind.Open; } return false; } return true; } private GenericInstanceType _ImportGenericInstance(Type type, IGenericParameterProvider context, TypeReference typeRef) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Expected O, but got Unknown GenericInstanceType val = new GenericInstanceType(typeRef); Type[] genericArguments = type.GetGenericArguments(); foreach (Type type2 in genericArguments) { val.GenericArguments.Add(_ImportReference(type2, context)); } return val; } private TypeReference _ImportReference(Type type, IGenericParameterProvider context, GenericImportKind importKind = GenericImportKind.Open) { //IL_0063: Unknown result type (might be due to invalid IL or missing references) //IL_006e: Expected O, but got Unknown //IL_0087: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Expected O, but got Unknown //IL_0182: Unknown result type (might be due to invalid IL or missing references) //IL_0188: Expected O, but got Unknown //IL_00af: Unknown result type (might be due to invalid IL or missing references) //IL_00b5: Expected O, but got Unknown //IL_00e2: Unknown result type (might be due to invalid IL or missing references) //IL_01e4: Unknown result type (might be due to invalid IL or missing references) //IL_01ee: Expected O, but got Unknown if (TryGetCachedType(type, out var typeRef, importKind)) { if (!_IsGenericInstance(type, importKind)) { return typeRef; } return (TypeReference)(object)_ImportGenericInstance(type, context, typeRef); } if (UseDefault) { return SetCachedType(type, Default.ImportReference(type, context), importKind); } if (type.HasElementType) { if (type.IsByRef) { return SetCachedType(type, (TypeReference)new ByReferenceType(_ImportReference(type.GetElementType(), context)), importKind); } if (type.IsPointer) { return SetCachedType(type, (TypeReference)new PointerType(_ImportReference(type.GetElementType(), context)), importKind); } if (type.IsArray) { ArrayType val = new ArrayType(_ImportReference(type.GetElementType(), context), type.GetArrayRank()); if (type != type.GetElementType().MakeArrayType()) { for (int i = 0; i < val.Rank; i++) { val.Dimensions[i] = new ArrayDimension((int?)0, (int?)null); } } return CachedTypes[type] = (TypeReference)(object)val; } } if (_IsGenericInstance(type, importKind)) { return (TypeReference)(object)_ImportGenericInstance(type, context, _ImportReference(type.GetGenericTypeDefinition(), context, GenericImportKind.Definition)); } if (type.IsGenericParameter) { return SetCachedType(type, ImportGenericParameter(type, context), importKind); } if (ElementTypes.TryGetValue(type, out typeRef)) { return SetCachedType(type, typeRef, importKind); } typeRef = new TypeReference(string.Empty, type.Name, Module, (IMetadataScope)(object)ImportReference(type.Assembly), type.IsValueType); if (type.IsNested) { ((MemberReference)typeRef).DeclaringType = _ImportReference(type.DeclaringType, context, importKind); } else if (type.Namespace != null) { typeRef.Namespace = type.Namespace; } if (type.IsGenericType) { Type[] genericArguments = type.GetGenericArguments(); foreach (Type type2 in genericArguments) { typeRef.GenericParameters.Add(new GenericParameter(type2.Name, (IGenericParameterProvider)(object)typeRef)); } } return SetCachedType(type, typeRef, importKind); } private static TypeReference ImportGenericParameter(Type type, IGenericParameterProvider context) { MethodReference val = (MethodReference)(object)((context is MethodReference) ? context : null); if (val != null) { if (type.DeclaringMethod != null) { return (TypeReference)(object)val.GenericParameters[type.GenericParameterPosition]; } context = (IGenericParameterProvider)(object)((MemberReference)val).DeclaringType; } Type declaringType = type.DeclaringType; if (declaringType == null) { throw new InvalidOperationException(); } TypeReference val2 = (TypeReference)(object)((context is TypeReference) ? context : null); if (val2 != null) { while (val2 != null) { TypeReference elementType = val2.GetElementType(); if (((MemberReference)(object)elementType).Is(declaringType)) { return (TypeReference)(object)elementType.GenericParameters[type.GenericParameterPosition]; } if (((MemberReference)(object)val2).Is(declaringType)) { return (TypeReference)(object)val2.GenericParameters[type.GenericParameterPosition]; } val2 = ((MemberReference)val2).DeclaringType; } } throw new NotSupportedException(); } public FieldReference ImportReference(FieldInfo field, IGenericParameterProvider context) { //IL_00a3: Unknown result type (might be due to invalid IL or missing references) //IL_00a8: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: Expected O, but got Unknown //IL_00b0: Expected O, but got Unknown if (CachedFields.TryGetValue(field, out var value)) { return value; } if (UseDefault) { return CachedFields[field] = Default.ImportReference(field, context); } Type declaringType = field.DeclaringType; TypeReference val2 = ((declaringType != null) ? ImportReference(declaringType, context) : ImportModuleType(field.Module, context)); FieldInfo key = field; if (declaringType != null && declaringType.IsGenericType) { field = field.Module.ResolveField(field.MetadataToken); } Dictionary cachedFields = CachedFields; FieldReference val3 = new FieldReference(field.Name, _ImportReference(field.FieldType, (IGenericParameterProvider)(object)val2), val2); FieldReference result = val3; cachedFields[key] = val3; return result; } public MethodReference ImportReference(MethodBase method, IGenericParameterProvider context) { return _ImportReference(method, context, (context == null) ? GenericImportKind.Definition : GenericImportKind.Open); } private MethodReference _ImportReference(MethodBase method, IGenericParameterProvider context, GenericImportKind importKind) { //IL_0091: Unknown result type (might be due to invalid IL or missing references) //IL_0098: Expected O, but got Unknown //IL_0124: Unknown result type (might be due to invalid IL or missing references) //IL_012a: Expected O, but got Unknown //IL_01af: Unknown result type (might be due to invalid IL or missing references) //IL_01b9: Expected O, but got Unknown //IL_022d: Unknown result type (might be due to invalid IL or missing references) //IL_0237: Expected O, but got Unknown if (CachedMethods.TryGetValue(method, out var value) && importKind == GenericImportKind.Open) { return value; } if (method is MethodInfo methodInfo && methodInfo.IsDynamicMethod()) { return (MethodReference)(object)new DynamicMethodReference(Module, methodInfo); } if (UseDefault) { return CachedMethods[method] = Default.ImportReference(method, context); } if ((method.IsGenericMethod && !method.IsGenericMethodDefinition) || (method.IsGenericMethod && method.IsGenericMethodDefinition && importKind == GenericImportKind.Open)) { GenericInstanceMethod val2 = new GenericInstanceMethod(_ImportReference((method as MethodInfo).GetGenericMethodDefinition(), context, GenericImportKind.Definition)); Type[] genericArguments = method.GetGenericArguments(); foreach (Type type in genericArguments) { val2.GenericArguments.Add(_ImportReference(type, context)); } return CachedMethods[method] = (MethodReference)(object)val2; } Type declaringType = method.DeclaringType; value = new MethodReference(method.Name, _ImportReference(typeof(void), context), (declaringType != null) ? _ImportReference(declaringType, context, GenericImportKind.Definition) : ImportModuleType(method.Module, context)); value.HasThis = (method.CallingConvention & CallingConventions.HasThis) != 0; value.ExplicitThis = (method.CallingConvention & CallingConventions.ExplicitThis) != 0; if ((method.CallingConvention & CallingConventions.VarArgs) != 0) { value.CallingConvention = (MethodCallingConvention)5; } MethodBase key = method; if (declaringType != null && declaringType.IsGenericType) { method = method.Module.ResolveMethod(method.MetadataToken); } if (method.IsGenericMethodDefinition) { Type[] genericArguments = method.GetGenericArguments(); foreach (Type type2 in genericArguments) { value.GenericParameters.Add(new GenericParameter(type2.Name, (IGenericParameterProvider)(object)value)); } } value.ReturnType = _ImportReference((method as MethodInfo)?.ReturnType ?? typeof(void), (IGenericParameterProvider)(object)value); ParameterInfo[] parameters = method.GetParameters(); foreach (ParameterInfo parameterInfo in parameters) { value.Parameters.Add(new ParameterDefinition(parameterInfo.Name, (ParameterAttributes)(ushort)parameterInfo.Attributes, _ImportReference(parameterInfo.ParameterType, (IGenericParameterProvider)(object)value))); } return CachedMethods[key] = value; } } [Flags] public enum Platform { OS = 1, Bits64 = 2, NT = 4, Unix = 8, ARM = 0x10000, Wine = 0x20000, Unknown = 0x11, Windows = 0x25, MacOS = 0x49, Linux = 0x89, Android = 0x189, iOS = 0x249 } public static class PlatformHelper { private static Platform _current = Platform.Unknown; private static bool _currentLocked = false; private static string _librarySuffix; public static Platform Current { get { if (!_currentLocked) { if (_current == Platform.Unknown) { DeterminePlatform(); } _currentLocked = true; } return _current; } set { if (_currentLocked) { throw new InvalidOperationException("Cannot set the value of PlatformHelper.Current once it has been accessed."); } _current = value; } } public static string LibrarySuffix { get { if (_librarySuffix == null) { _librarySuffix = (Is(Platform.MacOS) ? "dylib" : (Is(Platform.Unix) ? "so" : "dll")); } return _librarySuffix; } } private static void DeterminePlatform() { _current = Platform.Unknown; if (OperatingSystem.IsWindows()) { _current = Platform.Windows; } else if (OperatingSystem.IsAndroid()) { _current = Platform.Android; } else if (OperatingSystem.IsLinux()) { _current = Platform.Linux; } else if (OperatingSystem.IsIOS() || OperatingSystem.IsTvOS()) { _current = Platform.iOS; } else if (OperatingSystem.IsMacOS()) { _current = Platform.MacOS; } else if (OperatingSystem.IsFreeBSD() || File.Exists("/proc/sys/kernel/ostype")) { _current = Platform.Unix; } if (_current != Platform.Unknown) { if (Is(Platform.Linux) && Directory.Exists("/data") && File.Exists("/system/build.prop")) { _current = Platform.Android; } else if (Is(Platform.Unix) && Directory.Exists("/Applications") && Directory.Exists("/System") && Directory.Exists("/User") && !Directory.Exists("/Users")) { _current = Platform.iOS; } else if (Is(Platform.Windows) && CheckWine()) { _current |= Platform.Wine; } } MethodInfo methodInfo = typeof(Environment).GetProperty("Is64BitOperatingSystem")?.GetGetMethod(); if (methodInfo != null) { _current |= (Platform)(((bool)methodInfo.Invoke(null, new object[0])) ? 2 : 0); } else { _current |= (Platform)((IntPtr.Size >= 8) ? 2 : 0); } if (RuntimeInformation.ProcessArchitecture.HasFlag(Architecture.Arm) || RuntimeInformation.OSArchitecture.HasFlag(Architecture.Arm)) { _current |= Platform.ARM; } } public static bool Is(Platform platform) { return (Current & platform) == platform; } private static bool CheckWine() { string environmentVariable = Environment.GetEnvironmentVariable("MONOMOD_WINE"); if (environmentVariable == "1") { return true; } if (environmentVariable == "0") { return false; } environmentVariable = Environment.GetEnvironmentVariable("XL_WINEONLINUX")?.ToLower(CultureInfo.InvariantCulture); if (environmentVariable == "true") { return true; } if (environmentVariable == "false") { return false; } IntPtr moduleHandle = GetModuleHandle("ntdll.dll"); if (moduleHandle != IntPtr.Zero && GetProcAddress(moduleHandle, "wine_get_version") != IntPtr.Zero) { return true; } return false; } [DllImport("kernel32", SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] private static extern IntPtr GetProcAddress(IntPtr hModule, string procName); } public static class ReflectionHelper { private class CacheFixEntry { public object Cache; public Array Properties; public Array Fields; public bool NeedsVerify; } public static readonly bool IsMono; public static readonly bool IsCore; private static readonly object[] _NoArgs; internal static readonly Dictionary AssemblyCache; internal static readonly Dictionary AssembliesCache; internal static readonly Dictionary ResolveReflectionCache; public static readonly byte[] AssemblyHashPrefix; public static readonly string AssemblyHashNameTag; private const BindingFlags _BindingFlagsAll = (BindingFlags)(-1); private static readonly object[] _CacheGetterArgs; private static Type t_RuntimeType; private static Type t_RuntimeTypeCache; private static PropertyInfo p_RuntimeType_Cache; private static MethodInfo m_RuntimeTypeCache_GetFieldList; private static MethodInfo m_RuntimeTypeCache_GetPropertyList; private static readonly ConditionalWeakTable _CacheFixed; private static Type t_RuntimeModule; private static PropertyInfo p_RuntimeModule_RuntimeType; private static FieldInfo f_RuntimeModule__impl; private static MethodInfo m_RuntimeModule_GetGlobalType; private static readonly FieldInfo f_SignatureHelper_module; private static MemberInfo _Cache(string cacheKey, MemberInfo value) { if (cacheKey != null && value == null) { MMDbgLog.Log("ResolveRefl failure: " + cacheKey); } if (cacheKey != null && value != null) { lock (ResolveReflectionCache) { ResolveReflectionCache[cacheKey] = new WeakReference(value); } } return value; } public static Assembly Load(ModuleDefinition module) { using MemoryStream memoryStream = new MemoryStream(); module.Write((Stream)memoryStream); memoryStream.Seek(0L, SeekOrigin.Begin); return Load((Stream)memoryStream); } public static Assembly Load(Stream stream) { Assembly asm; if (stream is MemoryStream memoryStream) { asm = Assembly.Load(memoryStream.GetBuffer()); } else { using MemoryStream memoryStream2 = new MemoryStream(); stream.CopyTo(memoryStream2); memoryStream2.Seek(0L, SeekOrigin.Begin); asm = Assembly.Load(memoryStream2.GetBuffer()); } AppDomain.CurrentDomain.AssemblyResolve += (object? s, ResolveEventArgs e) => (!(e.Name == asm.FullName)) ? null : asm; return asm; } public static Type GetType(string name) { if (string.IsNullOrEmpty(name)) { return null; } Type type = Type.GetType(name); if (type != null) { return type; } Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); for (int i = 0; i < assemblies.Length; i++) { type = assemblies[i].GetType(name); if (type != null) { return type; } } return null; } public static void ApplyRuntimeHash(this AssemblyNameReference asmRef, Assembly asm) { byte[] array = new byte[AssemblyHashPrefix.Length + 4]; Array.Copy(AssemblyHashPrefix, 0, array, 0, AssemblyHashPrefix.Length); Array.Copy(BitConverter.GetBytes(asm.GetHashCode()), 0, array, AssemblyHashPrefix.Length, 4); asmRef.HashAlgorithm = (AssemblyHashAlgorithm)(-1); asmRef.Hash = array; } public static string GetRuntimeHashedFullName(this Assembly asm) { return $"{asm.FullName}{AssemblyHashNameTag}{asm.GetHashCode()}"; } public static string GetRuntimeHashedFullName(this AssemblyNameReference asm) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Invalid comparison between Unknown and I4 if ((int)asm.HashAlgorithm != -1) { return asm.FullName; } byte[] hash = asm.Hash; if (hash.Length != AssemblyHashPrefix.Length + 4) { return asm.FullName; } for (int i = 0; i < AssemblyHashPrefix.Length; i++) { if (hash[i] != AssemblyHashPrefix[i]) { return asm.FullName; } } return $"{asm.FullName}{AssemblyHashNameTag}{BitConverter.ToInt32(hash, AssemblyHashPrefix.Length)}"; } public static Type ResolveReflection(this TypeReference mref) { return _ResolveReflection((MemberReference)(object)mref, null) as Type; } public static MethodBase ResolveReflection(this MethodReference mref) { return _ResolveReflection((MemberReference)(object)mref, null) as MethodBase; } public static FieldInfo ResolveReflection(this FieldReference mref) { return _ResolveReflection((MemberReference)(object)mref, null) as FieldInfo; } public static PropertyInfo ResolveReflection(this PropertyReference mref) { return _ResolveReflection((MemberReference)(object)mref, null) as PropertyInfo; } public static EventInfo ResolveReflection(this EventReference mref) { return _ResolveReflection((MemberReference)(object)mref, null) as EventInfo; } public static MemberInfo ResolveReflection(this MemberReference mref) { return _ResolveReflection(mref, null); } private static MemberInfo _ResolveReflection(MemberReference mref, Module[] modules) { if (mref == null) { return null; } if (mref is DynamicMethodReference dynamicMethodReference) { return dynamicMethodReference.DynamicMethod; } MemberReference obj = mref; string text = ((MethodReference)(object)((obj is MethodReference) ? obj : null))?.GetID() ?? mref.FullName; TypeReference val = (TypeReference)(((object)mref.DeclaringType) ?? ((object)(((mref is TypeReference) ? mref : null) ?? null))); IMetadataScope val2 = ((val != null) ? val.Scope : null); AssemblyNameReference val3 = (AssemblyNameReference)(object)((val2 is AssemblyNameReference) ? val2 : null); string asmName; string moduleName; if (val3 == null) { ModuleDefinition val4 = (ModuleDefinition)(object)((val2 is ModuleDefinition) ? val2 : null); if (val4 == null) { if (!(val2 is ModuleReference)) { if (val2 != null) { } asmName = null; moduleName = null; } else { asmName = ((AssemblyNameReference)(object)((MemberReference)val).Module.Assembly.Name).GetRuntimeHashedFullName(); moduleName = ((ModuleReference)((MemberReference)val).Module).Name; } } else { asmName = ((AssemblyNameReference)(object)val4.Assembly.Name).GetRuntimeHashedFullName(); moduleName = ((ModuleReference)val4).Name; } } else { asmName = val3.GetRuntimeHashedFullName(); moduleName = null; } text = text + " | " + (asmName ?? "NOASSEMBLY") + ", " + (moduleName ?? "NOMODULE"); lock (ResolveReflectionCache) { if (ResolveReflectionCache.TryGetValue(text, out var value) && value != null && value.SafeGetTarget() is MemberInfo result) { return result; } } if (mref is GenericParameter) { throw new NotSupportedException("ResolveReflection on GenericParameter currently not supported"); } MemberReference obj2 = mref; MethodReference val5 = (MethodReference)(object)((obj2 is MethodReference) ? obj2 : null); if (val5 != null && mref.DeclaringType is ArrayType) { Type type = _ResolveReflection((MemberReference)(object)mref.DeclaringType, modules) as Type; string methodID = val5.GetID(null, null, withType: false); MethodBase methodBase = type.GetMethods((BindingFlags)(-1)).Cast().Concat(type.GetConstructors((BindingFlags)(-1))) .FirstOrDefault((MethodBase m) => m.GetID(null, null, withType: false) == methodID); if (methodBase != null) { return _Cache(text, methodBase); } } if (val == null) { throw new ArgumentException("MemberReference hasn't got a DeclaringType / isn't a TypeReference in itself"); } if (asmName == null && moduleName == null) { throw new NotSupportedException("Unsupported scope type " + ((object)val.Scope).GetType().FullName); } bool flag = true; bool flag2 = false; bool flag3 = false; MemberInfo memberInfo; while (true) { if (flag3) { modules = null; } flag3 = true; if (modules == null) { Assembly[] array = null; if (flag && flag2) { flag2 = false; flag = false; } if (flag) { lock (AssemblyCache) { if (AssemblyCache.TryGetValue(asmName, out var value2) && value2.SafeGetTarget() is Assembly assembly) { array = new Assembly[1] { assembly }; } } } if (array == null && !flag2) { lock (AssembliesCache) { if (AssembliesCache.TryGetValue(asmName, out var value3)) { array = (from asmRef in value3 select asmRef.SafeGetTarget() as Assembly into asm where asm != null select asm).ToArray(); } } } if (array == null) { int num = asmName.IndexOf(AssemblyHashNameTag, StringComparison.Ordinal); if (num != -1 && int.TryParse(asmName.Substring(num + 2), out var hash)) { array = (from other in AppDomain.CurrentDomain.GetAssemblies() where other.GetHashCode() == hash select other).ToArray(); if (array.Length == 0) { array = null; } asmName = asmName.Substring(0, num); } if (array == null) { array = (from other in AppDomain.CurrentDomain.GetAssemblies() where other.GetName().FullName == asmName select other).ToArray(); if (array.Length == 0) { array = (from other in AppDomain.CurrentDomain.GetAssemblies() where other.GetName().Name == asmName select other).ToArray(); } if (array.Length == 0) { Assembly assembly2 = Assembly.Load(new AssemblyName(asmName)); if ((object)assembly2 != null) { array = new Assembly[1] { assembly2 }; } } } if (array.Length != 0) { lock (AssembliesCache) { AssembliesCache[asmName] = array.Select((Assembly asm) => new WeakReference(asm)).ToArray(); } } } modules = (string.IsNullOrEmpty(moduleName) ? array.SelectMany((Assembly asm) => asm.GetModules()) : array.Select((Assembly asm) => asm.GetModule(moduleName))).Where((Module mod) => mod != null).ToArray(); if (modules.Length == 0) { throw new Exception("Cannot resolve assembly / module " + asmName + " / " + moduleName); } } MemberReference obj3 = mref; TypeReference val6 = (TypeReference)(object)((obj3 is TypeReference) ? obj3 : null); if (val6 != null) { if (((MemberReference)val6).FullName == "") { throw new ArgumentException("Type cannot be resolved to a runtime reflection type"); } MemberReference obj4 = mref; TypeSpecification val7 = (TypeSpecification)(object)((obj4 is TypeSpecification) ? obj4 : null); Type type; if (val7 != null) { type = _ResolveReflection((MemberReference)(object)val7.ElementType, null) as Type; if (type == null) { return null; } if (((TypeReference)val7).IsByReference) { return _Cache(text, type.MakeByRefType()); } if (((TypeReference)val7).IsPointer) { return _Cache(text, type.MakePointerType()); } if (((TypeReference)val7).IsArray) { return _Cache(text, ((ArrayType)((val7 is ArrayType) ? val7 : null)).IsVector ? type.MakeArrayType() : type.MakeArrayType(((ArrayType)((val7 is ArrayType) ? val7 : null)).Dimensions.Count)); } if (((TypeReference)val7).IsGenericInstance) { return _Cache(text, type.MakeGenericType(((IEnumerable)((GenericInstanceType)((val7 is GenericInstanceType) ? val7 : null)).GenericArguments).Select((TypeReference arg) => _ResolveReflection((MemberReference)(object)arg, null) as Type).ToArray())); } } else { type = modules.Select((Module module) => module.GetType(mref.FullName.Replace("/", "+", StringComparison.Ordinal), throwOnError: false, ignoreCase: false)).FirstOrDefault((Type m) => m != null); if (type == null) { type = modules.Select((Module module) => module.GetTypes().FirstOrDefault((Type m) => mref.Is(m))).FirstOrDefault((Type m) => m != null); } if (type == null && !flag2) { goto IL_02b3; } } return _Cache(text, type); } bool flag4 = ((MemberReference)mref.DeclaringType).FullName == ""; MemberReference obj5 = mref; GenericInstanceMethod val8 = (GenericInstanceMethod)(object)((obj5 is GenericInstanceMethod) ? obj5 : null); if (val8 != null) { memberInfo = _ResolveReflection((MemberReference)(object)((MethodSpecification)val8).ElementMethod, modules); memberInfo = (memberInfo as MethodInfo)?.MakeGenericMethod(((IEnumerable)val8.GenericArguments).Select((TypeReference arg) => _ResolveReflection((MemberReference)(object)arg, null) as Type).ToArray()); } else if (flag4) { if (mref is MethodReference) { memberInfo = modules.Select((Module module) => module.GetMethods((BindingFlags)(-1)).FirstOrDefault((MethodInfo m) => mref.Is(m))).FirstOrDefault((MethodInfo m) => m != null); } else { if (!(mref is FieldReference)) { throw new NotSupportedException("Unsupported member type " + ((object)mref).GetType().FullName); } memberInfo = modules.Select((Module module) => module.GetFields((BindingFlags)(-1)).FirstOrDefault((FieldInfo m) => mref.Is(m))).FirstOrDefault((FieldInfo m) => m != null); } } else { Type type2 = _ResolveReflection((MemberReference)(object)mref.DeclaringType, modules) as Type; memberInfo = ((mref is MethodReference) ? type2.GetMethods((BindingFlags)(-1)).Cast().Concat(type2.GetConstructors((BindingFlags)(-1))) .FirstOrDefault((MethodBase m) => mref.Is(m)) : ((!(mref is FieldReference)) ? type2.GetMembers((BindingFlags)(-1)).FirstOrDefault((MemberInfo m) => mref.Is(m)) : type2.GetFields((BindingFlags)(-1)).FirstOrDefault((FieldInfo m) => mref.Is(m)))); } if (!(memberInfo == null) || flag2) { break; } goto IL_02b3; IL_02b3: flag2 = true; } return _Cache(text, memberInfo); } public static SignatureHelper ResolveReflection(this CallSite csite, Module context) { return ((IMethodSignature)(object)csite).ResolveReflectionSignature(context); } public static SignatureHelper ResolveReflectionSignature(this IMethodSignature csite, Module context) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Invalid comparison between Unknown and I4 //IL_0165: Unknown result type (might be due to invalid IL or missing references) //IL_016a: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Unknown result type (might be due to invalid IL or missing references) MethodCallingConvention callingConvention = csite.CallingConvention; if (callingConvention - 1 <= 4) { throw new NotSupportedException("Unmanaged calling conventions for callsites not supported on .NET Standard"); } SignatureHelper signatureHelper = ((!csite.ExplicitThis) ? SignatureHelper.GetMethodSigHelper(context, CallingConventions.Standard, csite.ReturnType.ResolveReflection()) : SignatureHelper.GetMethodSigHelper(context, CallingConventions.ExplicitThis, csite.ReturnType.ResolveReflection())); if (context != null) { List list = new List(); List list2 = new List(); Enumerator enumerator = csite.Parameters.GetEnumerator(); try { while (enumerator.MoveNext()) { ParameterDefinition current = enumerator.Current; if (((ParameterReference)current).ParameterType.IsSentinel) { signatureHelper.AddSentinel(); } if (((ParameterReference)current).ParameterType.IsPinned) { signatureHelper.AddArgument(((ParameterReference)current).ParameterType.ResolveReflection(), pinned: true); continue; } list2.Clear(); list.Clear(); TypeReference val = ((ParameterReference)current).ParameterType; while (true) { TypeSpecification val2 = (TypeSpecification)(object)((val is TypeSpecification) ? val : null); if (val2 == null) { break; } RequiredModifierType val3 = (RequiredModifierType)(object)((val is RequiredModifierType) ? val : null); if (val3 == null) { OptionalModifierType val4 = (OptionalModifierType)(object)((val is OptionalModifierType) ? val : null); if (val4 != null) { list2.Add(val4.ModifierType.ResolveReflection()); } } else { list.Add(val3.ModifierType.ResolveReflection()); } val = val2.ElementType; } signatureHelper.AddArgument(((ParameterReference)current).ParameterType.ResolveReflection(), list.ToArray(), list2.ToArray()); } } finally { ((IDisposable)enumerator).Dispose(); } } else { Enumerator enumerator = csite.Parameters.GetEnumerator(); try { while (enumerator.MoveNext()) { ParameterDefinition current2 = enumerator.Current; signatureHelper.AddArgument(((ParameterReference)current2).ParameterType.ResolveReflection()); } } finally { ((IDisposable)enumerator).Dispose(); } } return signatureHelper; } static ReflectionHelper() { IsMono = Type.GetType("Mono.Runtime") != null || Type.GetType("Mono.RuntimeStructs") != null; IsCore = typeof(object).Assembly.GetName().Name == "System.Private.CoreLib"; _NoArgs = new object[0]; AssemblyCache = new Dictionary(); AssembliesCache = new Dictionary(); ResolveReflectionCache = new Dictionary(); AssemblyHashPrefix = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes("MonoModRefl").Concat(new byte[1]).ToArray(); AssemblyHashNameTag = "@#"; _CacheGetterArgs = new object[2] { 0, null }; t_RuntimeType = typeof(Type).Assembly.GetType("System.RuntimeType"); t_RuntimeTypeCache = t_RuntimeType.GetNestedType("RuntimeTypeCache", BindingFlags.Public | BindingFlags.NonPublic); p_RuntimeType_Cache = ((t_RuntimeTypeCache == null) ? null : typeof(Type).Assembly.GetType("System.RuntimeType")?.GetProperty("Cache", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, t_RuntimeTypeCache, Type.EmptyTypes, null)); m_RuntimeTypeCache_GetFieldList = typeof(Type).Assembly.GetType("System.RuntimeType+RuntimeTypeCache")?.GetMethod("GetFieldList", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); m_RuntimeTypeCache_GetPropertyList = typeof(Type).Assembly.GetType("System.RuntimeType+RuntimeTypeCache")?.GetMethod("GetPropertyList", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); _CacheFixed = new ConditionalWeakTable(); t_RuntimeModule = typeof(Module).Assembly.GetType("System.Reflection.RuntimeModule"); p_RuntimeModule_RuntimeType = typeof(Module).Assembly.GetType("System.Reflection.RuntimeModule")?.GetProperty("RuntimeType", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); f_RuntimeModule__impl = typeof(Module).Assembly.GetType("System.Reflection.RuntimeModule")?.GetField("_impl", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); m_RuntimeModule_GetGlobalType = typeof(Module).Assembly.GetType("System.Reflection.RuntimeModule")?.GetMethod("GetGlobalType", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); f_SignatureHelper_module = typeof(SignatureHelper).GetField("m_module", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? typeof(SignatureHelper).GetField("module", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } public static void FixReflectionCacheAuto(this Type type) { } public static void FixReflectionCache(this Type type) { if (t_RuntimeType == null || p_RuntimeType_Cache == null || m_RuntimeTypeCache_GetFieldList == null || m_RuntimeTypeCache_GetPropertyList == null) { return; } while (type != null) { if (t_RuntimeType.IsInstanceOfType(type)) { CacheFixEntry value = _CacheFixed.GetValue(type, delegate(Type rt) { CacheFixEntry cacheFixEntry = new CacheFixEntry(); object cache = (cacheFixEntry.Cache = p_RuntimeType_Cache.GetValue(rt, _NoArgs)); Array orig = (cacheFixEntry.Properties = _GetArray(cache, m_RuntimeTypeCache_GetPropertyList)); Array orig2 = (cacheFixEntry.Fields = _GetArray(cache, m_RuntimeTypeCache_GetFieldList)); _FixReflectionCacheOrder(orig); _FixReflectionCacheOrder(orig2); cacheFixEntry.NeedsVerify = false; return cacheFixEntry; }); if (value.NeedsVerify && !_Verify(value, type)) { lock (value) { _FixReflectionCacheOrder(value.Properties); _FixReflectionCacheOrder(value.Fields); } } value.NeedsVerify = true; } type = type.DeclaringType; } } private static bool _Verify(CacheFixEntry entry, Type type) { object value; if (entry.Cache != (value = p_RuntimeType_Cache.GetValue(type, _NoArgs))) { entry.Cache = value; entry.Properties = _GetArray(value, m_RuntimeTypeCache_GetPropertyList); entry.Fields = _GetArray(value, m_RuntimeTypeCache_GetFieldList); return false; } Array properties; if (entry.Properties != (properties = _GetArray(value, m_RuntimeTypeCache_GetPropertyList))) { entry.Properties = properties; entry.Fields = _GetArray(value, m_RuntimeTypeCache_GetFieldList); return false; } Array fields; if (entry.Fields != (fields = _GetArray(value, m_RuntimeTypeCache_GetFieldList))) { entry.Fields = fields; return false; } return true; } private static Array _GetArray(object cache, MethodInfo getter) { getter.Invoke(cache, _CacheGetterArgs); return (Array)getter.Invoke(cache, _CacheGetterArgs); } private static void _FixReflectionCacheOrder(Array orig) where T : MemberInfo { List list = new List(orig.Length); for (int i = 0; i < orig.Length; i++) { list.Add((T)orig.GetValue(i)); } list.Sort((T a, T b) => a.MetadataToken - b.MetadataToken); for (int num = orig.Length - 1; num >= 0; num--) { orig.SetValue(list[num], num); } } public static Type GetModuleType(this Module module) { if (module == null || t_RuntimeModule == null || !t_RuntimeModule.IsInstanceOfType(module)) { return null; } if (p_RuntimeModule_RuntimeType != null) { return (Type)p_RuntimeModule_RuntimeType.GetValue(module, _NoArgs); } if (f_RuntimeModule__impl != null && m_RuntimeModule_GetGlobalType != null) { return (Type)m_RuntimeModule_GetGlobalType.Invoke(null, new object[1] { f_RuntimeModule__impl.GetValue(module) }); } return null; } public static Type GetRealDeclaringType(this MemberInfo member) { Type type = member.DeclaringType; if ((object)type == null) { Module module = member.Module; if ((object)module == null) { return null; } type = module.GetModuleType(); } return type; } public static CallSite ImportCallSite(this ModuleDefinition moduleTo, ICallSiteGenerator signature) { return signature.ToCallSite(moduleTo); } public static CallSite ImportCallSite(this ModuleDefinition moduleTo, SignatureHelper signature) { return moduleTo.ImportCallSite(f_SignatureHelper_module.GetValue(signature) as Module, signature.GetSignature()); } public static CallSite ImportCallSite(this ModuleDefinition moduleTo, Module moduleFrom, int token) { return moduleTo.ImportCallSite(moduleFrom, moduleFrom.ResolveSignature(token)); } public static CallSite ImportCallSite(this ModuleDefinition moduleTo, Module moduleFrom, byte[] data) { //IL_0020: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Expected O, but got Unknown CallSite val = new CallSite(moduleTo.TypeSystem.Void); BinaryReader reader; using (MemoryStream input = new MemoryStream(data, writable: false)) { reader = new BinaryReader(input); try { ReadMethodSignature((IMethodSignature)(object)val); return val; } finally { if (reader != null) { ((IDisposable)reader).Dispose(); } } void ReadMethodSignature(IMethodSignature method) { //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_007a: Expected O, but got Unknown byte b3 = reader.ReadByte(); if ((b3 & 0x20u) != 0) { method.HasThis = true; b3 = (byte)(b3 & 0xFFFFFFDFu); } if ((b3 & 0x40u) != 0) { method.ExplicitThis = true; b3 = (byte)(b3 & 0xFFFFFFBFu); } method.CallingConvention = (MethodCallingConvention)b3; if ((b3 & 0x10u) != 0) { ReadCompressedUInt32(); } uint num8 = ReadCompressedUInt32(); method.MethodReturnType.ReturnType = ReadTypeSignature(); for (int l = 0; l < num8; l++) { method.Parameters.Add(new ParameterDefinition(ReadTypeSignature())); } } } TypeReference GetTypeDefOrRef() { uint num3 = ReadCompressedUInt32(); uint num4 = num3 >> 2; return moduleTo.ImportReference(moduleFrom.ResolveType((num3 & 3) switch { 0u => (int)(0x2000000 | num4), 1u => (int)(0x1000000 | num4), 2u => (int)(0x1B000000 | num4), _ => 0, })); } int ReadCompressedInt32() { byte b = reader.ReadByte(); reader.BaseStream.Seek(-1L, SeekOrigin.Current); uint num = ReadCompressedUInt32(); int num2 = (int)num >> 1; if ((num & 1) == 0) { return num2; } switch (b & 0xC0) { case 0: case 64: return num2 - 64; case 128: return num2 - 8192; default: return num2 - 268435456; } } uint ReadCompressedUInt32() { byte b2 = reader.ReadByte(); if ((b2 & 0x80) == 0) { return b2; } if ((b2 & 0x40) == 0) { return (uint)(((b2 & -129) << 8) | reader.ReadByte()); } return (uint)(((b2 & -193) << 24) | (reader.ReadByte() << 16) | (reader.ReadByte() << 8) | reader.ReadByte()); } TypeReference ReadTypeSignature() { //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Expected I4, but got Unknown //IL_00b5: Unknown result type (might be due to invalid IL or missing references) //IL_00bb: Expected O, but got Unknown //IL_00d0: Unknown result type (might be due to invalid IL or missing references) //IL_00d6: Expected O, but got Unknown //IL_0221: Unknown result type (might be due to invalid IL or missing references) //IL_00f7: Unknown result type (might be due to invalid IL or missing references) //IL_00fd: Expected O, but got Unknown //IL_0369: Unknown result type (might be due to invalid IL or missing references) //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_00c0: Unknown result type (might be due to invalid IL or missing references) //IL_00c8: Expected O, but got Unknown //IL_00c9: Expected O, but got Unknown //IL_00ea: Unknown result type (might be due to invalid IL or missing references) //IL_00f0: Expected O, but got Unknown //IL_0209: Unknown result type (might be due to invalid IL or missing references) //IL_020f: Expected O, but got Unknown //IL_01f5: Unknown result type (might be due to invalid IL or missing references) //IL_01fb: Expected O, but got Unknown //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_0097: Invalid comparison between Unknown and I4 //IL_0216: Unknown result type (might be due to invalid IL or missing references) //IL_021c: Expected O, but got Unknown //IL_009c: Unknown result type (might be due to invalid IL or missing references) //IL_009f: Invalid comparison between Unknown and I4 //IL_00dd: Unknown result type (might be due to invalid IL or missing references) //IL_00e3: Expected O, but got Unknown //IL_01ce: Unknown result type (might be due to invalid IL or missing references) MetadataType val2 = (MetadataType)reader.ReadByte(); switch (val2 - 1) { default: if ((int)val2 == 65) { return (TypeReference)new SentinelType(ReadTypeSignature()); } if ((int)val2 == 69) { return (TypeReference)new PinnedType(ReadTypeSignature()); } break; case 16: case 17: return GetTypeDefOrRef(); case 14: return (TypeReference)new PointerType(ReadTypeSignature()); case 26: { FunctionPointerType val4 = new FunctionPointerType(); ReadMethodSignature((IMethodSignature)val4); return (TypeReference)val4; } case 15: return (TypeReference)new ByReferenceType(ReadTypeSignature()); case 28: return (TypeReference)new ArrayType(ReadTypeSignature()); case 19: { ArrayType val3 = new ArrayType(ReadTypeSignature()); uint num5 = ReadCompressedUInt32(); uint[] array = new uint[ReadCompressedUInt32()]; for (int i = 0; i < array.Length; i++) { array[i] = ReadCompressedUInt32(); } int[] array2 = new int[ReadCompressedUInt32()]; for (int j = 0; j < array2.Length; j++) { array2[j] = ReadCompressedInt32(); } val3.Dimensions.Clear(); for (int k = 0; k < num5; k++) { int? num6 = null; int? num7 = null; if (k < array2.Length) { num6 = array2[k]; } if (k < array.Length) { num7 = num6 + (int)array[k] - 1; } val3.Dimensions.Add(new ArrayDimension(num6, num7)); } return (TypeReference)(object)val3; } case 31: return (TypeReference)new OptionalModifierType(GetTypeDefOrRef(), ReadTypeSignature()); case 30: return (TypeReference)new RequiredModifierType(GetTypeDefOrRef(), ReadTypeSignature()); case 18: case 20: case 29: throw new NotSupportedException($"Unsupported generic callsite element: {val2}"); case 27: return moduleTo.TypeSystem.Object; case 0: return moduleTo.TypeSystem.Void; case 21: return moduleTo.TypeSystem.TypedReference; case 23: return moduleTo.TypeSystem.IntPtr; case 24: return moduleTo.TypeSystem.UIntPtr; case 1: return moduleTo.TypeSystem.Boolean; case 2: return moduleTo.TypeSystem.Char; case 3: return moduleTo.TypeSystem.SByte; case 4: return moduleTo.TypeSystem.Byte; case 5: return moduleTo.TypeSystem.Int16; case 6: return moduleTo.TypeSystem.UInt16; case 7: return moduleTo.TypeSystem.Int32; case 8: return moduleTo.TypeSystem.UInt32; case 9: return moduleTo.TypeSystem.Int64; case 10: return moduleTo.TypeSystem.UInt64; case 11: return moduleTo.TypeSystem.Single; case 12: return moduleTo.TypeSystem.Double; case 13: return moduleTo.TypeSystem.String; case 22: case 25: break; } throw new NotSupportedException($"Unsupported callsite element: {val2}"); } } } public class RelinkFailedException : Exception { public const string DefaultMessage = "MonoMod failed relinking"; public IMetadataTokenProvider MTP; public IMetadataTokenProvider Context; public RelinkFailedException(IMetadataTokenProvider mtp, IMetadataTokenProvider context = null) : this(_Format("MonoMod failed relinking", mtp, context), mtp, context) { } public RelinkFailedException(string message, IMetadataTokenProvider mtp, IMetadataTokenProvider context = null) : base(message) { MTP = mtp; Context = context; } public RelinkFailedException(string message, Exception innerException, IMetadataTokenProvider mtp, IMetadataTokenProvider context = null) : base(message ?? _Format("MonoMod failed relinking", mtp, context), innerException) { MTP = mtp; Context = context; } protected static string _Format(string message, IMetadataTokenProvider mtp, IMetadataTokenProvider context) { if (mtp == null && context == null) { return message; } StringBuilder stringBuilder = new StringBuilder(message); stringBuilder.Append(" "); if (mtp != null) { stringBuilder.Append(((object)mtp).ToString()); } if (context != null) { stringBuilder.Append(" "); } if (context != null) { stringBuilder.Append("(context: ").Append(((object)context).ToString()).Append(")"); } return stringBuilder.ToString(); } } public class RelinkTargetNotFoundException : RelinkFailedException { public new const string DefaultMessage = "MonoMod relinker failed finding"; public RelinkTargetNotFoundException(IMetadataTokenProvider mtp, IMetadataTokenProvider context = null) : base(RelinkFailedException._Format("MonoMod relinker failed finding", mtp, context), mtp, context) { } public RelinkTargetNotFoundException(string message, IMetadataTokenProvider mtp, IMetadataTokenProvider context = null) : base(message ?? "MonoMod relinker failed finding", mtp, context) { } public RelinkTargetNotFoundException(string message, Exception innerException, IMetadataTokenProvider mtp, IMetadataTokenProvider context = null) : base(message ?? "MonoMod relinker failed finding", innerException, mtp, context) { } } public sealed class WeakReferenceComparer : EqualityComparer { public override bool Equals(WeakReference x, WeakReference y) { if (x.SafeGetTarget() == y.SafeGetTarget()) { return x.SafeGetIsAlive() == y.SafeGetIsAlive(); } return false; } public override int GetHashCode(WeakReference obj) { return obj.SafeGetTarget()?.GetHashCode() ?? 0; } } } namespace MonoMod.Utils.Cil { public sealed class CecilILGenerator : ILGeneratorShim { private class LabelInfo { public bool Emitted; public Instruction Instruction = Instruction.Create(OpCodes.Nop); public readonly List Branches = new List(); } private class LabelledExceptionHandler { public Label TryStart = NullLabel; public Label TryEnd = NullLabel; public Label HandlerStart = NullLabel; public Label HandlerEnd = NullLabel; public Label FilterStart = NullLabel; public ExceptionHandlerType HandlerType; public TypeReference ExceptionType; } private class ExceptionHandlerChain { private readonly CecilILGenerator IL; private readonly Label _Start; public readonly Label SkipAll; private Label _SkipHandler; private LabelledExceptionHandler _Prev; private LabelledExceptionHandler _Handler; public ExceptionHandlerChain(CecilILGenerator il) { IL = il; _Start = il.DefineLabel(); il.MarkLabel(_Start); SkipAll = il.DefineLabel(); } public LabelledExceptionHandler BeginHandler(ExceptionHandlerType type) { //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0073: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Invalid comparison between Unknown and I4 LabelledExceptionHandler labelledExceptionHandler = (_Prev = _Handler); if (labelledExceptionHandler != null) { EndHandler(labelledExceptionHandler); } IL.Emit(OpCodes.Leave, _SkipHandler = IL.DefineLabel()); Label label = IL.DefineLabel(); IL.MarkLabel(label); LabelledExceptionHandler labelledExceptionHandler2 = (_Handler = new LabelledExceptionHandler { TryStart = _Start, TryEnd = label, HandlerType = type, HandlerEnd = _SkipHandler }); if ((int)type == 1) { labelledExceptionHandler2.FilterStart = label; } else { labelledExceptionHandler2.HandlerStart = label; } return labelledExceptionHandler2; } public void EndHandler(LabelledExceptionHandler handler) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Invalid comparison between Unknown and I4 //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0014: Invalid comparison between Unknown and I4 Label skipHandler = _SkipHandler; ExceptionHandlerType handlerType = handler.HandlerType; if ((int)handlerType != 1) { if ((int)handlerType == 2) { ((ILGeneratorShim)IL).Emit(OpCodes.Endfinally); } else { IL.Emit(OpCodes.Leave, skipHandler); } } else { ((ILGeneratorShim)IL).Emit(OpCodes.Endfilter); } IL.MarkLabel(skipHandler); IL._ExceptionHandlersToMark.Add(handler); } public void End() { EndHandler(_Handler); IL.MarkLabel(SkipAll); } } private static readonly ConstructorInfo c_LocalBuilder; private static readonly FieldInfo f_LocalBuilder_position; private static readonly FieldInfo f_LocalBuilder_is_pinned; private static int c_LocalBuilder_params; private static readonly Dictionary _MCCOpCodes; private static Label NullLabel; public readonly ILProcessor IL; private readonly Dictionary _LabelInfos = new Dictionary(); private readonly List _LabelsToMark = new List(); private readonly List _ExceptionHandlersToMark = new List(); private readonly Dictionary _Variables = new Dictionary(); private readonly Stack _ExceptionHandlers = new Stack(); private int labelCounter; private int _ILOffset; public override int ILOffset => _ILOffset; unsafe static CecilILGenerator() { //IL_00a0: Unknown result type (might be due to invalid IL or missing references) //IL_00a5: Unknown result type (might be due to invalid IL or missing references) //IL_00b2: Unknown result type (might be due to invalid IL or missing references) c_LocalBuilder = (from c in typeof(LocalBuilder).GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) orderby c.GetParameters().Length descending select c).First(); f_LocalBuilder_position = typeof(LocalBuilder).GetField("position", BindingFlags.Instance | BindingFlags.NonPublic); f_LocalBuilder_is_pinned = typeof(LocalBuilder).GetField("is_pinned", BindingFlags.Instance | BindingFlags.NonPublic); c_LocalBuilder_params = c_LocalBuilder.GetParameters().Length; _MCCOpCodes = new Dictionary(); FieldInfo[] fields = typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < fields.Length; i++) { OpCode value = (OpCode)fields[i].GetValue(null); _MCCOpCodes[((OpCode)(ref value)).Value] = value; } Label nullLabel = default(Label); *(int*)(&nullLabel) = -1; NullLabel = nullLabel; } public CecilILGenerator(ILProcessor il) { IL = il; } private OpCode _(OpCode opcode) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) return _MCCOpCodes[opcode.Value]; } private LabelInfo _(Label handle) { if (!_LabelInfos.TryGetValue(handle, out var value)) { return null; } return value; } private VariableDefinition _(LocalBuilder handle) { return _Variables[handle]; } private TypeReference _(Type info) { return ((MemberReference)IL.Body.Method).Module.ImportReference(info); } private FieldReference _(FieldInfo info) { return ((MemberReference)IL.Body.Method).Module.ImportReference(info); } private MethodReference _(MethodBase info) { return ((MemberReference)IL.Body.Method).Module.ImportReference(info); } private Instruction ProcessLabels(Instruction ins) { //IL_011b: Unknown result type (might be due to invalid IL or missing references) //IL_0120: Unknown result type (might be due to invalid IL or missing references) //IL_0125: Unknown result type (might be due to invalid IL or missing references) //IL_0144: Unknown result type (might be due to invalid IL or missing references) //IL_0163: Unknown result type (might be due to invalid IL or missing references) //IL_0182: Unknown result type (might be due to invalid IL or missing references) //IL_01a1: Unknown result type (might be due to invalid IL or missing references) //IL_01c0: Unknown result type (might be due to invalid IL or missing references) //IL_01d2: Expected O, but got Unknown if (_LabelsToMark.Count != 0) { foreach (LabelInfo item in _LabelsToMark) { foreach (Instruction branch in item.Branches) { object operand = branch.Operand; if (!(operand is Instruction)) { if (!(operand is Instruction[] array)) { continue; } for (int i = 0; i < array.Length; i++) { if (array[i] == item.Instruction) { array[i] = ins; break; } } } else { branch.Operand = ins; } } item.Emitted = true; item.Instruction = ins; } _LabelsToMark.Clear(); } if (_ExceptionHandlersToMark.Count != 0) { foreach (LabelledExceptionHandler item2 in _ExceptionHandlersToMark) { IL.Body.ExceptionHandlers.Add(new ExceptionHandler(item2.HandlerType) { TryStart = _(item2.TryStart)?.Instruction, TryEnd = _(item2.TryEnd)?.Instruction, HandlerStart = _(item2.HandlerStart)?.Instruction, HandlerEnd = _(item2.HandlerEnd)?.Instruction, FilterStart = _(item2.FilterStart)?.Instruction, CatchType = item2.ExceptionType }); } _ExceptionHandlersToMark.Clear(); } return ins; } public unsafe override Label DefineLabel() { Label label = default(Label); *(int*)(&label) = labelCounter++; _LabelInfos[label] = new LabelInfo(); return label; } public override void MarkLabel(Label loc) { if (_LabelInfos.TryGetValue(loc, out var value) && !value.Emitted) { _LabelsToMark.Add(value); } } public override LocalBuilder DeclareLocal(Type type) { return DeclareLocal(type, pinned: false); } public override LocalBuilder DeclareLocal(Type type, bool pinned) { //IL_00f0: Unknown result type (might be due to invalid IL or missing references) //IL_00f6: Expected O, but got Unknown //IL_00e9: Unknown result type (might be due to invalid IL or missing references) //IL_00ef: Expected O, but got Unknown int count = IL.Body.Variables.Count; object obj; if (c_LocalBuilder_params != 4) { if (c_LocalBuilder_params != 3) { if (c_LocalBuilder_params != 2) { if (c_LocalBuilder_params != 0) { throw new NotSupportedException(); } obj = c_LocalBuilder.Invoke(new object[0]); } else { obj = c_LocalBuilder.Invoke(new object[2] { type, null }); } } else { obj = c_LocalBuilder.Invoke(new object[3] { count, type, null }); } } else { obj = c_LocalBuilder.Invoke(new object[4] { count, type, null, pinned }); } LocalBuilder localBuilder = (LocalBuilder)obj; f_LocalBuilder_position?.SetValue(localBuilder, (ushort)count); f_LocalBuilder_is_pinned?.SetValue(localBuilder, pinned); TypeReference val = _(type); if (pinned) { val = (TypeReference)new PinnedType(val); } VariableDefinition val2 = new VariableDefinition(val); IL.Body.Variables.Add(val2); _Variables[localBuilder] = val2; return localBuilder; } private void Emit(Instruction ins) { ins.Offset = _ILOffset; _ILOffset += ins.GetSize(); IL.Append(ProcessLabels(ins)); } public override void Emit(OpCode opcode) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode))); } public override void Emit(OpCode opcode, byte arg) { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) if (opcode.OperandType == OperandType.ShortInlineVar || opcode.OperandType == OperandType.InlineVar) { _EmitInlineVar(_(opcode), arg); } else { Emit(IL.Create(_(opcode), arg)); } } public override void Emit(OpCode opcode, sbyte arg) { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) if (opcode.OperandType == OperandType.ShortInlineVar || opcode.OperandType == OperandType.InlineVar) { _EmitInlineVar(_(opcode), arg); } else { Emit(IL.Create(_(opcode), arg)); } } public override void Emit(OpCode opcode, short arg) { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) if (opcode.OperandType == OperandType.ShortInlineVar || opcode.OperandType == OperandType.InlineVar) { _EmitInlineVar(_(opcode), arg); } else { Emit(IL.Create(_(opcode), (int)arg)); } } public override void Emit(OpCode opcode, int arg) { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_005d: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) if (opcode.OperandType == OperandType.ShortInlineVar || opcode.OperandType == OperandType.InlineVar) { _EmitInlineVar(_(opcode), arg); } else if (opcode.Name.EndsWith(".s", StringComparison.Ordinal)) { Emit(IL.Create(_(opcode), (sbyte)arg)); } else { Emit(IL.Create(_(opcode), arg)); } } public override void Emit(OpCode opcode, long arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), arg)); } public override void Emit(OpCode opcode, float arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), arg)); } public override void Emit(OpCode opcode, double arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), arg)); } public override void Emit(OpCode opcode, string arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), arg)); } public override void Emit(OpCode opcode, Type arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), _(arg))); } public override void Emit(OpCode opcode, FieldInfo arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), _(arg))); } public override void Emit(OpCode opcode, ConstructorInfo arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), _(arg))); } public override void Emit(OpCode opcode, MethodInfo arg) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), _(arg))); } public override void Emit(OpCode opcode, Label label) { //IL_000f: Unknown result type (might be due to invalid IL or missing references) LabelInfo labelInfo = _(label); Instruction val = IL.Create(_(opcode), _(label).Instruction); labelInfo.Branches.Add(val); Emit(ProcessLabels(val)); } public override void Emit(OpCode opcode, Label[] labels) { //IL_0020: Unknown result type (might be due to invalid IL or missing references) IEnumerable enumerable = labels.Distinct().Select(_); Instruction val = IL.Create(_(opcode), enumerable.Select((LabelInfo labelInfo) => labelInfo.Instruction).ToArray()); foreach (LabelInfo item in enumerable) { item.Branches.Add(val); } Emit(ProcessLabels(val)); } public override void Emit(OpCode opcode, LocalBuilder local) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), _(local))); } public override void Emit(OpCode opcode, SignatureHelper signature) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), ((MemberReference)IL.Body.Method).Module.ImportCallSite(signature))); } public void Emit(OpCode opcode, ICallSiteGenerator signature) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), ((MemberReference)IL.Body.Method).Module.ImportCallSite(signature))); } private void _EmitInlineVar(OpCode opcode, int index) { //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_002d: Expected I4, but got Unknown //IL_0064: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Unknown result type (might be due to invalid IL or missing references) //IL_0097: Unknown result type (might be due to invalid IL or missing references) OperandType operandType = ((OpCode)(ref opcode)).OperandType; switch (operandType - 13) { case 1: case 6: Emit(IL.Create(opcode, ((MethodReference)IL.Body.Method).Parameters[index])); break; case 0: case 5: Emit(IL.Create(opcode, IL.Body.Variables[index])); break; default: throw new NotSupportedException($"Unsupported SRE InlineVar -> Cecil {((OpCode)(ref opcode)).OperandType} for {opcode} {index}"); } } public override void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes) { //IL_0009: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(_(opcode), _(methodInfo))); } public override void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes) { throw new NotSupportedException(); } public override void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes) { throw new NotSupportedException(); } public override void EmitWriteLine(FieldInfo field) { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0061: Unknown result type (might be due to invalid IL or missing references) if (field.IsStatic) { Emit(IL.Create(OpCodes.Ldsfld, _(field))); } else { Emit(IL.Create(OpCodes.Ldarg_0)); Emit(IL.Create(OpCodes.Ldfld, _(field))); } Emit(IL.Create(OpCodes.Call, _(typeof(Console).GetMethod("WriteLine", new Type[1] { field.FieldType })))); } public override void EmitWriteLine(LocalBuilder localBuilder) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(OpCodes.Ldloc, _(localBuilder))); Emit(IL.Create(OpCodes.Call, _(typeof(Console).GetMethod("WriteLine", new Type[1] { localBuilder.LocalType })))); } public override void EmitWriteLine(string value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(OpCodes.Ldstr, value)); Emit(IL.Create(OpCodes.Call, _(typeof(Console).GetMethod("WriteLine", new Type[1] { typeof(string) })))); } public override void ThrowException(Type type) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) Emit(IL.Create(OpCodes.Newobj, _(type.GetConstructor(Type.EmptyTypes)))); Emit(IL.Create(OpCodes.Throw)); } public override Label BeginExceptionBlock() { ExceptionHandlerChain exceptionHandlerChain = new ExceptionHandlerChain(this); _ExceptionHandlers.Push(exceptionHandlerChain); return exceptionHandlerChain.SkipAll; } public override void BeginCatchBlock(Type exceptionType) { _ExceptionHandlers.Peek().BeginHandler((ExceptionHandlerType)0).ExceptionType = ((exceptionType == null) ? null : _(exceptionType)); } public override void BeginExceptFilterBlock() { _ExceptionHandlers.Peek().BeginHandler((ExceptionHandlerType)1); } public override void BeginFaultBlock() { _ExceptionHandlers.Peek().BeginHandler((ExceptionHandlerType)4); } public override void BeginFinallyBlock() { _ExceptionHandlers.Peek().BeginHandler((ExceptionHandlerType)2); } public override void EndExceptionBlock() { _ExceptionHandlers.Pop().End(); } public override void BeginScope() { } public override void EndScope() { } public override void UsingNamespace(string usingNamespace) { } } public abstract class ILGeneratorShim { internal static class ILGeneratorBuilder { public const string Namespace = "MonoMod.Utils.Cil"; public const string Name = "ILGeneratorProxy"; public const string FullName = "MonoMod.Utils.Cil.ILGeneratorProxy"; public const string TargetName = "Target"; private static Type ProxyType; public static Type GenerateProxy() { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_004a: Expected O, but got Unknown //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_005f: Expected O, but got Unknown //IL_008b: Unknown result type (might be due to invalid IL or missing references) //IL_00b3: Unknown result type (might be due to invalid IL or missing references) //IL_00b8: Unknown result type (might be due to invalid IL or missing references) //IL_00c8: Expected O, but got Unknown //IL_00e7: Unknown result type (might be due to invalid IL or missing references) //IL_00ee: Expected O, but got Unknown //IL_00f7: Unknown result type (might be due to invalid IL or missing references) //IL_0101: Expected O, but got Unknown //IL_0117: Unknown result type (might be due to invalid IL or missing references) //IL_011e: Expected O, but got Unknown //IL_012e: Unknown result type (might be due to invalid IL or missing references) //IL_0135: Expected O, but got Unknown //IL_014c: Unknown result type (might be due to invalid IL or missing references) //IL_0153: Expected O, but got Unknown //IL_0169: Unknown result type (might be due to invalid IL or missing references) //IL_0170: Expected O, but got Unknown //IL_0179: Unknown result type (might be due to invalid IL or missing references) //IL_0183: Expected O, but got Unknown //IL_01a1: Unknown result type (might be due to invalid IL or missing references) //IL_01ad: Unknown result type (might be due to invalid IL or missing references) //IL_01b9: Unknown result type (might be due to invalid IL or missing references) //IL_01c7: Unknown result type (might be due to invalid IL or missing references) //IL_0250: Unknown result type (might be due to invalid IL or missing references) //IL_0255: Unknown result type (might be due to invalid IL or missing references) //IL_025e: Expected O, but got Unknown //IL_0288: Unknown result type (might be due to invalid IL or missing references) //IL_0292: Expected O, but got Unknown //IL_02be: Unknown result type (might be due to invalid IL or missing references) //IL_02ca: Unknown result type (might be due to invalid IL or missing references) //IL_02dd: Unknown result type (might be due to invalid IL or missing references) //IL_02e2: Unknown result type (might be due to invalid IL or missing references) //IL_02f1: Unknown result type (might be due to invalid IL or missing references) //IL_0328: Unknown result type (might be due to invalid IL or missing references) //IL_0321: Unknown result type (might be due to invalid IL or missing references) //IL_034c: Unknown result type (might be due to invalid IL or missing references) if (ProxyType != null) { return ProxyType; } Type typeFromHandle = typeof(ILGenerator); Type typeFromHandle2 = typeof(ILGeneratorShim); ModuleDefinition val = ModuleDefinition.CreateModule("MonoMod.Utils.Cil.ILGeneratorProxy", new ModuleParameters { Kind = (ModuleKind)0, ReflectionImporterProvider = MMReflectionImporter.Provider }); Assembly assembly; try { CustomAttribute val2 = new CustomAttribute(val.ImportReference((MethodBase)DynamicMethodDefinition.c_IgnoresAccessChecksToAttribute)); val2.ConstructorArguments.Add(new CustomAttributeArgument(val.TypeSystem.String, (object)typeof(ILGeneratorShim).Assembly.GetName().Name)); val.Assembly.CustomAttributes.Add(val2); TypeDefinition val3 = new TypeDefinition("MonoMod.Utils.Cil", "ILGeneratorProxy", (TypeAttributes)1) { BaseType = val.ImportReference(typeFromHandle) }; val.Types.Add(val3); TypeReference val4 = val.ImportReference(typeFromHandle2); GenericParameter val5 = new GenericParameter("TTarget", (IGenericParameterProvider)(object)val3); val5.Constraints.Add(new GenericParameterConstraint(val4)); ((TypeReference)val3).GenericParameters.Add(val5); FieldDefinition val6 = new FieldDefinition("Target", (FieldAttributes)6, (TypeReference)(object)val5); val3.Fields.Add(val6); GenericInstanceType val7 = new GenericInstanceType((TypeReference)(object)val3); val7.GenericArguments.Add((TypeReference)(object)val5); FieldReference val8 = new FieldReference("Target", (TypeReference)(object)val5, (TypeReference)(object)val7); MethodDefinition val9 = new MethodDefinition(".ctor", (MethodAttributes)6278, val.TypeSystem.Void); ((MethodReference)val9).Parameters.Add(new ParameterDefinition((TypeReference)(object)val5)); val3.Methods.Add(val9); ILProcessor iLProcessor = val9.Body.GetILProcessor(); iLProcessor.Emit(OpCodes.Ldarg_0); iLProcessor.Emit(OpCodes.Ldarg_1); iLProcessor.Emit(OpCodes.Stfld, val8); iLProcessor.Emit(OpCodes.Ret); MethodInfo[] methods = typeFromHandle.GetMethods(BindingFlags.Instance | BindingFlags.Public); foreach (MethodInfo methodInfo in methods) { MethodInfo method = typeFromHandle2.GetMethod(methodInfo.Name, (from p in methodInfo.GetParameters() select p.ParameterType).ToArray()); if (method == null) { continue; } MethodDefinition val10 = new MethodDefinition(methodInfo.Name, (MethodAttributes)198, val.ImportReference(methodInfo.ReturnType)) { HasThis = true }; ParameterInfo[] parameters = methodInfo.GetParameters(); foreach (ParameterInfo parameterInfo in parameters) { ((MethodReference)val10).Parameters.Add(new ParameterDefinition(val.ImportReference(parameterInfo.ParameterType))); } val3.Methods.Add(val10); iLProcessor = val10.Body.GetILProcessor(); iLProcessor.Emit(OpCodes.Ldarg_0); iLProcessor.Emit(OpCodes.Ldfld, val8); Enumerator enumerator = ((MethodReference)val10).Parameters.GetEnumerator(); try { while (enumerator.MoveNext()) { ParameterDefinition current = enumerator.Current; iLProcessor.Emit(OpCodes.Ldarg, current); } } finally { ((IDisposable)enumerator).Dispose(); } iLProcessor.Emit(method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, ((MemberReference)iLProcessor.Body.Method).Module.ImportReference((MethodBase)method)); iLProcessor.Emit(OpCodes.Ret); } assembly = ReflectionHelper.Load(val); assembly.SetMonoCorlibInternal(value: true); } finally { ((IDisposable)val)?.Dispose(); } ResolveEventHandler value = (object? asmSender, ResolveEventArgs asmArgs) => (new AssemblyName(asmArgs.Name).Name == typeof(ILGeneratorBuilder).Assembly.GetName().Name) ? typeof(ILGeneratorBuilder).Assembly : null; AppDomain.CurrentDomain.AssemblyResolve += value; try { ProxyType = assembly.GetType("MonoMod.Utils.Cil.ILGeneratorProxy"); } finally { AppDomain.CurrentDomain.AssemblyResolve -= value; } if (ProxyType == null) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Couldn't find ILGeneratorShim proxy \"").Append("MonoMod.Utils.Cil.ILGeneratorProxy").Append("\" in autogenerated \"") .Append(assembly.FullName) .AppendLine("\""); Type[] types; Exception[] array; try { types = assembly.GetTypes(); array = null; } catch (ReflectionTypeLoadException ex) { types = ex.Types; array = new Exception[ex.LoaderExceptions.Length + 1]; array[0] = ex; for (int k = 0; k < ex.LoaderExceptions.Length; k++) { array[k + 1] = ex.LoaderExceptions[k]; } } stringBuilder.AppendLine("Listing all types in autogenerated assembly:"); Type[] array2 = types; for (int i = 0; i < array2.Length; i++) { stringBuilder.AppendLine(array2[i]?.FullName ?? ""); } if (((array != null && array.Length != 0) ? 1 : 0) > (false ? 1 : 0)) { stringBuilder.AppendLine("Listing all exceptions:"); for (int l = 0; l < array.Length; l++) { stringBuilder.Append("#").Append(l).Append(": ") .AppendLine(array[l]?.ToString() ?? "NULL"); } } throw new Exception(stringBuilder.ToString()); } return ProxyType; } } public abstract int ILOffset { get; } public static Type ProxyType => ILGeneratorBuilder.GenerateProxy(); public abstract void BeginCatchBlock(Type exceptionType); public abstract void BeginExceptFilterBlock(); public abstract Label BeginExceptionBlock(); public abstract void BeginFaultBlock(); public abstract void BeginFinallyBlock(); public abstract void BeginScope(); public abstract LocalBuilder DeclareLocal(Type localType); public abstract LocalBuilder DeclareLocal(Type localType, bool pinned); public abstract Label DefineLabel(); public abstract void Emit(OpCode opcode); public abstract void Emit(OpCode opcode, byte arg); public abstract void Emit(OpCode opcode, double arg); public abstract void Emit(OpCode opcode, short arg); public abstract void Emit(OpCode opcode, int arg); public abstract void Emit(OpCode opcode, long arg); public abstract void Emit(OpCode opcode, ConstructorInfo con); public abstract void Emit(OpCode opcode, Label label); public abstract void Emit(OpCode opcode, Label[] labels); public abstract void Emit(OpCode opcode, LocalBuilder local); public abstract void Emit(OpCode opcode, SignatureHelper signature); public abstract void Emit(OpCode opcode, FieldInfo field); public abstract void Emit(OpCode opcode, MethodInfo meth); public abstract void Emit(OpCode opcode, sbyte arg); public abstract void Emit(OpCode opcode, float arg); public abstract void Emit(OpCode opcode, string str); public abstract void Emit(OpCode opcode, Type cls); public abstract void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes); public abstract void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes); public abstract void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes); public abstract void EmitWriteLine(LocalBuilder localBuilder); public abstract void EmitWriteLine(FieldInfo fld); public abstract void EmitWriteLine(string value); public abstract void EndExceptionBlock(); public abstract void EndScope(); public abstract void MarkLabel(Label loc); public abstract void ThrowException(Type excType); public abstract void UsingNamespace(string usingNamespace); public ILGenerator GetProxy() { return (ILGenerator)ILGeneratorBuilder.GenerateProxy().MakeGenericType(GetType()).GetConstructors()[0].Invoke(new object[1] { this }); } public static Type GetProxyType() where TShim : ILGeneratorShim { return GetProxyType(typeof(TShim)); } public static Type GetProxyType(Type tShim) { return ProxyType.MakeGenericType(tShim); } } public static class ILGeneratorShimExt { private static readonly Dictionary _Emitters; private static readonly Dictionary _EmittersShim; static ILGeneratorShimExt() { _Emitters = new Dictionary(); _EmittersShim = new Dictionary(); MethodInfo[] methods = typeof(ILGenerator).GetMethods(); foreach (MethodInfo methodInfo in methods) { if (!(methodInfo.Name != "Emit")) { ParameterInfo[] parameters = methodInfo.GetParameters(); if (parameters.Length == 2 && !(parameters[0].ParameterType != typeof(OpCode))) { _Emitters[parameters[1].ParameterType] = methodInfo; } } } methods = typeof(ILGeneratorShim).GetMethods(); foreach (MethodInfo methodInfo2 in methods) { if (!(methodInfo2.Name != "Emit")) { ParameterInfo[] parameters2 = methodInfo2.GetParameters(); if (parameters2.Length == 2 && !(parameters2[0].ParameterType != typeof(OpCode))) { _EmittersShim[parameters2[1].ParameterType] = methodInfo2; } } } } public static ILGeneratorShim GetProxiedShim(this ILGenerator il) { return il.GetType().GetField("Target", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(il) as ILGeneratorShim; } public static T GetProxiedShim(this ILGenerator il) where T : ILGeneratorShim { return il.GetProxiedShim() as T; } public static object DynEmit(this ILGenerator il, OpCode opcode, object operand) { return il.DynEmit(new object[2] { opcode, operand }); } public static object DynEmit(this ILGenerator il, object[] emitArgs) { Type operandType = emitArgs[1].GetType(); object obj = ((object)il.GetProxiedShim()) ?? ((object)il); Dictionary dictionary = ((obj is ILGeneratorShim) ? _EmittersShim : _Emitters); if (!dictionary.TryGetValue(operandType, out var value)) { value = dictionary.FirstOrDefault((KeyValuePair kvp) => kvp.Key.IsAssignableFrom(operandType)).Value; } if (value == null) { throw new InvalidOperationException("Unexpected unemittable operand type " + operandType.FullName); } return value.Invoke(obj, emitArgs); } } } namespace MonoMod.Cil { public class ILContext : IDisposable { public delegate void Manipulator(ILContext il); internal List _Labels = new List(); public IILReferenceBag ReferenceBag = NopILReferenceBag.Instance; public MethodDefinition Method { get; private set; } public ILProcessor IL { get; private set; } public MethodBody Body => Method.Body; public ModuleDefinition Module => ((MemberReference)Method).Module; public Collection Instrs => Body.Instructions; public ReadOnlyCollection Labels => _Labels.AsReadOnly(); public bool IsReadOnly => IL == null; public event Action OnDispose; public ILContext(MethodDefinition method) { Method = method; IL = method.Body.GetILProcessor(); } public void Invoke(Manipulator manip) { //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_00a2: Unknown result type (might be due to invalid IL or missing references) if (IsReadOnly) { throw new InvalidOperationException(); } Enumerator enumerator = Instrs.GetEnumerator(); try { while (enumerator.MoveNext()) { Instruction current = enumerator.Current; object operand = current.Operand; Instruction val = (Instruction)((operand is Instruction) ? operand : null); if (val != null) { current.Operand = new ILLabel(this, val); } else if (current.Operand is Instruction[] source) { current.Operand = source.Select((Instruction t) => new ILLabel(this, t)).ToArray(); } } } finally { ((IDisposable)enumerator).Dispose(); } manip(this); if (IsReadOnly) { return; } enumerator = Instrs.GetEnumerator(); try { while (enumerator.MoveNext()) { Instruction current2 = enumerator.Current; if (current2.Operand is ILLabel iLLabel) { current2.Operand = iLLabel.Target; } else if (current2.Operand is ILLabel[] source2) { current2.Operand = source2.Select((ILLabel l) => l.Target).ToArray(); } } } finally { ((IDisposable)enumerator).Dispose(); } Method.FixShortLongOps(); } public void MakeReadOnly() { Method = null; IL = null; _Labels = new List(); } [Obsolete("Use new ILCursor(il).Goto(index)")] public ILCursor At(int index) { return new ILCursor(this).Goto(index); } [Obsolete("Use new ILCursor(il).Goto(index)")] public ILCursor At(ILLabel label) { return new ILCursor(this).GotoLabel(label); } [Obsolete("Use new ILCursor(il).Goto(index)")] public ILCursor At(Instruction instr) { return new ILCursor(this).Goto(instr); } public FieldReference Import(FieldInfo field) { return Module.ImportReference(field); } public MethodReference Import(MethodBase method) { return Module.ImportReference(method); } public TypeReference Import(Type type) { return Module.ImportReference(type); } public ILLabel DefineLabel() { return new ILLabel(this); } public ILLabel DefineLabel(Instruction target) { return new ILLabel(this, target); } public int IndexOf(Instruction instr) { int num = Instrs.IndexOf(instr); if (num != -1) { return num; } return Instrs.Count; } public IEnumerable GetIncomingLabels(Instruction instr) { return _Labels.Where((ILLabel l) => l.Target == instr); } public int AddReference(T t) { IILReferenceBag bag = ReferenceBag; int id = bag.Store(t); OnDispose += delegate { bag.Clear(id); }; return id; } public void Dispose() { this.OnDispose?.Invoke(); this.OnDispose = null; MakeReadOnly(); } public override string ToString() { //IL_0031: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Unknown result type (might be due to invalid IL or missing references) if (Method == null) { return "// ILContext: READONLY"; } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"// ILContext: {Method}"); Enumerator enumerator = Instrs.GetEnumerator(); try { while (enumerator.MoveNext()) { Instruction current = enumerator.Current; ToString(stringBuilder, current); } } finally { ((IDisposable)enumerator).Dispose(); } return stringBuilder.ToString(); } internal static StringBuilder ToString(StringBuilder builder, Instruction instr) { if (instr == null) { return builder; } object operand = instr.Operand; if (operand is ILLabel iLLabel) { instr.Operand = iLLabel.Target; } else if (operand is ILLabel[] source) { instr.Operand = source.Select((ILLabel l) => l.Target).ToArray(); } builder.AppendLine(((object)instr).ToString()); instr.Operand = operand; return builder; } } public enum MoveType { Before, AfterLabel, After } public enum SearchTarget { None, Next, Prev } public class ILCursor { private Instruction _next; private ILLabel[] _afterLabels; private SearchTarget _searchTarget; public ILContext Context { get; } public Instruction Next { get { return _next; } set { Goto(value); } } public Instruction Prev { get { if (Next != null) { return Next.Previous; } return Instrs[Instrs.Count - 1]; } set { Goto(value, MoveType.After); } } public Instruction Previous { get { return Prev; } set { Prev = value; } } public int Index { get { return Context.IndexOf(Next); } set { Goto(value); } } public SearchTarget SearchTarget { get { return _searchTarget; } set { if ((value == SearchTarget.Next && Next == null) || (value == SearchTarget.Prev && Prev == null)) { value = SearchTarget.None; } _searchTarget = value; } } public IEnumerable IncomingLabels => Context.GetIncomingLabels(Next); public MethodDefinition Method => Context.Method; public ILProcessor IL => Context.IL; public MethodBody Body => Context.Body; public ModuleDefinition Module => Context.Module; public Collection Instrs => Context.Instrs; public ILCursor(ILContext context) { Context = context; Index = 0; } public ILCursor(ILCursor c) { Context = c.Context; _next = c._next; _searchTarget = c._searchTarget; _afterLabels = c._afterLabels; } public ILCursor Clone() { return new ILCursor(this); } public bool IsBefore(Instruction instr) { return Index <= Context.IndexOf(instr); } public bool IsAfter(Instruction instr) { return Index > Context.IndexOf(instr); } public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"// ILCursor: {Method}, {Index}, {SearchTarget}"); ILContext.ToString(stringBuilder, Prev); ILContext.ToString(stringBuilder, Next); return stringBuilder.ToString(); } public ILCursor Goto(Instruction insn, MoveType moveType = MoveType.Before, bool setTarget = false) { if (moveType == MoveType.After) { _next = ((insn != null) ? insn.Next : null); } else { _next = insn; } if (setTarget) { _searchTarget = ((moveType != MoveType.After) ? SearchTarget.Next : SearchTarget.Prev); } else { _searchTarget = SearchTarget.None; } if (moveType == MoveType.AfterLabel) { MoveAfterLabels(); } else { MoveBeforeLabels(); } return this; } public ILCursor MoveAfterLabels() { _afterLabels = IncomingLabels.ToArray(); return this; } public ILCursor MoveBeforeLabels() { _afterLabels = null; return this; } public ILCursor Goto(int index, MoveType moveType = MoveType.Before, bool setTarget = false) { if (index < 0) { index += Instrs.Count; } return Goto((index == Instrs.Count) ? null : Instrs[index], moveType, setTarget); } public ILCursor GotoLabel(ILLabel label, MoveType moveType = MoveType.AfterLabel, bool setTarget = false) { return Goto(label.Target, moveType, setTarget); } public ILCursor GotoNext(MoveType moveType = MoveType.Before, params Func[] predicates) { if (!TryGotoNext(moveType, predicates)) { throw new KeyNotFoundException(); } return this; } public bool TryGotoNext(MoveType moveType = MoveType.Before, params Func[] predicates) { Collection instrs = Instrs; int i = Index; if (SearchTarget == SearchTarget.Next) { i++; } for (; i + predicates.Length <= instrs.Count; i++) { int num = 0; while (true) { if (num < predicates.Length) { Func obj = predicates[num]; if (obj != null && !obj(instrs[i + num])) { break; } num++; continue; } Goto((moveType == MoveType.After) ? (i + predicates.Length - 1) : i, moveType, setTarget: true); return true; } } return false; } public ILCursor GotoPrev(MoveType moveType = MoveType.Before, params Func[] predicates) { if (!TryGotoPrev(moveType, predicates)) { throw new KeyNotFoundException(); } return this; } public bool TryGotoPrev(MoveType moveType = MoveType.Before, params Func[] predicates) { Collection instrs = Instrs; int num = Index - 1; if (SearchTarget == SearchTarget.Prev) { num--; } for (num = Math.Min(num, instrs.Count - predicates.Length); num >= 0; num--) { int num2 = 0; while (true) { if (num2 < predicates.Length) { Func obj = predicates[num2]; if (obj != null && !obj(instrs[num + num2])) { break; } num2++; continue; } Goto((moveType == MoveType.After) ? (num + predicates.Length - 1) : num, moveType, setTarget: true); return true; } } return false; } public ILCursor GotoNext(params Func[] predicates) { return GotoNext(MoveType.Before, predicates); } public bool TryGotoNext(params Func[] predicates) { return TryGotoNext(MoveType.Before, predicates); } public ILCursor GotoPrev(params Func[] predicates) { return GotoPrev(MoveType.Before, predicates); } public bool TryGotoPrev(params Func[] predicates) { return TryGotoPrev(MoveType.Before, predicates); } public void FindNext(out ILCursor[] cursors, params Func[] predicates) { if (!TryFindNext(out cursors, predicates)) { throw new KeyNotFoundException(); } } public bool TryFindNext(out ILCursor[] cursors, params Func[] predicates) { cursors = new ILCursor[predicates.Length]; ILCursor iLCursor = this; for (int i = 0; i < predicates.Length; i++) { iLCursor = iLCursor.Clone(); if (!iLCursor.TryGotoNext(predicates[i])) { return false; } cursors[i] = iLCursor; } return true; } public void FindPrev(out ILCursor[] cursors, params Func[] predicates) { if (!TryFindPrev(out cursors, predicates)) { throw new KeyNotFoundException(); } } public bool TryFindPrev(out ILCursor[] cursors, params Func[] predicates) { cursors = new ILCursor[predicates.Length]; ILCursor iLCursor = this; for (int num = predicates.Length - 1; num >= 0; num--) { iLCursor = iLCursor.Clone(); if (!iLCursor.TryGotoPrev(predicates[num])) { return false; } cursors[num] = iLCursor; } return true; } public void MarkLabel(ILLabel label) { if (label == null) { label = new ILLabel(Context); } label.Target = Next; if (_afterLabels != null) { Array.Resize(ref _afterLabels, _afterLabels.Length + 1); _afterLabels[_afterLabels.Length - 1] = label; } else { _afterLabels = new ILLabel[1] { label }; } } public ILLabel MarkLabel() { ILLabel iLLabel = DefineLabel(); MarkLabel(iLLabel); return iLLabel; } public ILLabel DefineLabel() { return Context.DefineLabel(); } private ILCursor _Insert(Instruction instr) { Instrs.Insert(Index, instr); _Retarget(instr, MoveType.After); return this; } public ILCursor Remove() { int index = Index; _Retarget(Next.Next, MoveType.Before); Instrs.RemoveAt(index); return this; } public ILCursor RemoveRange(int num) { int index = Index; _Retarget(Instrs[index + num], MoveType.Before); while (num-- > 0) { Instrs.RemoveAt(index); } return this; } private void _Retarget(Instruction next, MoveType moveType) { if (_afterLabels != null) { ILLabel[] afterLabels = _afterLabels; for (int i = 0; i < afterLabels.Length; i++) { afterLabels[i].Target = next; } } Goto(next, moveType); } public ILCursor Emit(OpCode opcode, ParameterDefinition parameter) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, parameter)); } public ILCursor Emit(OpCode opcode, VariableDefinition variable) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, variable)); } public ILCursor Emit(OpCode opcode, Instruction[] targets) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, targets)); } public ILCursor Emit(OpCode opcode, Instruction target) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, target)); } public ILCursor Emit(OpCode opcode, double value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, value)); } public ILCursor Emit(OpCode opcode, float value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, value)); } public ILCursor Emit(OpCode opcode, long value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, value)); } public ILCursor Emit(OpCode opcode, sbyte value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, value)); } public ILCursor Emit(OpCode opcode, byte value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, value)); } public ILCursor Emit(OpCode opcode, string value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, value)); } public ILCursor Emit(OpCode opcode, FieldReference field) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, field)); } public ILCursor Emit(OpCode opcode, CallSite site) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, site)); } public ILCursor Emit(OpCode opcode, TypeReference type) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, type)); } public ILCursor Emit(OpCode opcode) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode)); } public ILCursor Emit(OpCode opcode, int value) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, value)); } public ILCursor Emit(OpCode opcode, MethodReference method) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, method)); } public ILCursor Emit(OpCode opcode, FieldInfo field) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, field)); } public ILCursor Emit(OpCode opcode, MethodBase method) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, method)); } public ILCursor Emit(OpCode opcode, Type type) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, type)); } public ILCursor Emit(OpCode opcode, object operand) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, operand)); } public ILCursor Emit(OpCode opcode, string memberName) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) return _Insert(IL.Create(opcode, typeof(T).GetMember(memberName, (BindingFlags)(-1)).FirstOrDefault())); } public int AddReference(T t) { return Context.AddReference(t); } public void EmitGetReference(int id) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) Emit(OpCodes.Ldc_I4, id); Emit(OpCodes.Call, (MethodBase)Context.ReferenceBag.GetGetter()); } public int EmitReference(T t) { int num = AddReference(t); EmitGetReference(num); return num; } public int EmitDelegate(T cb) where T : Delegate { //IL_0083: Unknown result type (might be due to invalid IL or missing references) //IL_0075: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) if (cb.GetInvocationList().Length == 1 && cb.Target == null) { Emit(OpCodes.Call, (MethodBase)cb.Method); return -1; } int result = EmitReference(cb); MethodInfo method = typeof(T).GetMethod("Invoke"); MethodInfo delegateInvoker = Context.ReferenceBag.GetDelegateInvoker(); if (delegateInvoker != null) { AddReference(delegateInvoker); Emit(OpCodes.Call, (MethodBase)delegateInvoker); return result; } Emit(OpCodes.Callvirt, (MethodBase)method); return result; } } public sealed class ILLabel { private readonly ILContext Context; public Instruction Target; public IEnumerable Branches => ((IEnumerable)Context.Instrs).Where((Instruction i) => i.Operand == this); internal ILLabel(ILContext context) { Context = context; Context._Labels.Add(this); } internal ILLabel(ILContext context, Instruction target) : this(context) { Target = target; } } public static class ILPatternMatchingExt { public static bool Match(this Instruction instr, OpCode opcode) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) return instr.OpCode == opcode; } public static bool Match(this Instruction instr, OpCode opcode, T value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) if (instr.Match(opcode, out var value2)) { return value2?.Equals(value) ?? (value == null); } return false; } public static bool Match(this Instruction instr, OpCode opcode, out T value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == opcode) { value = (T)instr.Operand; return true; } value = default(T); return false; } public static bool MatchNop(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Nop) { return true; } return false; } public static bool MatchBreak(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Break) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdarg instead.", true)] public static bool MatchLdarg0(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldarg_0) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdarg instead.", true)] public static bool MatchLdarg1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldarg_1) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdarg instead.", true)] public static bool MatchLdarg2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldarg_2) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdarg instead.", true)] public static bool MatchLdarg3(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldarg_3) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdloc instead.", true)] public static bool MatchLdloc0(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldloc_0) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdloc instead.", true)] public static bool MatchLdloc1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldloc_1) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdloc instead.", true)] public static bool MatchLdloc2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldloc_2) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchLdloc instead.", true)] public static bool MatchLdloc3(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldloc_3) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchStloc instead.", true)] public static bool MatchStloc0(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stloc_0) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchStloc instead.", true)] public static bool MatchStloc1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stloc_1) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchStloc instead.", true)] public static bool MatchStloc2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stloc_2) { return true; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Use MatchStloc instead.", true)] public static bool MatchStloc3(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stloc_3) { return true; } return false; } public static bool MatchLdnull(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldnull) { return true; } return false; } public static bool MatchLdcI4(this Instruction instr, int value) { if (instr.MatchLdcI4(out var value2)) { return value2 == value; } return false; } public static bool MatchLdcI4(this Instruction instr, out int value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_0048: Unknown result type (might be due to invalid IL or missing references) //IL_005a: Unknown result type (might be due to invalid IL or missing references) //IL_005f: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_0088: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Unknown result type (might be due to invalid IL or missing references) //IL_009f: Unknown result type (might be due to invalid IL or missing references) //IL_00a4: Unknown result type (might be due to invalid IL or missing references) //IL_00b6: Unknown result type (might be due to invalid IL or missing references) //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_00cd: Unknown result type (might be due to invalid IL or missing references) //IL_00d2: Unknown result type (might be due to invalid IL or missing references) //IL_00e4: Unknown result type (might be due to invalid IL or missing references) //IL_00e9: Unknown result type (might be due to invalid IL or missing references) //IL_00fb: Unknown result type (might be due to invalid IL or missing references) //IL_0100: Unknown result type (might be due to invalid IL or missing references) //IL_0112: Unknown result type (might be due to invalid IL or missing references) //IL_0117: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldc_I4) { value = (int)instr.Operand; return true; } if (instr.OpCode == OpCodes.Ldc_I4_S) { value = (sbyte)instr.Operand; return true; } if (instr.OpCode == OpCodes.Ldc_I4_M1) { value = -1; return true; } if (instr.OpCode == OpCodes.Ldc_I4_0) { value = 0; return true; } if (instr.OpCode == OpCodes.Ldc_I4_1) { value = 1; return true; } if (instr.OpCode == OpCodes.Ldc_I4_2) { value = 2; return true; } if (instr.OpCode == OpCodes.Ldc_I4_3) { value = 3; return true; } if (instr.OpCode == OpCodes.Ldc_I4_4) { value = 4; return true; } if (instr.OpCode == OpCodes.Ldc_I4_5) { value = 5; return true; } if (instr.OpCode == OpCodes.Ldc_I4_6) { value = 6; return true; } if (instr.OpCode == OpCodes.Ldc_I4_7) { value = 7; return true; } if (instr.OpCode == OpCodes.Ldc_I4_8) { value = 8; return true; } value = 0; return false; } public static bool MatchLdcI8(this Instruction instr, long value) { if (instr.MatchLdcI8(out var value2)) { return value2 == value; } return false; } public static bool MatchLdcI8(this Instruction instr, out long value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldc_I8) { value = (long)instr.Operand; return true; } value = 0L; return false; } public static bool MatchLdcR4(this Instruction instr, float value) { if (instr.MatchLdcR4(out var value2)) { return value2 == value; } return false; } public static bool MatchLdcR4(this Instruction instr, out float value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldc_R4) { value = (float)instr.Operand; return true; } value = 0f; return false; } public static bool MatchLdcR8(this Instruction instr, double value) { if (instr.MatchLdcR8(out var value2)) { return value2 == value; } return false; } public static bool MatchLdcR8(this Instruction instr, out double value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldc_R8) { value = (double)instr.Operand; return true; } value = 0.0; return false; } public static bool MatchDup(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Dup) { return true; } return false; } public static bool MatchPop(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Pop) { return true; } return false; } public static bool MatchJmp(this Instruction instr, string typeFullName, string name) { if (instr.MatchJmp(out var value)) { return value.Is(typeFullName, name); } return false; } public static bool MatchJmp(this Instruction instr, string name) { if (instr.MatchJmp(out var value)) { return value.Is(typeof(T), name); } return false; } public static bool MatchJmp(this Instruction instr, Type type, string name) { if (instr.MatchJmp(out var value)) { return value.Is(type, name); } return false; } public static bool MatchJmp(this Instruction instr, MethodBase value) { if (instr.MatchJmp(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchJmp(this Instruction instr, MethodReference value) { if (instr.MatchJmp(out var value2)) { return value2 == value; } return false; } public static bool MatchJmp(this Instruction instr, out MethodReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Jmp) { object operand = instr.Operand; value = (MethodReference)((operand is MethodReference) ? operand : null); return true; } value = null; return false; } public static bool MatchCall(this Instruction instr, string typeFullName, string name) { if (instr.MatchCall(out var value)) { return value.Is(typeFullName, name); } return false; } public static bool MatchCall(this Instruction instr, string name) { if (instr.MatchCall(out var value)) { return value.Is(typeof(T), name); } return false; } public static bool MatchCall(this Instruction instr, Type type, string name) { if (instr.MatchCall(out var value)) { return value.Is(type, name); } return false; } public static bool MatchCall(this Instruction instr, MethodBase value) { if (instr.MatchCall(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchCall(this Instruction instr, MethodReference value) { if (instr.MatchCall(out var value2)) { return value2 == value; } return false; } public static bool MatchCall(this Instruction instr, out MethodReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Call) { object operand = instr.Operand; value = (MethodReference)((operand is MethodReference) ? operand : null); return true; } value = null; return false; } public static bool MatchCallvirt(this Instruction instr, string typeFullName, string name) { if (instr.MatchCallvirt(out var value)) { return value.Is(typeFullName, name); } return false; } public static bool MatchCallvirt(this Instruction instr, string name) { if (instr.MatchCallvirt(out var value)) { return value.Is(typeof(T), name); } return false; } public static bool MatchCallvirt(this Instruction instr, Type type, string name) { if (instr.MatchCallvirt(out var value)) { return value.Is(type, name); } return false; } public static bool MatchCallvirt(this Instruction instr, MethodBase value) { if (instr.MatchCallvirt(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchCallvirt(this Instruction instr, MethodReference value) { if (instr.MatchCallvirt(out var value2)) { return value2 == value; } return false; } public static bool MatchCallvirt(this Instruction instr, out MethodReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Callvirt) { object operand = instr.Operand; value = (MethodReference)((operand is MethodReference) ? operand : null); return true; } value = null; return false; } public static bool MatchCallOrCallvirt(this Instruction instr, string typeFullName, string name) { if (instr.MatchCallOrCallvirt(out var value)) { return value.Is(typeFullName, name); } return false; } public static bool MatchCallOrCallvirt(this Instruction instr, string name) { if (instr.MatchCallOrCallvirt(out var value)) { return value.Is(typeof(T), name); } return false; } public static bool MatchCallOrCallvirt(this Instruction instr, Type type, string name) { if (instr.MatchCallOrCallvirt(out var value)) { return value.Is(type, name); } return false; } public static bool MatchCallOrCallvirt(this Instruction instr, MethodBase value) { if (instr.MatchCallOrCallvirt(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchCallOrCallvirt(this Instruction instr, MethodReference value) { if (instr.MatchCallOrCallvirt(out var value2)) { return value2 == value; } return false; } public static bool MatchCallOrCallvirt(this Instruction instr, out MethodReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Call || instr.OpCode == OpCodes.Callvirt) { object operand = instr.Operand; value = (MethodReference)((operand is MethodReference) ? operand : null); return true; } value = null; return false; } public static bool MatchCalli(this Instruction instr, IMethodSignature value) { if (instr.MatchCalli(out var value2)) { return value2 == value; } return false; } public static bool MatchCalli(this Instruction instr, out IMethodSignature value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Calli) { value = (IMethodSignature)instr.Operand; return true; } value = null; return false; } public static bool MatchRet(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ret) { return true; } return false; } public static bool MatchBr(this Instruction instr, ILLabel value) { if (instr.MatchBr(out var value2)) { return value2 == value; } return false; } public static bool MatchBr(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Br || instr.OpCode == OpCodes.Br_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBrfalse(this Instruction instr, ILLabel value) { if (instr.MatchBrfalse(out var value2)) { return value2 == value; } return false; } public static bool MatchBrfalse(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Brfalse || instr.OpCode == OpCodes.Brfalse_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBrtrue(this Instruction instr, ILLabel value) { if (instr.MatchBrtrue(out var value2)) { return value2 == value; } return false; } public static bool MatchBrtrue(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Brtrue || instr.OpCode == OpCodes.Brtrue_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBeq(this Instruction instr, ILLabel value) { if (instr.MatchBeq(out var value2)) { return value2 == value; } return false; } public static bool MatchBeq(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Beq || instr.OpCode == OpCodes.Beq_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBge(this Instruction instr, ILLabel value) { if (instr.MatchBge(out var value2)) { return value2 == value; } return false; } public static bool MatchBge(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Bge || instr.OpCode == OpCodes.Bge_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBgt(this Instruction instr, ILLabel value) { if (instr.MatchBgt(out var value2)) { return value2 == value; } return false; } public static bool MatchBgt(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Bgt || instr.OpCode == OpCodes.Bgt_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBle(this Instruction instr, ILLabel value) { if (instr.MatchBle(out var value2)) { return value2 == value; } return false; } public static bool MatchBle(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ble || instr.OpCode == OpCodes.Ble_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBlt(this Instruction instr, ILLabel value) { if (instr.MatchBlt(out var value2)) { return value2 == value; } return false; } public static bool MatchBlt(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Blt || instr.OpCode == OpCodes.Blt_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBneUn(this Instruction instr, ILLabel value) { if (instr.MatchBneUn(out var value2)) { return value2 == value; } return false; } public static bool MatchBneUn(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Bne_Un || instr.OpCode == OpCodes.Bne_Un_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBgeUn(this Instruction instr, ILLabel value) { if (instr.MatchBgeUn(out var value2)) { return value2 == value; } return false; } public static bool MatchBgeUn(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Bge_Un || instr.OpCode == OpCodes.Bge_Un_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBgtUn(this Instruction instr, ILLabel value) { if (instr.MatchBgtUn(out var value2)) { return value2 == value; } return false; } public static bool MatchBgtUn(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Bgt_Un || instr.OpCode == OpCodes.Bgt_Un_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBleUn(this Instruction instr, ILLabel value) { if (instr.MatchBleUn(out var value2)) { return value2 == value; } return false; } public static bool MatchBleUn(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ble_Un || instr.OpCode == OpCodes.Ble_Un_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchBltUn(this Instruction instr, ILLabel value) { if (instr.MatchBltUn(out var value2)) { return value2 == value; } return false; } public static bool MatchBltUn(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Blt_Un || instr.OpCode == OpCodes.Blt_Un_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchSwitch(this Instruction instr, ILLabel[] value) { if (instr.MatchSwitch(out var value2)) { return value2.SequenceEqual(value); } return false; } public static bool MatchSwitch(this Instruction instr, out ILLabel[] value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Switch) { value = (ILLabel[])instr.Operand; return true; } value = null; return false; } public static bool MatchLdindI1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_I1) { return true; } return false; } public static bool MatchLdindU1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_U1) { return true; } return false; } public static bool MatchLdindI2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_I2) { return true; } return false; } public static bool MatchLdindU2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_U2) { return true; } return false; } public static bool MatchLdindI4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_I4) { return true; } return false; } public static bool MatchLdindU4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_U4) { return true; } return false; } public static bool MatchLdindI8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_I8) { return true; } return false; } public static bool MatchLdindI(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_I) { return true; } return false; } public static bool MatchLdindR4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_R4) { return true; } return false; } public static bool MatchLdindR8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_R8) { return true; } return false; } public static bool MatchLdindRef(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldind_Ref) { return true; } return false; } public static bool MatchStindRef(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_Ref) { return true; } return false; } public static bool MatchStindI1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_I1) { return true; } return false; } public static bool MatchStindI2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_I2) { return true; } return false; } public static bool MatchStindI4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_I4) { return true; } return false; } public static bool MatchStindI8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_I8) { return true; } return false; } public static bool MatchStindR4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_R4) { return true; } return false; } public static bool MatchStindR8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_R8) { return true; } return false; } public static bool MatchAdd(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Add) { return true; } return false; } public static bool MatchSub(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Sub) { return true; } return false; } public static bool MatchMul(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Mul) { return true; } return false; } public static bool MatchDiv(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Div) { return true; } return false; } public static bool MatchDivUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Div_Un) { return true; } return false; } public static bool MatchRem(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Rem) { return true; } return false; } public static bool MatchRemUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Rem_Un) { return true; } return false; } public static bool MatchAnd(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.And) { return true; } return false; } public static bool MatchOr(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Or) { return true; } return false; } public static bool MatchXor(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Xor) { return true; } return false; } public static bool MatchShl(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Shl) { return true; } return false; } public static bool MatchShr(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Shr) { return true; } return false; } public static bool MatchShrUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Shr_Un) { return true; } return false; } public static bool MatchNeg(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Neg) { return true; } return false; } public static bool MatchNot(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Not) { return true; } return false; } public static bool MatchConvI1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_I1) { return true; } return false; } public static bool MatchConvI2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_I2) { return true; } return false; } public static bool MatchConvI4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_I4) { return true; } return false; } public static bool MatchConvI8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_I8) { return true; } return false; } public static bool MatchConvR4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_R4) { return true; } return false; } public static bool MatchConvR8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_R8) { return true; } return false; } public static bool MatchConvU4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_U4) { return true; } return false; } public static bool MatchConvU8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_U8) { return true; } return false; } public static bool MatchCpobj(this Instruction instr, string fullName) { if (instr.MatchCpobj(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchCpobj(this Instruction instr) { if (instr.MatchCpobj(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchCpobj(this Instruction instr, Type value) { if (instr.MatchCpobj(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchCpobj(this Instruction instr, TypeReference value) { if (instr.MatchCpobj(out var value2)) { return value2 == value; } return false; } public static bool MatchCpobj(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Cpobj) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdobj(this Instruction instr, string fullName) { if (instr.MatchLdobj(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchLdobj(this Instruction instr) { if (instr.MatchLdobj(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchLdobj(this Instruction instr, Type value) { if (instr.MatchLdobj(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdobj(this Instruction instr, TypeReference value) { if (instr.MatchLdobj(out var value2)) { return value2 == value; } return false; } public static bool MatchLdobj(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldobj) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdstr(this Instruction instr, string value) { if (instr.MatchLdstr(out var value2)) { return value2 == value; } return false; } public static bool MatchLdstr(this Instruction instr, out string value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldstr) { value = (string)instr.Operand; return true; } value = null; return false; } public static bool MatchNewobj(this Instruction instr, string typeFullName) { if (instr.MatchNewobj(out var value)) { return ((MemberReference)(object)((MemberReference)value).DeclaringType).Is(typeFullName); } return false; } public static bool MatchNewobj(this Instruction instr) { if (instr.MatchNewobj(out var value)) { return ((MemberReference)(object)((MemberReference)value).DeclaringType).Is(typeof(T)); } return false; } public static bool MatchNewobj(this Instruction instr, Type type) { if (instr.MatchNewobj(out var value)) { return ((MemberReference)(object)((MemberReference)value).DeclaringType).Is(type); } return false; } public static bool MatchNewobj(this Instruction instr, MethodBase value) { if (instr.MatchNewobj(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchNewobj(this Instruction instr, MethodReference value) { if (instr.MatchNewobj(out var value2)) { return value2 == value; } return false; } public static bool MatchNewobj(this Instruction instr, out MethodReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Newobj) { object operand = instr.Operand; value = (MethodReference)((operand is MethodReference) ? operand : null); return true; } value = null; return false; } public static bool MatchCastclass(this Instruction instr, string fullName) { if (instr.MatchCastclass(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchCastclass(this Instruction instr) { if (instr.MatchCastclass(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchCastclass(this Instruction instr, Type value) { if (instr.MatchCastclass(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchCastclass(this Instruction instr, TypeReference value) { if (instr.MatchCastclass(out var value2)) { return value2 == value; } return false; } public static bool MatchCastclass(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Castclass) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchIsinst(this Instruction instr, string fullName) { if (instr.MatchIsinst(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchIsinst(this Instruction instr) { if (instr.MatchIsinst(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchIsinst(this Instruction instr, Type value) { if (instr.MatchIsinst(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchIsinst(this Instruction instr, TypeReference value) { if (instr.MatchIsinst(out var value2)) { return value2 == value; } return false; } public static bool MatchIsinst(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Isinst) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchConvRUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_R_Un) { return true; } return false; } public static bool MatchUnbox(this Instruction instr, string fullName) { if (instr.MatchUnbox(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchUnbox(this Instruction instr) { if (instr.MatchUnbox(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchUnbox(this Instruction instr, Type value) { if (instr.MatchUnbox(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchUnbox(this Instruction instr, TypeReference value) { if (instr.MatchUnbox(out var value2)) { return value2 == value; } return false; } public static bool MatchUnbox(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Unbox) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchThrow(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Throw) { return true; } return false; } public static bool MatchLdfld(this Instruction instr, string typeFullName, string name) { if (instr.MatchLdfld(out var value)) { return ((MemberReference)(object)value).Is(typeFullName, name); } return false; } public static bool MatchLdfld(this Instruction instr, string name) { if (instr.MatchLdfld(out var value)) { return ((MemberReference)(object)value).Is(typeof(T), name); } return false; } public static bool MatchLdfld(this Instruction instr, Type type, string name) { if (instr.MatchLdfld(out var value)) { return ((MemberReference)(object)value).Is(type, name); } return false; } public static bool MatchLdfld(this Instruction instr, FieldInfo value) { if (instr.MatchLdfld(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdfld(this Instruction instr, FieldReference value) { if (instr.MatchLdfld(out var value2)) { return value2 == value; } return false; } public static bool MatchLdfld(this Instruction instr, out FieldReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldfld) { value = (FieldReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdflda(this Instruction instr, string typeFullName, string name) { if (instr.MatchLdflda(out var value)) { return ((MemberReference)(object)value).Is(typeFullName, name); } return false; } public static bool MatchLdflda(this Instruction instr, string name) { if (instr.MatchLdflda(out var value)) { return ((MemberReference)(object)value).Is(typeof(T), name); } return false; } public static bool MatchLdflda(this Instruction instr, Type type, string name) { if (instr.MatchLdflda(out var value)) { return ((MemberReference)(object)value).Is(type, name); } return false; } public static bool MatchLdflda(this Instruction instr, FieldInfo value) { if (instr.MatchLdflda(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdflda(this Instruction instr, FieldReference value) { if (instr.MatchLdflda(out var value2)) { return value2 == value; } return false; } public static bool MatchLdflda(this Instruction instr, out FieldReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldflda) { value = (FieldReference)instr.Operand; return true; } value = null; return false; } public static bool MatchStfld(this Instruction instr, string typeFullName, string name) { if (instr.MatchStfld(out var value)) { return ((MemberReference)(object)value).Is(typeFullName, name); } return false; } public static bool MatchStfld(this Instruction instr, string name) { if (instr.MatchStfld(out var value)) { return ((MemberReference)(object)value).Is(typeof(T), name); } return false; } public static bool MatchStfld(this Instruction instr, Type type, string name) { if (instr.MatchStfld(out var value)) { return ((MemberReference)(object)value).Is(type, name); } return false; } public static bool MatchStfld(this Instruction instr, FieldInfo value) { if (instr.MatchStfld(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchStfld(this Instruction instr, FieldReference value) { if (instr.MatchStfld(out var value2)) { return value2 == value; } return false; } public static bool MatchStfld(this Instruction instr, out FieldReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Stfld) { value = (FieldReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdsfld(this Instruction instr, string typeFullName, string name) { if (instr.MatchLdsfld(out var value)) { return ((MemberReference)(object)value).Is(typeFullName, name); } return false; } public static bool MatchLdsfld(this Instruction instr, string name) { if (instr.MatchLdsfld(out var value)) { return ((MemberReference)(object)value).Is(typeof(T), name); } return false; } public static bool MatchLdsfld(this Instruction instr, Type type, string name) { if (instr.MatchLdsfld(out var value)) { return ((MemberReference)(object)value).Is(type, name); } return false; } public static bool MatchLdsfld(this Instruction instr, FieldInfo value) { if (instr.MatchLdsfld(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdsfld(this Instruction instr, FieldReference value) { if (instr.MatchLdsfld(out var value2)) { return value2 == value; } return false; } public static bool MatchLdsfld(this Instruction instr, out FieldReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldsfld) { value = (FieldReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdsflda(this Instruction instr, string typeFullName, string name) { if (instr.MatchLdsflda(out var value)) { return ((MemberReference)(object)value).Is(typeFullName, name); } return false; } public static bool MatchLdsflda(this Instruction instr, string name) { if (instr.MatchLdsflda(out var value)) { return ((MemberReference)(object)value).Is(typeof(T), name); } return false; } public static bool MatchLdsflda(this Instruction instr, Type type, string name) { if (instr.MatchLdsflda(out var value)) { return ((MemberReference)(object)value).Is(type, name); } return false; } public static bool MatchLdsflda(this Instruction instr, FieldInfo value) { if (instr.MatchLdsflda(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdsflda(this Instruction instr, FieldReference value) { if (instr.MatchLdsflda(out var value2)) { return value2 == value; } return false; } public static bool MatchLdsflda(this Instruction instr, out FieldReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldsflda) { value = (FieldReference)instr.Operand; return true; } value = null; return false; } public static bool MatchStsfld(this Instruction instr, string typeFullName, string name) { if (instr.MatchStsfld(out var value)) { return ((MemberReference)(object)value).Is(typeFullName, name); } return false; } public static bool MatchStsfld(this Instruction instr, string name) { if (instr.MatchStsfld(out var value)) { return ((MemberReference)(object)value).Is(typeof(T), name); } return false; } public static bool MatchStsfld(this Instruction instr, Type type, string name) { if (instr.MatchStsfld(out var value)) { return ((MemberReference)(object)value).Is(type, name); } return false; } public static bool MatchStsfld(this Instruction instr, FieldInfo value) { if (instr.MatchStsfld(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchStsfld(this Instruction instr, FieldReference value) { if (instr.MatchStsfld(out var value2)) { return value2 == value; } return false; } public static bool MatchStsfld(this Instruction instr, out FieldReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Stsfld) { value = (FieldReference)instr.Operand; return true; } value = null; return false; } public static bool MatchStobj(this Instruction instr, string fullName) { if (instr.MatchStobj(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchStobj(this Instruction instr) { if (instr.MatchStobj(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchStobj(this Instruction instr, Type value) { if (instr.MatchStobj(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchStobj(this Instruction instr, TypeReference value) { if (instr.MatchStobj(out var value2)) { return value2 == value; } return false; } public static bool MatchStobj(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Stobj) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchConvOvfI1Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I1_Un) { return true; } return false; } public static bool MatchConvOvfI2Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I2_Un) { return true; } return false; } public static bool MatchConvOvfI4Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I4_Un) { return true; } return false; } public static bool MatchConvOvfI8Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I8_Un) { return true; } return false; } public static bool MatchConvOvfU1Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U1_Un) { return true; } return false; } public static bool MatchConvOvfU2Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U2_Un) { return true; } return false; } public static bool MatchConvOvfU4Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U4_Un) { return true; } return false; } public static bool MatchConvOvfU8Un(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U8_Un) { return true; } return false; } public static bool MatchConvOvfIUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I_Un) { return true; } return false; } public static bool MatchConvOvfUUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U_Un) { return true; } return false; } public static bool MatchBox(this Instruction instr, string fullName) { if (instr.MatchBox(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchBox(this Instruction instr) { if (instr.MatchBox(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchBox(this Instruction instr, Type value) { if (instr.MatchBox(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchBox(this Instruction instr, TypeReference value) { if (instr.MatchBox(out var value2)) { return value2 == value; } return false; } public static bool MatchBox(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Box) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchNewarr(this Instruction instr, string fullName) { if (instr.MatchNewarr(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchNewarr(this Instruction instr) { if (instr.MatchNewarr(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchNewarr(this Instruction instr, Type value) { if (instr.MatchNewarr(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchNewarr(this Instruction instr, TypeReference value) { if (instr.MatchNewarr(out var value2)) { return value2 == value; } return false; } public static bool MatchNewarr(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Newarr) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdlen(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldlen) { return true; } return false; } public static bool MatchLdelema(this Instruction instr, string fullName) { if (instr.MatchLdelema(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchLdelema(this Instruction instr) { if (instr.MatchLdelema(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchLdelema(this Instruction instr, Type value) { if (instr.MatchLdelema(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdelema(this Instruction instr, TypeReference value) { if (instr.MatchLdelema(out var value2)) { return value2 == value; } return false; } public static bool MatchLdelema(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldelema) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdelemI1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_I1) { return true; } return false; } public static bool MatchLdelemU1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_U1) { return true; } return false; } public static bool MatchLdelemI2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_I2) { return true; } return false; } public static bool MatchLdelemU2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_U2) { return true; } return false; } public static bool MatchLdelemI4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_I4) { return true; } return false; } public static bool MatchLdelemU4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_U4) { return true; } return false; } public static bool MatchLdelemI8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_I8) { return true; } return false; } public static bool MatchLdelemI(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_I) { return true; } return false; } public static bool MatchLdelemR4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_R4) { return true; } return false; } public static bool MatchLdelemR8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_R8) { return true; } return false; } public static bool MatchLdelemRef(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldelem_Ref) { return true; } return false; } public static bool MatchStelemI(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_I) { return true; } return false; } public static bool MatchStelemI1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_I1) { return true; } return false; } public static bool MatchStelemI2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_I2) { return true; } return false; } public static bool MatchStelemI4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_I4) { return true; } return false; } public static bool MatchStelemI8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_I8) { return true; } return false; } public static bool MatchStelemR4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_R4) { return true; } return false; } public static bool MatchStelemR8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_R8) { return true; } return false; } public static bool MatchStelemRef(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stelem_Ref) { return true; } return false; } public static bool MatchLdelemAny(this Instruction instr, string fullName) { if (instr.MatchLdelemAny(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchLdelemAny(this Instruction instr) { if (instr.MatchLdelemAny(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchLdelemAny(this Instruction instr, Type value) { if (instr.MatchLdelemAny(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdelemAny(this Instruction instr, TypeReference value) { if (instr.MatchLdelemAny(out var value2)) { return value2 == value; } return false; } public static bool MatchLdelemAny(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldelem_Any) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchStelemAny(this Instruction instr, string fullName) { if (instr.MatchStelemAny(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchStelemAny(this Instruction instr) { if (instr.MatchStelemAny(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchStelemAny(this Instruction instr, Type value) { if (instr.MatchStelemAny(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchStelemAny(this Instruction instr, TypeReference value) { if (instr.MatchStelemAny(out var value2)) { return value2 == value; } return false; } public static bool MatchStelemAny(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Stelem_Any) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchUnboxAny(this Instruction instr, string fullName) { if (instr.MatchUnboxAny(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchUnboxAny(this Instruction instr) { if (instr.MatchUnboxAny(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchUnboxAny(this Instruction instr, Type value) { if (instr.MatchUnboxAny(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchUnboxAny(this Instruction instr, TypeReference value) { if (instr.MatchUnboxAny(out var value2)) { return value2 == value; } return false; } public static bool MatchUnboxAny(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Unbox_Any) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchConvOvfI1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I1) { return true; } return false; } public static bool MatchConvOvfU1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U1) { return true; } return false; } public static bool MatchConvOvfI2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I2) { return true; } return false; } public static bool MatchConvOvfU2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U2) { return true; } return false; } public static bool MatchConvOvfI4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I4) { return true; } return false; } public static bool MatchConvOvfU4(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U4) { return true; } return false; } public static bool MatchConvOvfI8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I8) { return true; } return false; } public static bool MatchConvOvfU8(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U8) { return true; } return false; } public static bool MatchRefanyval(this Instruction instr, string fullName) { if (instr.MatchRefanyval(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchRefanyval(this Instruction instr) { if (instr.MatchRefanyval(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchRefanyval(this Instruction instr, Type value) { if (instr.MatchRefanyval(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchRefanyval(this Instruction instr, TypeReference value) { if (instr.MatchRefanyval(out var value2)) { return value2 == value; } return false; } public static bool MatchRefanyval(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Refanyval) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchCkfinite(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ckfinite) { return true; } return false; } public static bool MatchMkrefany(this Instruction instr, string fullName) { if (instr.MatchMkrefany(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchMkrefany(this Instruction instr) { if (instr.MatchMkrefany(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchMkrefany(this Instruction instr, Type value) { if (instr.MatchMkrefany(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchMkrefany(this Instruction instr, TypeReference value) { if (instr.MatchMkrefany(out var value2)) { return value2 == value; } return false; } public static bool MatchMkrefany(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Mkrefany) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchLdtoken(this Instruction instr, IMetadataTokenProvider value) { if (instr.MatchLdtoken(out var value2)) { return value2 == value; } return false; } public static bool MatchLdtoken(this Instruction instr, out IMetadataTokenProvider value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Ldtoken) { value = (IMetadataTokenProvider)instr.Operand; return true; } value = null; return false; } public static bool MatchConvU2(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_U2) { return true; } return false; } public static bool MatchConvU1(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_U1) { return true; } return false; } public static bool MatchConvI(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_I) { return true; } return false; } public static bool MatchConv_OvfI(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_I) { return true; } return false; } public static bool MatchConv_OvfU(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_Ovf_U) { return true; } return false; } public static bool MatchAddOvf(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Add_Ovf) { return true; } return false; } public static bool MatchAdd_OvfUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Add_Ovf_Un) { return true; } return false; } public static bool MatchMulOvf(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Mul_Ovf) { return true; } return false; } public static bool MatchMulOvfUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Mul_Ovf_Un) { return true; } return false; } public static bool MatchSubOvf(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Sub_Ovf) { return true; } return false; } public static bool MatchSubOvfUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Sub_Ovf_Un) { return true; } return false; } public static bool MatchEndfinally(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Endfinally) { return true; } return false; } public static bool MatchLeave(this Instruction instr, ILLabel value) { if (instr.MatchLeave(out var value2)) { return value2 == value; } return false; } public static bool MatchLeave(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Leave) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchLeaveS(this Instruction instr, ILLabel value) { if (instr.MatchLeaveS(out var value2)) { return value2 == value; } return false; } public static bool MatchLeaveS(this Instruction instr, out ILLabel value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Leave_S) { value = (ILLabel)instr.Operand; return true; } value = null; return false; } public static bool MatchStindI(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stind_I) { return true; } return false; } public static bool MatchConvU(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Conv_U) { return true; } return false; } public static bool MatchArglist(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Arglist) { return true; } return false; } public static bool MatchCeq(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ceq) { return true; } return false; } public static bool MatchCgt(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Cgt) { return true; } return false; } public static bool MatchCgtUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Cgt_Un) { return true; } return false; } public static bool MatchClt(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Clt) { return true; } return false; } public static bool MatchCltUn(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Clt_Un) { return true; } return false; } public static bool MatchLdftn(this Instruction instr, string typeFullName, string name) { if (instr.MatchLdftn(out var value)) { return value.Is(typeFullName, name); } return false; } public static bool MatchLdftn(this Instruction instr, string name) { if (instr.MatchLdftn(out var value)) { return value.Is(typeof(T), name); } return false; } public static bool MatchLdftn(this Instruction instr, Type type, string name) { if (instr.MatchLdftn(out var value)) { return value.Is(type, name); } return false; } public static bool MatchLdftn(this Instruction instr, MethodBase value) { if (instr.MatchLdftn(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdftn(this Instruction instr, MethodReference value) { if (instr.MatchLdftn(out var value2)) { return value2 == value; } return false; } public static bool MatchLdftn(this Instruction instr, out MethodReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldftn) { object operand = instr.Operand; value = (MethodReference)((operand is MethodReference) ? operand : null); return true; } value = null; return false; } public static bool MatchLdvirtftn(this Instruction instr, string typeFullName, string name) { if (instr.MatchLdvirtftn(out var value)) { return value.Is(typeFullName, name); } return false; } public static bool MatchLdvirtftn(this Instruction instr, string name) { if (instr.MatchLdvirtftn(out var value)) { return value.Is(typeof(T), name); } return false; } public static bool MatchLdvirtftn(this Instruction instr, Type type, string name) { if (instr.MatchLdvirtftn(out var value)) { return value.Is(type, name); } return false; } public static bool MatchLdvirtftn(this Instruction instr, MethodBase value) { if (instr.MatchLdvirtftn(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchLdvirtftn(this Instruction instr, MethodReference value) { if (instr.MatchLdvirtftn(out var value2)) { return value2 == value; } return false; } public static bool MatchLdvirtftn(this Instruction instr, out MethodReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldvirtftn) { object operand = instr.Operand; value = (MethodReference)((operand is MethodReference) ? operand : null); return true; } value = null; return false; } public static bool MatchLdarg(this Instruction instr, int value) { if (instr.MatchLdarg(out var value2)) { return value2 == value; } return false; } public static bool MatchLdarg(this Instruction instr, out int value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_0083: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldarg || instr.OpCode == OpCodes.Ldarg_S) { value = ((ParameterReference)instr.Operand).Index; return true; } if (instr.OpCode == OpCodes.Ldarg_0) { value = 0; return true; } if (instr.OpCode == OpCodes.Ldarg_1) { value = 1; return true; } if (instr.OpCode == OpCodes.Ldarg_2) { value = 2; return true; } if (instr.OpCode == OpCodes.Ldarg_3) { value = 3; return true; } value = 0; return false; } public static bool MatchLdarga(this Instruction instr, int value) { if (instr.MatchLdarga(out var value2)) { return value2 == value; } return false; } public static bool MatchLdarga(this Instruction instr, out int value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldarga || instr.OpCode == OpCodes.Ldarga_S) { value = ((ParameterReference)instr.Operand).Index; return true; } value = 0; return false; } public static bool MatchStarg(this Instruction instr, int value) { if (instr.MatchStarg(out var value2)) { return value2 == value; } return false; } public static bool MatchStarg(this Instruction instr, out int value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Starg || instr.OpCode == OpCodes.Starg_S) { value = ((ParameterReference)instr.Operand).Index; return true; } value = 0; return false; } public static bool MatchLdloc(this Instruction instr, int value) { if (instr.MatchLdloc(out var value2)) { return value2 == value; } return false; } public static bool MatchLdloc(this Instruction instr, out int value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_0083: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldloc || instr.OpCode == OpCodes.Ldloc_S) { value = ((VariableReference)instr.Operand).Index; return true; } if (instr.OpCode == OpCodes.Ldloc_0) { value = 0; return true; } if (instr.OpCode == OpCodes.Ldloc_1) { value = 1; return true; } if (instr.OpCode == OpCodes.Ldloc_2) { value = 2; return true; } if (instr.OpCode == OpCodes.Ldloc_3) { value = 3; return true; } value = 0; return false; } public static bool MatchLdloca(this Instruction instr, int value) { if (instr.MatchLdloca(out var value2)) { return value2 == value; } return false; } public static bool MatchLdloca(this Instruction instr, out int value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Ldloca || instr.OpCode == OpCodes.Ldloca_S) { value = ((VariableReference)instr.Operand).Index; return true; } value = 0; return false; } public static bool MatchStloc(this Instruction instr, int value) { if (instr.MatchStloc(out var value2)) { return value2 == value; } return false; } public static bool MatchStloc(this Instruction instr, out int value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_0083: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Stloc || instr.OpCode == OpCodes.Stloc_S) { value = ((VariableReference)instr.Operand).Index; return true; } if (instr.OpCode == OpCodes.Stloc_0) { value = 0; return true; } if (instr.OpCode == OpCodes.Stloc_1) { value = 1; return true; } if (instr.OpCode == OpCodes.Stloc_2) { value = 2; return true; } if (instr.OpCode == OpCodes.Stloc_3) { value = 3; return true; } value = 0; return false; } public static bool MatchLocalloc(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Localloc) { return true; } return false; } public static bool MatchEndfilter(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Endfilter) { return true; } return false; } public static bool MatchUnaligned(this Instruction instr, sbyte value) { if (instr.MatchUnaligned(out var value2)) { return value2 == value; } return false; } public static bool MatchUnaligned(this Instruction instr, out sbyte value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Unaligned) { value = (sbyte)instr.Operand; return true; } value = 0; return false; } public static bool MatchVolatile(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Volatile) { return true; } return false; } public static bool MatchTail(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Tail) { return true; } return false; } public static bool MatchInitobj(this Instruction instr, string fullName) { if (instr.MatchInitobj(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchInitobj(this Instruction instr) { if (instr.MatchInitobj(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchInitobj(this Instruction instr, Type value) { if (instr.MatchInitobj(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchInitobj(this Instruction instr, TypeReference value) { if (instr.MatchInitobj(out var value2)) { return value2 == value; } return false; } public static bool MatchInitobj(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Initobj) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchConstrained(this Instruction instr, string fullName) { if (instr.MatchConstrained(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchConstrained(this Instruction instr) { if (instr.MatchConstrained(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchConstrained(this Instruction instr, Type value) { if (instr.MatchConstrained(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchConstrained(this Instruction instr, TypeReference value) { if (instr.MatchConstrained(out var value2)) { return value2 == value; } return false; } public static bool MatchConstrained(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Constrained) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchCpblk(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Cpblk) { return true; } return false; } public static bool MatchInitblk(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Initblk) { return true; } return false; } public static bool MatchNo(this Instruction instr, sbyte value) { if (instr.MatchNo(out var value2)) { return value2 == value; } return false; } public static bool MatchNo(this Instruction instr, out sbyte value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.No) { value = (sbyte)instr.Operand; return true; } value = 0; return false; } public static bool MatchRethrow(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Rethrow) { return true; } return false; } public static bool MatchSizeof(this Instruction instr, string fullName) { if (instr.MatchSizeof(out var value)) { return ((MemberReference)(object)value).Is(fullName); } return false; } public static bool MatchSizeof(this Instruction instr) { if (instr.MatchSizeof(out var value)) { return ((MemberReference)(object)value).Is(typeof(T)); } return false; } public static bool MatchSizeof(this Instruction instr, Type value) { if (instr.MatchSizeof(out var value2)) { return ((MemberReference)(object)value2).Is(value); } return false; } public static bool MatchSizeof(this Instruction instr, TypeReference value) { if (instr.MatchSizeof(out var value2)) { return value2 == value; } return false; } public static bool MatchSizeof(this Instruction instr, out TypeReference value) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown if (instr.OpCode == OpCodes.Sizeof) { value = (TypeReference)instr.Operand; return true; } value = null; return false; } public static bool MatchRefanytype(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Refanytype) { return true; } return false; } public static bool MatchReadonly(this Instruction instr) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) if (instr.OpCode == OpCodes.Readonly) { return true; } return false; } } public interface IILReferenceBag { T Get(int id); MethodInfo GetGetter(); int Store(T t); void Clear(int id); MethodInfo GetDelegateInvoker() where T : Delegate; } public sealed class NopILReferenceBag : IILReferenceBag { public static readonly NopILReferenceBag Instance = new NopILReferenceBag(); private Exception NOP() { return new NotSupportedException("Inline references not supported in this context"); } public T Get(int id) { throw NOP(); } public MethodInfo GetGetter() { throw NOP(); } public int Store(T t) { throw NOP(); } public void Clear(int id) { throw NOP(); } public MethodInfo GetDelegateInvoker() where T : Delegate { throw NOP(); } } public sealed class RuntimeILReferenceBag : IILReferenceBag { public static class InnerBag { private static T[] array = new T[4]; private static int count; public static readonly MethodInfo Getter = typeof(InnerBag).GetMethod("Get"); private static readonly object _storeLock = new object(); public static T Get(int id) { lock (_storeLock) { return array[id]; } } public static int Store(T t) { lock (_storeLock) { if (count == array.Length) { T[] destinationArray = new T[array.Length * 2]; Array.Copy(array, destinationArray, array.Length); array = destinationArray; } array[count] = t; return count++; } } public static void Clear(int id) { lock (_storeLock) { array[id] = default(T); } } } public static class FastDelegateInvokers { public delegate void Action(T1 arg1); public delegate TResult Func(T1 arg1); public delegate void Action(T1 arg1, T2 arg2); public delegate TResult Func(T1 arg1, T2 arg2); public delegate void Action(T1 arg1, T2 arg2, T3 arg3); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15); public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16); private static readonly MethodInfo[] actions; private static readonly MethodInfo[] funcs; static FastDelegateInvokers() { IOrderedEnumerable source = from m in typeof(FastDelegateInvokers).GetMethods() where m.Name == "Invoke" orderby m.GetParameters().Length select m; actions = source.Where((MethodInfo m) => m.ReturnType == typeof(void)).ToArray(); funcs = source.Where((MethodInfo m) => m.ReturnType != typeof(void)).ToArray(); } public static MethodInfo GetInvoker(MethodInfo signature) { object[] customAttributes = signature.ReturnTypeCustomAttributes.GetCustomAttributes(inherit: true); if (((customAttributes != null && customAttributes.Length != 0) ? 1 : 0) > (false ? 1 : 0) || signature.ReturnType.IsByRef || signature.ReturnType.IsMarshalByRef) { return null; } bool flag = signature.ReturnType != typeof(void); ParameterInfo[] parameters = signature.GetParameters(); int num = parameters.Length; if (num > actions.Length) { return null; } Type[] array = new Type[parameters.Length + (flag ? 1 : 0)]; int num2 = 0; while (num2 < num) { ParameterInfo parameterInfo = parameters[num2]; if (parameterInfo.Attributes == ParameterAttributes.None) { object[] customAttributes2 = parameterInfo.GetCustomAttributes(inherit: true); if (((customAttributes2 != null && customAttributes2.Length != 0) ? 1 : 0) <= (false ? 1 : 0) && !parameterInfo.ParameterType.IsByRef && !parameterInfo.ParameterType.IsMarshalByRef) { array[num2] = parameterInfo.ParameterType; num2++; continue; } } return null; } if (flag) { array[num] = signature.ReturnType; } return (flag ? funcs : actions)[num - 1].MakeGenericMethod(array); } public static void Invoke(T1 arg1, Action del) { del(arg1); } public static TResult Invoke(T1 arg1, Func del) { return del(arg1); } public static void Invoke(T1 arg1, T2 arg2, Action del) { del(arg1, arg2); } public static TResult Invoke(T1 arg1, T2 arg2, Func del) { return del(arg1, arg2); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, Action del) { del(arg1, arg2, arg3); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, Func del) { return del(arg1, arg2, arg3); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, Action del) { del(arg1, arg2, arg3, arg4); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, Func del) { return del(arg1, arg2, arg3, arg4); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Action del) { del(arg1, arg2, arg3, arg4, arg5); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Func del) { return del(arg1, arg2, arg3, arg4, arg5); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); } public static void Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, Action del) { del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); } public static TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, Func del) { return del(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); } } public static readonly RuntimeILReferenceBag Instance = new RuntimeILReferenceBag(); private static readonly Dictionary invokerCache = new Dictionary(); public T Get(int id) { return InnerBag.Get(id); } public MethodInfo GetGetter() { return InnerBag.Getter; } public int Store(T t) { return InnerBag.Store(t); } public void Clear(int id) { InnerBag.Clear(id); } public MethodInfo GetDelegateInvoker() where T : Delegate { //IL_00f3: Unknown result type (might be due to invalid IL or missing references) //IL_0108: Unknown result type (might be due to invalid IL or missing references) //IL_0124: Unknown result type (might be due to invalid IL or missing references) //IL_0131: Unknown result type (might be due to invalid IL or missing references) Type typeFromHandle = typeof(T); MethodInfo result; if (invokerCache.TryGetValue(typeFromHandle, out var value)) { if (value == null) { return null; } result = value.Target as MethodInfo; if (value.IsAlive) { return result; } } MethodInfo method = typeFromHandle.GetMethod("Invoke"); ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length == 0) { invokerCache[typeFromHandle] = null; return null; } result = FastDelegateInvokers.GetInvoker(method); if (result != null) { invokerCache[typeFromHandle] = new WeakReference(result); return result; } Type[] array = new Type[parameters.Length + 1]; for (int i = 0; i < parameters.Length; i++) { array[i] = parameters[i].ParameterType; } array[parameters.Length] = typeof(T); using DynamicMethodDefinition dynamicMethodDefinition = new DynamicMethodDefinition("MMIL:Invoke<" + method.DeclaringType.FullName + ">", method.ReturnType, array); ILProcessor iLProcessor = dynamicMethodDefinition.GetILProcessor(); iLProcessor.Emit(OpCodes.Ldarg, parameters.Length); for (int j = 0; j < parameters.Length; j++) { iLProcessor.Emit(OpCodes.Ldarg, j); } iLProcessor.Emit(OpCodes.Callvirt, method); iLProcessor.Emit(OpCodes.Ret); result = dynamicMethodDefinition.Generate(); invokerCache[typeFromHandle] = new WeakReference(result); return result; } } }