MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.32k stars 329 forks source link

Generic IEnumerator not disposed #667

Open andy-clymer opened 10 months ago

andy-clymer commented 10 months ago

I'm using Mapster.Tool to generate extension methods from my mapping registrations like so:

config.NewConfig<TSource, TDestination>().GenerateMapper(MapType.Map | MapType.MapToTarget);

When inspecting the generated code I noticed that anywhere GetEnumerator() is used, the generic Enumerator is never disposed.

This is a snippet of what is being generated:

...
IEnumerator<string> enumerator = p2.GetEnumerator();

while (enumerator.MoveNext())
 {
    string item = enumerator.Current;
    result.Add(item);
}

return result;

I believe what it should be generating is the following:

using IEnumerator<string> enumerator = p2.GetEnumerator();

while (enumerator.MoveNext())
 {
    string item = enumerator.Current;
    result.Add(item);
}

return result;

I'm not sure if this issue arises in other areas (e.g. generated code from interfaces) as I haven't tested it personally, but I would expect the Mapster.Tool generates the code the same way.