Cut the polyline with boundary rectangle c#

I have polylines that are partially in boundary rectangle. I need to cut off their outside parts and keep the inside slices. What is the easiest way to do it in c# ?

Hi,

This is exactly replicating the Trim with Brep component.
Build a closed box from the rectangle, test for intersections, shatter the curve (curve.Split()) and select pieces that have their middle inside the box.