How to create a type at runtime and unload characters after use

Problem

I need to create a type dynamically (without dynamic or ExpandoObject), I need a type to get a strong type when returning my request (also dynamically).

I tried using Reflection.Emitand CodeDOM, but both retain the characters created in memory.

My attempts

Using Reflection.Emit, I tried AssemblyBuilderAccess.RunAndCollect, and this work, but only if I do not use the type in my Query (Dapper) and call GC.Collect().

I tried destroying the objects Reflection.Emit, but without success ( https://stackoverflow.com/a/167189/ );

I tried using GC.Collect(), GC.SuppressFinalize()to free the variables, but it does not work.

I tried to create the type on another AppDomain in order to unload the domain after use, but I could not for two reasons: 1. DoCallBack should be used, if I do, I cannot pass the type to other methods; 2. Attempting to use DoCallBack from the outside. I get an error message:"Type 'System.Reflection.Emit.AssemblyBuilder' in assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."

Does anyone know how to solve this?

+4
source share

Source: https://habr.com/ru/post/1691458/


All Articles