How to trim a surface using a 3d curve

I have a 3d closed curve and a surface. I don’t know how to split/ cut a portion of this surface using the 3d trimming curve. I found a Method called split for brepface but i don’t know how to convert my surface into a brepface. I also came across a split method for brep but again i dont know how to generate the brepsplitter necessary for this method. I am using c# in rhinocommon. Please help.

Step 1.

Convert your surface to a BREP.

Surface s; // defined elsewhere
Brep b = s.ToBrep();

When converting 1 surface to a BREP, the BREP has 1 face that is the surface

Step 2.

Split the one and only face of the BREP with the curve

Curve splitter; // defined elsewhere
double splitTolerance = doc.ModelAbsoluteTolerance; // or some other value
Brep split = b.Faces[0].Split(new Curve[]{splitter}, splitTolerance);

if (split.Faces.Count != 2)
    return; // something went wrong

Brep firstHalf = split.Faces[0].DuplicateFace();
Brep secondHalf = split.Faces[1].DuplicateFace();