Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Generic IEnumerator not disposed

Open andy-clymer opened this issue 2 years ago • 0 comments

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.

andy-clymer avatar Dec 07 '23 19:12 andy-clymer