using System; using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; namespace ScreenConnect; public class EmitBuilder { private static AssemblyBuilder assemblyBuilder = ReflectionExtensions.DefineDynamicAssembly(new AssemblyName(typeof(EmitBuilder).FullName), AssemblyBuilderAccess.Run); private static ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(typeof(EmitBuilder).FullName); private static Dictionary cache = new Dictionary(); public static T GetOrBuild(object cacheKey, Func buildFunc) { lock (cache) { return (T)cache.GetValueAndCreateIfNotFound(cacheKey, () => buildFunc(moduleBuilder)); } } }