temp0
✅ Sharing executing Assembly in Roslyn script
@reflectronic Wasn't sure if I should open another thread for this question but you seem to know your way around roslyn/ALCs... It seems loading up script object and running
Compile()
will load a lot of necessary assemblies and incur about 50mb overhead in memory, is there any design pattern for reclaiming memory other than running in a separate process? I have tried a custom ALC with .Unload() at the end but I think it's moreso all the libs that roslyn loads in that take up that memory13 replies
✅ Sharing executing Assembly in Roslyn script
Did some debugging and they were indeed two different assemblies in memory although pointed to the same module on disk... Maybe this could be part of the embedded runtime from
nethost
?
Just to "get things work" i used a workaround with reflection in the assembly but I'm pretty sure it's duplicating it for every instance I call the script uncached which is a no go. Anyone have any insight into sharing an assembly across Roslyn scripts or an alternative for script compiling?
var scriptOptions = ScriptOptions.Default.WithReferences(MetadataReference.CreateFromFile($"{directory}/DotNetTypes.dll"));
var state = await CSharpScript.RunAsync<object>(text, scriptOptions);
NpcMap[npcName] = state.ReturnValue;
var instance = NpcMap[npcName];
var createNpcEvent = instance.GetType()?.BaseType?.Assembly.ExportedTypes.FirstOrDefault(f => f.FullName == "EqFactory")?.GetMethod("CreateNpcEvent");
if (createNpcEvent != null)
{
var npcEvent = createNpcEvent.Invoke(instance, [initArgs?.Zone, initArgs?.EntityList, npcEventArgs.Npc, npcEventArgs.Mob, message ?? ""]);
var methodInfo = instance.GetType().GetMethod(MethodMap[id]);
if (methodInfo != null)
{
methodInfo.Invoke(instance, [npcEvent]);
}
}
13 replies