4

The Problem

I need create type dynamicly (without dynamic or ExpandoObject), I need of Type to get strong type on return of my query (making dynamicly too).

I have tried use Reflection.Emit and CodeDOM, but both keep the symbols created on memory.

My attemps

Using Reflection.Emit, I've tried AssemblyBuilderAccess.RunAndCollect, and this work, but just if I don't use the type on my Query (Dapper) and call GC.Collect().

I've tried Dispose the objects of Reflection.Emit, but without success (https://stackoverflow.com/questions/2503645/reflect-emit-dynamic-type-memory-blowup?answertab=active#tab-top);

I have tried use GC.Collect(), GC.SuppressFinalize(), to release the variables, but does not work.

I have tried create the type on another AppDomain, to Unload the domain after usage, but I did can't, by two reasons: 1. Should be use on DoCallBack, if I to do that, I can't pass the type for another methods; 2. Trying use outside DoCallBack I get the error: "Type 'System.Reflection.Emit.AssemblyBuilder' in assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."

Anybody know how solve that?

Renatto Machado
  • 1,534
  • 2
  • 16
  • 33
  • 1
    Using a second `AppDomain` is the only way to unload a assembly from memory. Working with AppDomains can be tricky, you have to ensure that you do no pass objects across the AppDomain boundary. – thehennyy Dec 28 '17 at 13:39

0 Answers0